home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / AbstractSet.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.2 KB  |  94 lines

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