1. /*
  2. * @(#)AbstractSet.java 1.14 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.util;
  11. /**
  12. * This class provides a skeletal implementation of the <tt>Set</tt>
  13. * interface to minimize the effort required to implement this
  14. * interface. <p>
  15. *
  16. * The process of implementing a set by extending this class is identical
  17. * to that of implementing a Collection by extending AbstractCollection,
  18. * except that all of the methods and constructors in subclasses of this
  19. * class must obey the additional constraints imposed by the <tt>Set</tt>
  20. * interface (for instance, the add method must not permit addition of
  21. * multiple intances of an object to a set).<p>
  22. *
  23. * Note that this class does not override any of the implementations from
  24. * the <tt>AbstractCollection</tt> class. It merely adds implementations
  25. * for <tt>equals</tt> and <tt>hashCode</tt>.
  26. *
  27. * @author Josh Bloch
  28. * @version 1.14, 02/02/00
  29. * @see Collection
  30. * @see AbstractCollection
  31. * @see Set
  32. * @since 1.2
  33. */
  34. public abstract class AbstractSet extends AbstractCollection implements Set {
  35. /**
  36. * Sole constructor. (For invocation by subclass constructors, typically
  37. * implicit.)
  38. */
  39. protected AbstractSet() {
  40. }
  41. // Comparison and hashing
  42. /**
  43. * Compares the specified object with this set for equality. Returns
  44. * <tt>true</tt> if the given object is also a set, the two sets have
  45. * the same size, and every member of the given set is contained in
  46. * this set. This ensures that the <tt>equals</tt> method works
  47. * properly across different implementations of the <tt>Set</tt>
  48. * interface.<p>
  49. *
  50. * This implementation first checks if the specified object is this
  51. * set; if so it returns <tt>true</tt>. Then, it checks if the
  52. * specified object is a set whose size is identical to the size of
  53. * this set; if not, it it returns false. If so, it returns
  54. * <tt>containsAll((Collection) o)</tt>.
  55. *
  56. * @param o Object to be compared for equality with this set.
  57. * @return <tt>true</tt> if the specified object is equal to this set.
  58. */
  59. public boolean equals(Object o) {
  60. if (o == this)
  61. return true;
  62. if (!(o instanceof Set))
  63. return false;
  64. Collection c = (Collection) o;
  65. if (c.size() != size())
  66. return false;
  67. return containsAll(c);
  68. }
  69. /**
  70. * Returns the hash code value for this set. The hash code of a set is
  71. * defined to be the sum of the hash codes of the elements in the set.
  72. * This ensures that <tt>s1.equals(s2)</tt> implies that
  73. * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
  74. * and <tt>s2</tt>, as required by the general contract of
  75. * Object.hashCode.<p>
  76. *
  77. * This implementation enumerates over the set, calling the
  78. * <tt>hashCode</tt> method on each element in the collection, and
  79. * adding up the results.
  80. *
  81. * @return the hash code value for this set.
  82. */
  83. public int hashCode() {
  84. int h = 0;
  85. Iterator i = iterator();
  86. while (i.hasNext()) {
  87. Object obj = i.next();
  88. if (obj != null)
  89. h += obj.hashCode();
  90. }
  91. return h;
  92. }
  93. /**
  94. * Removes from this set all of its elements that are contained in
  95. * the specified collection (optional operation).<p>
  96. *
  97. * This implementation determines which is the smaller of this set
  98. * and the specified collection, by invoking the <tt>size</tt>
  99. * method on each. If this set has fewer elements, then the
  100. * implementation iterates over this set, checking each element
  101. * returned by the iterator in turn to see if it is contained in
  102. * the specified collection. If it is so contained, it is removed
  103. * from this set with the iterator's <tt>remove</tt> method. If
  104. * the specified collection has fewer elements, then the
  105. * implementation iterates over the specified collection, removing
  106. * from this set each element returned by the iterator, using this
  107. * set's <tt>remove</tt> method.<p>
  108. *
  109. * Note that this implementation will throw an
  110. * <tt>UnsupportedOperationException</tt> if the iterator returned by the
  111. * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.
  112. *
  113. * @param c elements to be removed from this set.
  114. * @return <tt>true</tt> if this set changed as a result of the call.
  115. *
  116. * @throws UnsupportedOperationException removeAll is not supported
  117. * by this set.
  118. * @see #remove(Object)
  119. * @see #contains(Object)
  120. */
  121. public boolean removeAll(Collection c) {
  122. boolean modified = false;
  123. if (size() > c.size()) {
  124. for (Iterator i = c.iterator(); i.hasNext(); )
  125. modified |= remove(i.next());
  126. } else {
  127. for (Iterator i = iterator(); i.hasNext(); ) {
  128. if(c.contains(i.next())) {
  129. i.remove();
  130. modified = true;
  131. }
  132. }
  133. }
  134. return modified;
  135. }
  136. }