AbstractSet.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* AbstractSet.java -- Abstract implementation of most of Set
  2. Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. /**
  33. * An abstract implementation of Set to make it easier to create your own
  34. * implementations. In order to create a Set, subclass AbstractSet and
  35. * implement the same methods that are required for AbstractCollection
  36. * (although these methods must of course meet the requirements that Set puts
  37. * on them - specifically, no element may be in the set more than once). This
  38. * class simply provides implementations of equals() and hashCode() to fulfil
  39. * the requirements placed on them by the Set interface.
  40. *
  41. * @author Original author unknown
  42. * @author Eric Blake <ebb9@email.byu.edu>
  43. * @see Collection
  44. * @see AbstractCollection
  45. * @see Set
  46. * @see HashSet
  47. * @see TreeSet
  48. * @see LinkedHashSet
  49. * @since 1.2
  50. * @status updated to 1.4
  51. */
  52. public abstract class AbstractSet extends AbstractCollection implements Set
  53. {
  54. /**
  55. * The main constructor, for use by subclasses.
  56. */
  57. protected AbstractSet()
  58. {
  59. }
  60. /**
  61. * Tests whether the given object is equal to this Set. This implementation
  62. * first checks whether this set <em>is</em> the given object, and returns
  63. * true if so. Otherwise, if o is a Set and is the same size as this one, it
  64. * returns the result of calling containsAll on the given Set. Otherwise, it
  65. * returns false.
  66. *
  67. * @param o the Object to be tested for equality with this Set
  68. * @return true if the given object is equal to this Set
  69. */
  70. public boolean equals(Object o)
  71. {
  72. return (o == this ||
  73. (o instanceof Set && ((Set) o).size() == size()
  74. && containsAll((Collection) o)));
  75. }
  76. /**
  77. * Returns a hash code for this Set. The hash code of a Set is the sum of the
  78. * hash codes of all its elements, except that the hash code of null is
  79. * defined to be zero. This implementation obtains an Iterator over the Set,
  80. * and sums the results.
  81. *
  82. * @return a hash code for this Set
  83. */
  84. public int hashCode()
  85. {
  86. Iterator itr = iterator();
  87. int hash = 0;
  88. int pos = size();
  89. while (--pos >= 0)
  90. hash += hashCode(itr.next());
  91. return hash;
  92. }
  93. /**
  94. * Removes from this set all elements in the given collection (optional
  95. * operation). This implementation uses <code>size()</code> to determine
  96. * the smaller collection. Then, if this set is smaller, it iterates
  97. * over the set, calling Iterator.remove if the collection contains
  98. * the element. If this set is larger, it iterates over the collection,
  99. * calling Set.remove for all elements in the collection. Note that
  100. * this operation will fail if a remove methods is not supported.
  101. *
  102. * @param c the collection of elements to remove
  103. * @return true if the set was modified as a result
  104. * @throws UnsupportedOperationException if remove is not supported
  105. * @throws NullPointerException if the collection is null
  106. * @see AbstractCollection#remove(Object)
  107. * @see Collection#contains(Object)
  108. * @see Iterator#remove()
  109. */
  110. public boolean removeAll(Collection c)
  111. {
  112. int oldsize = size();
  113. int count = c.size();
  114. Iterator i;
  115. if (oldsize < count)
  116. {
  117. for (i = iterator(), count = oldsize; count > 0; count--)
  118. if (c.contains(i.next()))
  119. i.remove();
  120. }
  121. else
  122. for (i = c.iterator(); count > 0; count--)
  123. remove(i.next());
  124. return oldsize != size();
  125. }
  126. }