AbstractSet.java 5.1 KB

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