OSDN Git Service

e45e47ea84d9c67abc24899e3a2dc22c21b8524b
[pf3gnuchains/gcc-fork.git] / libjava / java / util / AbstractSet.java
1 /* AbstractSet.java -- Abstract implementation of most of Set
2    Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
26
27
28 package java.util;
29
30 /**
31  * An abstract implementation of Set to make it easier to create your own
32  * implementations. In order to create a Set, subclass AbstractSet and
33  * implement the same methods that are required for AbstractCollection
34  * (although these methods must of course meet the requirements that Set puts
35  * on them - specifically, no element may be in the set more than once). This
36  * class simply provides implementations of equals() and hashCode() to fulfil
37  * the requirements placed on them by the Set interface.
38  *
39  * @author Original author unknown
40  * @author Eric Blake <ebb9@email.byu.edu>
41  * @see Collection
42  * @see AbstractCollection
43  * @see Set
44  * @see HashSet
45  * @see TreeSet
46  * @see LinkedHashSet
47  * @since 1.2
48  * @status updated to 1.4
49  */
50 public abstract class AbstractSet extends AbstractCollection implements Set
51 {
52   /**
53    * The main constructor, for use by subclasses.
54    */
55   protected AbstractSet()
56   {
57   }
58
59   /**
60    * Tests whether the given object is equal to this Set. This implementation
61    * first checks whether this set <em>is</em> the given object, and returns
62    * true if so. Otherwise, if o is a Set and is the same size as this one, it
63    * returns the result of calling containsAll on the given Set. Otherwise, it
64    * returns false.
65    *
66    * @param o the Object to be tested for equality with this Set
67    * @return true if the given object is equal to this Set
68    */
69   public boolean equals(Object o)
70   {
71     return (o == this ||
72             (o instanceof Set && ((Set) o).size() == size()
73              && containsAll((Collection) o)));
74   }
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   /**
95    * Removes from this set all elements in the given collection (optional
96    * operation). This implementation uses <code>size()</code> to determine
97    * the smaller collection.  Then, if this set is smaller, it iterates
98    * over the set, calling Iterator.remove if the collection contains
99    * the element.  If this set is larger, it iterates over the collection,
100    * calling Set.remove for all elements in the collection. Note that
101    * this operation will fail if a remove methods is not supported.
102    *
103    * @param c the collection of elements to remove
104    * @return true if the set was modified as a result
105    * @throws UnsupportedOperationException if remove is not supported
106    * @throws NullPointerException if the collection is null
107    * @see AbstractCollection#remove(Object)
108    * @see Collection#contains(Object)
109    * @see Iterator#remove()
110    */
111   public boolean removeAll(Collection c)
112   {
113     int oldsize = size();
114     int count = c.size();
115     Iterator i;
116     if (oldsize < count)
117       {
118         for (i = iterator(), count = oldsize; count > 0; count--)
119           if (c.contains(i.next()))
120             i.remove();
121       }
122     else
123       for (i = c.iterator(); count > 0; count--)
124         remove(i.next());
125     return oldsize != size();
126   }
127
128 }