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 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.util;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * An abstract implementation of Set to make it easier to create your own
|
---|
43 | * implementations. In order to create a Set, subclass AbstractSet and
|
---|
44 | * implement the same methods that are required for AbstractCollection
|
---|
45 | * (although these methods must of course meet the requirements that Set puts
|
---|
46 | * on them - specifically, no element may be in the set more than once). This
|
---|
47 | * class simply provides implementations of equals() and hashCode() to fulfil
|
---|
48 | * the requirements placed on them by the Set interface.
|
---|
49 | *
|
---|
50 | * @author Original author unknown
|
---|
51 | * @author Eric Blake <ebb9@email.byu.edu>
|
---|
52 | * @see Collection
|
---|
53 | * @see AbstractCollection
|
---|
54 | * @see Set
|
---|
55 | * @see HashSet
|
---|
56 | * @see TreeSet
|
---|
57 | * @see LinkedHashSet
|
---|
58 | * @since 1.2
|
---|
59 | * @status updated to 1.4
|
---|
60 | */
|
---|
61 | public abstract class AbstractSet extends AbstractCollection implements Set
|
---|
62 | {
|
---|
63 | /**
|
---|
64 | * The main constructor, for use by subclasses.
|
---|
65 | */
|
---|
66 | protected AbstractSet()
|
---|
67 | {
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Tests whether the given object is equal to this Set. This implementation
|
---|
72 | * first checks whether this set <em>is</em> the given object, and returns
|
---|
73 | * true if so. Otherwise, if o is a Set and is the same size as this one, it
|
---|
74 | * returns the result of calling containsAll on the given Set. Otherwise, it
|
---|
75 | * returns false.
|
---|
76 | *
|
---|
77 | * @param o the Object to be tested for equality with this Set
|
---|
78 | * @return true if the given object is equal to this Set
|
---|
79 | */
|
---|
80 | public boolean equals(Object o)
|
---|
81 | {
|
---|
82 | return (o == this ||
|
---|
83 | (o instanceof Set && ((Set) o).size() == size()
|
---|
84 | && containsAll((Collection) o)));
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Returns a hash code for this Set. The hash code of a Set is the sum of the
|
---|
89 | * hash codes of all its elements, except that the hash code of null is
|
---|
90 | * defined to be zero. This implementation obtains an Iterator over the Set,
|
---|
91 | * and sums the results.
|
---|
92 | *
|
---|
93 | * @return a hash code for this Set
|
---|
94 | */
|
---|
95 | public int hashCode()
|
---|
96 | {
|
---|
97 | Iterator itr = iterator();
|
---|
98 | int hash = 0;
|
---|
99 | int pos = size();
|
---|
100 | while (--pos >= 0)
|
---|
101 | hash += hashCode(itr.next());
|
---|
102 | return hash;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Removes from this set all elements in the given collection (optional
|
---|
107 | * operation). This implementation uses <code>size()</code> to determine
|
---|
108 | * the smaller collection. Then, if this set is smaller, it iterates
|
---|
109 | * over the set, calling Iterator.remove if the collection contains
|
---|
110 | * the element. If this set is larger, it iterates over the collection,
|
---|
111 | * calling Set.remove for all elements in the collection. Note that
|
---|
112 | * this operation will fail if a remove methods is not supported.
|
---|
113 | *
|
---|
114 | * @param c the collection of elements to remove
|
---|
115 | * @return true if the set was modified as a result
|
---|
116 | * @throws UnsupportedOperationException if remove is not supported
|
---|
117 | * @throws NullPointerException if the collection is null
|
---|
118 | * @see AbstractCollection#remove(Object)
|
---|
119 | * @see Collection#contains(Object)
|
---|
120 | * @see Iterator#remove()
|
---|
121 | */
|
---|
122 | public boolean removeAll(Collection c)
|
---|
123 | {
|
---|
124 | int oldsize = size();
|
---|
125 | int count = c.size();
|
---|
126 | Iterator i;
|
---|
127 | if (oldsize < count)
|
---|
128 | {
|
---|
129 | for (i = iterator(), count = oldsize; count > 0; count--)
|
---|
130 | if (c.contains(i.next()))
|
---|
131 | i.remove();
|
---|
132 | }
|
---|
133 | else
|
---|
134 | for (i = c.iterator(); count > 0; count--)
|
---|
135 | remove(i.next());
|
---|
136 | return oldsize != size();
|
---|
137 | }
|
---|
138 |
|
---|
139 | }
|
---|