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