1. /*
  2. * @(#)AbstractSet.java 1.9 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. /**
  9. * This class provides a skeletal implementation of the <tt>Set</tt>
  10. * interface to minimize the effort required to implement this
  11. * interface. <p>
  12. *
  13. * The process of implementing a set by extending this class is identical
  14. * to that of implementing a Collection by extending AbstractCollection,
  15. * except that all of the methods and constructors in subclasses of this
  16. * class must obey the additional constraints imposed by the <tt>Set</tt>
  17. * interface (for instance, the add method must not permit addition of
  18. * multiple intances of an object to a set).<p>
  19. *
  20. * Note that this class does not override any of the implementations from
  21. * the <tt>AbstractCollection</tt> class. It merely adds implementations
  22. * for <tt>equals</tt> and <tt>hashCode</tt>.
  23. *
  24. * @author Josh Bloch
  25. * @version 1.9 11/29/01
  26. * @see Collection
  27. * @see AbstractCollection
  28. * @see Set
  29. * @since JDK1.2
  30. */
  31. public abstract class AbstractSet extends AbstractCollection implements Set {
  32. /**
  33. * Sole constructor. (For invocation by subclass constructors, typically
  34. * implicit.)
  35. */
  36. protected AbstractSet() {
  37. }
  38. // Comparison and hashing
  39. /**
  40. * Compares the specified object with this set for equality. Returns
  41. * <tt>true</tt> if the given object is also a set, the two sets have
  42. * the same size, and every member of the given set is contained in
  43. * this set. This ensures that the <tt>equals</tt> method works
  44. * properly across different implementations of the <tt>Set</tt>
  45. * interface.<p>
  46. *
  47. * This implementation first checks if the specified object is this
  48. * set; if so it returns <tt>true</tt>. Then, it checks if the
  49. * specified object is a set whose size is identical to the size of
  50. * this set; if not, it it returns false. If so, it returns
  51. * <tt>containsAll((Collection) o)</tt>.
  52. *
  53. * @param o Object to be compared for equality with this set.
  54. * @return <tt>true</tt> if the specified object is equal to this set.
  55. */
  56. public boolean equals(Object o) {
  57. if (o == this)
  58. return true;
  59. if (!(o instanceof Set))
  60. return false;
  61. Collection c = (Collection) o;
  62. if (c.size() != size())
  63. return false;
  64. return containsAll(c);
  65. }
  66. /**
  67. * Returns the hash code value for this set. The hash code of a set is
  68. * defined to be the sum of the hash codes of the elements in the set.
  69. * This ensures that <tt>s1.equals(s2)</tt> implies that
  70. * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
  71. * and <tt>s2</tt>, as required by the general contract of
  72. * Object.hashCode.<p>
  73. *
  74. * This implementation enumerates over the set, calling the
  75. * <tt>hashCode</tt> method on each element in the collection, and
  76. * adding up the results.
  77. *
  78. * @returns the hash code value for this set.
  79. */
  80. public int hashCode() {
  81. int h = 0;
  82. Iterator i = iterator();
  83. while (i.hasNext()) {
  84. Object obj = i.next();
  85. if (obj != null)
  86. h += obj.hashCode();
  87. }
  88. return h;
  89. }
  90. }