1 |
14 |
jlechner |
/* Set.java -- A collection that prohibits duplicates
|
2 |
|
|
Copyright (C) 1998, 2001, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 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 |
|
|
* A collection that contains no duplicates. In other words, for two set
|
43 |
|
|
* elements e1 and e2, <code>e1.equals(e2)</code> returns false. There
|
44 |
|
|
* are additional stipulations on <code>add</code>, <code>equals</code>
|
45 |
|
|
* and <code>hashCode</code>, as well as the requirements that constructors
|
46 |
|
|
* do not permit duplicate elements. The Set interface is incompatible with
|
47 |
|
|
* List; you cannot implement both simultaneously.
|
48 |
|
|
* <p>
|
49 |
|
|
*
|
50 |
|
|
* Note: Be careful about using mutable objects in sets. In particular,
|
51 |
|
|
* if a mutable object changes to become equal to another set element, you
|
52 |
|
|
* have violated the contract. As a special case of this, a Set is not
|
53 |
|
|
* allowed to be an element of itself, without risking undefined behavior.
|
54 |
|
|
*
|
55 |
|
|
* @author Original author unknown
|
56 |
|
|
* @author Eric Blake (ebb9@email.byu.edu)
|
57 |
|
|
* @see Collection
|
58 |
|
|
* @see List
|
59 |
|
|
* @see SortedSet
|
60 |
|
|
* @see HashSet
|
61 |
|
|
* @see TreeSet
|
62 |
|
|
* @see LinkedHashSet
|
63 |
|
|
* @see AbstractSet
|
64 |
|
|
* @see Collections#singleton(Object)
|
65 |
|
|
* @see Collections#EMPTY_SET
|
66 |
|
|
* @since 1.2
|
67 |
|
|
* @status updated to 1.4
|
68 |
|
|
*/
|
69 |
|
|
public interface Set extends Collection
|
70 |
|
|
{
|
71 |
|
|
/**
|
72 |
|
|
* Adds the specified element to the set if it is not already present
|
73 |
|
|
* (optional operation). In particular, the comparison algorithm is
|
74 |
|
|
* <code>o == null ? e == null : o.equals(e)</code>. Sets need not permit
|
75 |
|
|
* all values, and may document what exceptions will be thrown if
|
76 |
|
|
* a value is not permitted.
|
77 |
|
|
*
|
78 |
|
|
* @param o the object to add
|
79 |
|
|
* @return true if the object was not previously in the set
|
80 |
|
|
* @throws UnsupportedOperationException if this operation is not allowed
|
81 |
|
|
* @throws ClassCastException if the class of o prevents it from being added
|
82 |
|
|
* @throws IllegalArgumentException if some aspect of o prevents it from
|
83 |
|
|
* being added
|
84 |
|
|
* @throws NullPointerException if null is not permitted in this set
|
85 |
|
|
*/
|
86 |
|
|
boolean add(Object o);
|
87 |
|
|
|
88 |
|
|
/**
|
89 |
|
|
* Adds all of the elements of the given collection to this set (optional
|
90 |
|
|
* operation). If the argument is also a Set, this returns the mathematical
|
91 |
|
|
* <i>union</i> of the two. The behavior is unspecified if the set is
|
92 |
|
|
* modified while this is taking place.
|
93 |
|
|
*
|
94 |
|
|
* @param c the collection to add
|
95 |
|
|
* @return true if the set changed as a result
|
96 |
|
|
* @throws UnsupportedOperationException if this operation is not allowed
|
97 |
|
|
* @throws ClassCastException if the class of an element prevents it from
|
98 |
|
|
* being added
|
99 |
|
|
* @throws IllegalArgumentException if something about an element prevents
|
100 |
|
|
* it from being added
|
101 |
|
|
* @throws NullPointerException if null is not permitted in this set, or
|
102 |
|
|
* if the argument c is null
|
103 |
|
|
* @see #add(Object)
|
104 |
|
|
*/
|
105 |
|
|
boolean addAll(Collection c);
|
106 |
|
|
|
107 |
|
|
/**
|
108 |
|
|
* Removes all elements from this set (optional operation). This set will
|
109 |
|
|
* be empty afterwords, unless an exception occurs.
|
110 |
|
|
*
|
111 |
|
|
* @throws UnsupportedOperationException if this operation is not allowed
|
112 |
|
|
*/
|
113 |
|
|
void clear();
|
114 |
|
|
|
115 |
|
|
/**
|
116 |
|
|
* Returns true if the set contains the specified element. In other words,
|
117 |
|
|
* this looks for <code>o == null ? e == null : o.equals(e)</code>.
|
118 |
|
|
*
|
119 |
|
|
* @param o the object to look for
|
120 |
|
|
* @return true if it is found in the set
|
121 |
|
|
* @throws ClassCastException if the type of o is not a valid type
|
122 |
|
|
* for this set.
|
123 |
|
|
* @throws NullPointerException if o is null and this set doesn't
|
124 |
|
|
* support null values.
|
125 |
|
|
*/
|
126 |
|
|
boolean contains(Object o);
|
127 |
|
|
|
128 |
|
|
/**
|
129 |
|
|
* Returns true if this set contains all elements in the specified
|
130 |
|
|
* collection. If the argument is also a set, this is the <i>subset</i>
|
131 |
|
|
* relationship.
|
132 |
|
|
*
|
133 |
|
|
* @param c the collection to check membership in
|
134 |
|
|
* @return true if all elements in this set are in c
|
135 |
|
|
* @throws NullPointerException if c is null
|
136 |
|
|
* @throws ClassCastException if the type of any element in c is not
|
137 |
|
|
* a valid type for this set.
|
138 |
|
|
* @throws NullPointerException if some element of c is null and this
|
139 |
|
|
* set doesn't support null values.
|
140 |
|
|
* @see #contains(Object)
|
141 |
|
|
*/
|
142 |
|
|
boolean containsAll(Collection c);
|
143 |
|
|
|
144 |
|
|
/**
|
145 |
|
|
* Compares the specified object to this for equality. For sets, the object
|
146 |
|
|
* must be a set, the two must have the same size, and every element in
|
147 |
|
|
* one must be in the other.
|
148 |
|
|
*
|
149 |
|
|
* @param o the object to compare to
|
150 |
|
|
* @return true if it is an equal set
|
151 |
|
|
*/
|
152 |
|
|
boolean equals(Object o);
|
153 |
|
|
|
154 |
|
|
/**
|
155 |
|
|
* Returns the hash code for this set. In order to satisfy the contract of
|
156 |
|
|
* equals, this is the sum of the hashcode of all elements in the set.
|
157 |
|
|
*
|
158 |
|
|
* @return the sum of the hashcodes of all set elements
|
159 |
|
|
* @see #equals(Object)
|
160 |
|
|
*/
|
161 |
|
|
int hashCode();
|
162 |
|
|
|
163 |
|
|
/**
|
164 |
|
|
* Returns true if the set contains no elements.
|
165 |
|
|
*
|
166 |
|
|
* @return true if the set is empty
|
167 |
|
|
*/
|
168 |
|
|
boolean isEmpty();
|
169 |
|
|
|
170 |
|
|
/**
|
171 |
|
|
* Returns an iterator over the set. The iterator has no specific order,
|
172 |
|
|
* unless further specified.
|
173 |
|
|
*
|
174 |
|
|
* @return a set iterator
|
175 |
|
|
*/
|
176 |
|
|
Iterator iterator();
|
177 |
|
|
|
178 |
|
|
/**
|
179 |
|
|
* Removes the specified element from this set (optional operation). If
|
180 |
|
|
* an element e exists, <code>o == null ? e == null : o.equals(e)</code>,
|
181 |
|
|
* it is removed from the set.
|
182 |
|
|
*
|
183 |
|
|
* @param o the object to remove
|
184 |
|
|
* @return true if the set changed (an object was removed)
|
185 |
|
|
* @throws UnsupportedOperationException if this operation is not allowed
|
186 |
|
|
* @throws ClassCastException if the type of o is not a valid type
|
187 |
|
|
* for this set.
|
188 |
|
|
* @throws NullPointerException if o is null and this set doesn't allow
|
189 |
|
|
* the removal of a null value.
|
190 |
|
|
*/
|
191 |
|
|
boolean remove(Object o);
|
192 |
|
|
|
193 |
|
|
/**
|
194 |
|
|
* Removes from this set all elements contained in the specified collection
|
195 |
|
|
* (optional operation). If the argument is a set, this returns the
|
196 |
|
|
* <i>asymmetric set difference</i> of the two sets.
|
197 |
|
|
*
|
198 |
|
|
* @param c the collection to remove from this set
|
199 |
|
|
* @return true if this set changed as a result
|
200 |
|
|
* @throws UnsupportedOperationException if this operation is not allowed
|
201 |
|
|
* @throws NullPointerException if c is null
|
202 |
|
|
* @throws ClassCastException if the type of any element in c is not
|
203 |
|
|
* a valid type for this set.
|
204 |
|
|
* @throws NullPointerException if some element of c is null and this
|
205 |
|
|
* set doesn't support removing null values.
|
206 |
|
|
* @see #remove(Object)
|
207 |
|
|
*/
|
208 |
|
|
boolean removeAll(Collection c);
|
209 |
|
|
|
210 |
|
|
/**
|
211 |
|
|
* Retains only the elements in this set that are also in the specified
|
212 |
|
|
* collection (optional operation). If the argument is also a set, this
|
213 |
|
|
* performs the <i>intersection</i> of the two sets.
|
214 |
|
|
*
|
215 |
|
|
* @param c the collection to keep
|
216 |
|
|
* @return true if this set was modified
|
217 |
|
|
* @throws UnsupportedOperationException if this operation is not allowed
|
218 |
|
|
* @throws NullPointerException if c is null
|
219 |
|
|
* @throws ClassCastException if the type of any element in c is not
|
220 |
|
|
* a valid type for this set.
|
221 |
|
|
* @throws NullPointerException if some element of c is null and this
|
222 |
|
|
* set doesn't support retaining null values.
|
223 |
|
|
* @see #remove(Object)
|
224 |
|
|
*/
|
225 |
|
|
boolean retainAll(Collection c);
|
226 |
|
|
|
227 |
|
|
/**
|
228 |
|
|
* Returns the number of elements in the set. If there are more
|
229 |
|
|
* than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE. This is
|
230 |
|
|
* the <i>cardinality</i> of the set.
|
231 |
|
|
*
|
232 |
|
|
* @return the number of elements
|
233 |
|
|
*/
|
234 |
|
|
int size();
|
235 |
|
|
|
236 |
|
|
/**
|
237 |
|
|
* Returns an array containing the elements of this set. If the set
|
238 |
|
|
* makes a guarantee about iteration order, the array has the same
|
239 |
|
|
* order. The array is distinct from the set; modifying one does not
|
240 |
|
|
* affect the other.
|
241 |
|
|
*
|
242 |
|
|
* @return an array of this set's elements
|
243 |
|
|
* @see #toArray(Object[])
|
244 |
|
|
*/
|
245 |
|
|
Object[] toArray();
|
246 |
|
|
|
247 |
|
|
/**
|
248 |
|
|
* Returns an array containing the elements of this set, of the same runtime
|
249 |
|
|
* type of the argument. If the given set is large enough, it is reused,
|
250 |
|
|
* and null is inserted in the first unused slot. Otherwise, reflection
|
251 |
|
|
* is used to build a new array. If the set makes a guarantee about iteration
|
252 |
|
|
* order, the array has the same order. The array is distinct from the set;
|
253 |
|
|
* modifying one does not affect the other.
|
254 |
|
|
*
|
255 |
|
|
* @param a the array to determine the return type; if it is big enough
|
256 |
|
|
* it is used and returned
|
257 |
|
|
* @return an array holding the elements of the set
|
258 |
|
|
* @throws ArrayStoreException if the runtime type of a is not a supertype
|
259 |
|
|
* of all elements in the set
|
260 |
|
|
* @throws NullPointerException if a is null
|
261 |
|
|
* @see #toArray()
|
262 |
|
|
*/
|
263 |
|
|
Object[] toArray(Object[] a);
|
264 |
|
|
}
|