jtulach@1292
|
1 |
/*
|
jtulach@1292
|
2 |
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
|
jtulach@1292
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1292
|
4 |
*
|
jtulach@1292
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1292
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1292
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1292
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1292
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1292
|
10 |
*
|
jtulach@1292
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1292
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1292
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1292
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1292
|
15 |
* accompanied this code).
|
jtulach@1292
|
16 |
*
|
jtulach@1292
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1292
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1292
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1292
|
20 |
*
|
jtulach@1292
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1292
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1292
|
23 |
* questions.
|
jtulach@1292
|
24 |
*/
|
jtulach@1292
|
25 |
|
jtulach@1292
|
26 |
package java.util;
|
jtulach@1292
|
27 |
|
jtulach@1292
|
28 |
/**
|
jtulach@1292
|
29 |
* Private implementation class for EnumSet, for "regular sized" enum types
|
jtulach@1292
|
30 |
* (i.e., those with 64 or fewer enum constants).
|
jtulach@1292
|
31 |
*
|
jtulach@1292
|
32 |
* @author Josh Bloch
|
jtulach@1292
|
33 |
* @since 1.5
|
jtulach@1292
|
34 |
* @serial exclude
|
jtulach@1292
|
35 |
*/
|
jtulach@1292
|
36 |
class RegularEnumSet<E extends Enum<E>> extends EnumSet<E> {
|
jtulach@1292
|
37 |
private static final long serialVersionUID = 3411599620347842686L;
|
jtulach@1292
|
38 |
/**
|
jtulach@1292
|
39 |
* Bit vector representation of this set. The 2^k bit indicates the
|
jtulach@1292
|
40 |
* presence of universe[k] in this set.
|
jtulach@1292
|
41 |
*/
|
jtulach@1292
|
42 |
private long elements = 0L;
|
jtulach@1292
|
43 |
|
jtulach@1292
|
44 |
RegularEnumSet(Class<E>elementType, Enum[] universe) {
|
jtulach@1292
|
45 |
super(elementType, universe);
|
jtulach@1292
|
46 |
}
|
jtulach@1292
|
47 |
|
jtulach@1292
|
48 |
void addRange(E from, E to) {
|
jtulach@1292
|
49 |
elements = (-1L >>> (from.ordinal() - to.ordinal() - 1)) << from.ordinal();
|
jtulach@1292
|
50 |
}
|
jtulach@1292
|
51 |
|
jtulach@1292
|
52 |
void addAll() {
|
jtulach@1292
|
53 |
if (universe.length != 0)
|
jtulach@1292
|
54 |
elements = -1L >>> -universe.length;
|
jtulach@1292
|
55 |
}
|
jtulach@1292
|
56 |
|
jtulach@1292
|
57 |
void complement() {
|
jtulach@1292
|
58 |
if (universe.length != 0) {
|
jtulach@1292
|
59 |
elements = ~elements;
|
jtulach@1292
|
60 |
elements &= -1L >>> -universe.length; // Mask unused bits
|
jtulach@1292
|
61 |
}
|
jtulach@1292
|
62 |
}
|
jtulach@1292
|
63 |
|
jtulach@1292
|
64 |
/**
|
jtulach@1292
|
65 |
* Returns an iterator over the elements contained in this set. The
|
jtulach@1292
|
66 |
* iterator traverses the elements in their <i>natural order</i> (which is
|
jtulach@1292
|
67 |
* the order in which the enum constants are declared). The returned
|
jtulach@1292
|
68 |
* Iterator is a "snapshot" iterator that will never throw {@link
|
jtulach@1292
|
69 |
* ConcurrentModificationException}; the elements are traversed as they
|
jtulach@1292
|
70 |
* existed when this call was invoked.
|
jtulach@1292
|
71 |
*
|
jtulach@1292
|
72 |
* @return an iterator over the elements contained in this set
|
jtulach@1292
|
73 |
*/
|
jtulach@1292
|
74 |
public Iterator<E> iterator() {
|
jtulach@1292
|
75 |
return new EnumSetIterator<>();
|
jtulach@1292
|
76 |
}
|
jtulach@1292
|
77 |
|
jtulach@1292
|
78 |
private class EnumSetIterator<E extends Enum<E>> implements Iterator<E> {
|
jtulach@1292
|
79 |
/**
|
jtulach@1292
|
80 |
* A bit vector representing the elements in the set not yet
|
jtulach@1292
|
81 |
* returned by this iterator.
|
jtulach@1292
|
82 |
*/
|
jtulach@1292
|
83 |
long unseen;
|
jtulach@1292
|
84 |
|
jtulach@1292
|
85 |
/**
|
jtulach@1292
|
86 |
* The bit representing the last element returned by this iterator
|
jtulach@1292
|
87 |
* but not removed, or zero if no such element exists.
|
jtulach@1292
|
88 |
*/
|
jtulach@1292
|
89 |
long lastReturned = 0;
|
jtulach@1292
|
90 |
|
jtulach@1292
|
91 |
EnumSetIterator() {
|
jtulach@1292
|
92 |
unseen = elements;
|
jtulach@1292
|
93 |
}
|
jtulach@1292
|
94 |
|
jtulach@1292
|
95 |
public boolean hasNext() {
|
jtulach@1292
|
96 |
return unseen != 0;
|
jtulach@1292
|
97 |
}
|
jtulach@1292
|
98 |
|
jtulach@1292
|
99 |
public E next() {
|
jtulach@1292
|
100 |
if (unseen == 0)
|
jtulach@1292
|
101 |
throw new NoSuchElementException();
|
jtulach@1292
|
102 |
lastReturned = unseen & -unseen;
|
jtulach@1292
|
103 |
unseen -= lastReturned;
|
jtulach@1292
|
104 |
return (E) universe[Long.numberOfTrailingZeros(lastReturned)];
|
jtulach@1292
|
105 |
}
|
jtulach@1292
|
106 |
|
jtulach@1292
|
107 |
public void remove() {
|
jtulach@1292
|
108 |
if (lastReturned == 0)
|
jtulach@1292
|
109 |
throw new IllegalStateException();
|
jtulach@1292
|
110 |
elements &= ~lastReturned;
|
jtulach@1292
|
111 |
lastReturned = 0;
|
jtulach@1292
|
112 |
}
|
jtulach@1292
|
113 |
}
|
jtulach@1292
|
114 |
|
jtulach@1292
|
115 |
/**
|
jtulach@1292
|
116 |
* Returns the number of elements in this set.
|
jtulach@1292
|
117 |
*
|
jtulach@1292
|
118 |
* @return the number of elements in this set
|
jtulach@1292
|
119 |
*/
|
jtulach@1292
|
120 |
public int size() {
|
jtulach@1292
|
121 |
return Long.bitCount(elements);
|
jtulach@1292
|
122 |
}
|
jtulach@1292
|
123 |
|
jtulach@1292
|
124 |
/**
|
jtulach@1292
|
125 |
* Returns <tt>true</tt> if this set contains no elements.
|
jtulach@1292
|
126 |
*
|
jtulach@1292
|
127 |
* @return <tt>true</tt> if this set contains no elements
|
jtulach@1292
|
128 |
*/
|
jtulach@1292
|
129 |
public boolean isEmpty() {
|
jtulach@1292
|
130 |
return elements == 0;
|
jtulach@1292
|
131 |
}
|
jtulach@1292
|
132 |
|
jtulach@1292
|
133 |
/**
|
jtulach@1292
|
134 |
* Returns <tt>true</tt> if this set contains the specified element.
|
jtulach@1292
|
135 |
*
|
jtulach@1292
|
136 |
* @param e element to be checked for containment in this collection
|
jtulach@1292
|
137 |
* @return <tt>true</tt> if this set contains the specified element
|
jtulach@1292
|
138 |
*/
|
jtulach@1292
|
139 |
public boolean contains(Object e) {
|
jtulach@1292
|
140 |
if (e == null)
|
jtulach@1292
|
141 |
return false;
|
jtulach@1292
|
142 |
Class eClass = e.getClass();
|
jtulach@1292
|
143 |
if (eClass != elementType && eClass.getSuperclass() != elementType)
|
jtulach@1292
|
144 |
return false;
|
jtulach@1292
|
145 |
|
jtulach@1292
|
146 |
return (elements & (1L << ((Enum)e).ordinal())) != 0;
|
jtulach@1292
|
147 |
}
|
jtulach@1292
|
148 |
|
jtulach@1292
|
149 |
// Modification Operations
|
jtulach@1292
|
150 |
|
jtulach@1292
|
151 |
/**
|
jtulach@1292
|
152 |
* Adds the specified element to this set if it is not already present.
|
jtulach@1292
|
153 |
*
|
jtulach@1292
|
154 |
* @param e element to be added to this set
|
jtulach@1292
|
155 |
* @return <tt>true</tt> if the set changed as a result of the call
|
jtulach@1292
|
156 |
*
|
jtulach@1292
|
157 |
* @throws NullPointerException if <tt>e</tt> is null
|
jtulach@1292
|
158 |
*/
|
jtulach@1292
|
159 |
public boolean add(E e) {
|
jtulach@1292
|
160 |
typeCheck(e);
|
jtulach@1292
|
161 |
|
jtulach@1292
|
162 |
long oldElements = elements;
|
jtulach@1292
|
163 |
elements |= (1L << ((Enum)e).ordinal());
|
jtulach@1292
|
164 |
return elements != oldElements;
|
jtulach@1292
|
165 |
}
|
jtulach@1292
|
166 |
|
jtulach@1292
|
167 |
/**
|
jtulach@1292
|
168 |
* Removes the specified element from this set if it is present.
|
jtulach@1292
|
169 |
*
|
jtulach@1292
|
170 |
* @param e element to be removed from this set, if present
|
jtulach@1292
|
171 |
* @return <tt>true</tt> if the set contained the specified element
|
jtulach@1292
|
172 |
*/
|
jtulach@1292
|
173 |
public boolean remove(Object e) {
|
jtulach@1292
|
174 |
if (e == null)
|
jtulach@1292
|
175 |
return false;
|
jtulach@1292
|
176 |
Class eClass = e.getClass();
|
jtulach@1292
|
177 |
if (eClass != elementType && eClass.getSuperclass() != elementType)
|
jtulach@1292
|
178 |
return false;
|
jtulach@1292
|
179 |
|
jtulach@1292
|
180 |
long oldElements = elements;
|
jtulach@1292
|
181 |
elements &= ~(1L << ((Enum)e).ordinal());
|
jtulach@1292
|
182 |
return elements != oldElements;
|
jtulach@1292
|
183 |
}
|
jtulach@1292
|
184 |
|
jtulach@1292
|
185 |
// Bulk Operations
|
jtulach@1292
|
186 |
|
jtulach@1292
|
187 |
/**
|
jtulach@1292
|
188 |
* Returns <tt>true</tt> if this set contains all of the elements
|
jtulach@1292
|
189 |
* in the specified collection.
|
jtulach@1292
|
190 |
*
|
jtulach@1292
|
191 |
* @param c collection to be checked for containment in this set
|
jtulach@1292
|
192 |
* @return <tt>true</tt> if this set contains all of the elements
|
jtulach@1292
|
193 |
* in the specified collection
|
jtulach@1292
|
194 |
* @throws NullPointerException if the specified collection is null
|
jtulach@1292
|
195 |
*/
|
jtulach@1292
|
196 |
public boolean containsAll(Collection<?> c) {
|
jtulach@1292
|
197 |
if (!(c instanceof RegularEnumSet))
|
jtulach@1292
|
198 |
return super.containsAll(c);
|
jtulach@1292
|
199 |
|
jtulach@1292
|
200 |
RegularEnumSet es = (RegularEnumSet)c;
|
jtulach@1292
|
201 |
if (es.elementType != elementType)
|
jtulach@1292
|
202 |
return es.isEmpty();
|
jtulach@1292
|
203 |
|
jtulach@1292
|
204 |
return (es.elements & ~elements) == 0;
|
jtulach@1292
|
205 |
}
|
jtulach@1292
|
206 |
|
jtulach@1292
|
207 |
/**
|
jtulach@1292
|
208 |
* Adds all of the elements in the specified collection to this set.
|
jtulach@1292
|
209 |
*
|
jtulach@1292
|
210 |
* @param c collection whose elements are to be added to this set
|
jtulach@1292
|
211 |
* @return <tt>true</tt> if this set changed as a result of the call
|
jtulach@1292
|
212 |
* @throws NullPointerException if the specified collection or any
|
jtulach@1292
|
213 |
* of its elements are null
|
jtulach@1292
|
214 |
*/
|
jtulach@1292
|
215 |
public boolean addAll(Collection<? extends E> c) {
|
jtulach@1292
|
216 |
if (!(c instanceof RegularEnumSet))
|
jtulach@1292
|
217 |
return super.addAll(c);
|
jtulach@1292
|
218 |
|
jtulach@1292
|
219 |
RegularEnumSet es = (RegularEnumSet)c;
|
jtulach@1292
|
220 |
if (es.elementType != elementType) {
|
jtulach@1292
|
221 |
if (es.isEmpty())
|
jtulach@1292
|
222 |
return false;
|
jtulach@1292
|
223 |
else
|
jtulach@1292
|
224 |
throw new ClassCastException(
|
jtulach@1292
|
225 |
es.elementType + " != " + elementType);
|
jtulach@1292
|
226 |
}
|
jtulach@1292
|
227 |
|
jtulach@1292
|
228 |
long oldElements = elements;
|
jtulach@1292
|
229 |
elements |= es.elements;
|
jtulach@1292
|
230 |
return elements != oldElements;
|
jtulach@1292
|
231 |
}
|
jtulach@1292
|
232 |
|
jtulach@1292
|
233 |
/**
|
jtulach@1292
|
234 |
* Removes from this set all of its elements that are contained in
|
jtulach@1292
|
235 |
* the specified collection.
|
jtulach@1292
|
236 |
*
|
jtulach@1292
|
237 |
* @param c elements to be removed from this set
|
jtulach@1292
|
238 |
* @return <tt>true</tt> if this set changed as a result of the call
|
jtulach@1292
|
239 |
* @throws NullPointerException if the specified collection is null
|
jtulach@1292
|
240 |
*/
|
jtulach@1292
|
241 |
public boolean removeAll(Collection<?> c) {
|
jtulach@1292
|
242 |
if (!(c instanceof RegularEnumSet))
|
jtulach@1292
|
243 |
return super.removeAll(c);
|
jtulach@1292
|
244 |
|
jtulach@1292
|
245 |
RegularEnumSet es = (RegularEnumSet)c;
|
jtulach@1292
|
246 |
if (es.elementType != elementType)
|
jtulach@1292
|
247 |
return false;
|
jtulach@1292
|
248 |
|
jtulach@1292
|
249 |
long oldElements = elements;
|
jtulach@1292
|
250 |
elements &= ~es.elements;
|
jtulach@1292
|
251 |
return elements != oldElements;
|
jtulach@1292
|
252 |
}
|
jtulach@1292
|
253 |
|
jtulach@1292
|
254 |
/**
|
jtulach@1292
|
255 |
* Retains only the elements in this set that are contained in the
|
jtulach@1292
|
256 |
* specified collection.
|
jtulach@1292
|
257 |
*
|
jtulach@1292
|
258 |
* @param c elements to be retained in this set
|
jtulach@1292
|
259 |
* @return <tt>true</tt> if this set changed as a result of the call
|
jtulach@1292
|
260 |
* @throws NullPointerException if the specified collection is null
|
jtulach@1292
|
261 |
*/
|
jtulach@1292
|
262 |
public boolean retainAll(Collection<?> c) {
|
jtulach@1292
|
263 |
if (!(c instanceof RegularEnumSet))
|
jtulach@1292
|
264 |
return super.retainAll(c);
|
jtulach@1292
|
265 |
|
jtulach@1292
|
266 |
RegularEnumSet<?> es = (RegularEnumSet<?>)c;
|
jtulach@1292
|
267 |
if (es.elementType != elementType) {
|
jtulach@1292
|
268 |
boolean changed = (elements != 0);
|
jtulach@1292
|
269 |
elements = 0;
|
jtulach@1292
|
270 |
return changed;
|
jtulach@1292
|
271 |
}
|
jtulach@1292
|
272 |
|
jtulach@1292
|
273 |
long oldElements = elements;
|
jtulach@1292
|
274 |
elements &= es.elements;
|
jtulach@1292
|
275 |
return elements != oldElements;
|
jtulach@1292
|
276 |
}
|
jtulach@1292
|
277 |
|
jtulach@1292
|
278 |
/**
|
jtulach@1292
|
279 |
* Removes all of the elements from this set.
|
jtulach@1292
|
280 |
*/
|
jtulach@1292
|
281 |
public void clear() {
|
jtulach@1292
|
282 |
elements = 0;
|
jtulach@1292
|
283 |
}
|
jtulach@1292
|
284 |
|
jtulach@1292
|
285 |
/**
|
jtulach@1292
|
286 |
* Compares the specified object with this set for equality. Returns
|
jtulach@1292
|
287 |
* <tt>true</tt> if the given object is also a set, the two sets have
|
jtulach@1292
|
288 |
* the same size, and every member of the given set is contained in
|
jtulach@1292
|
289 |
* this set.
|
jtulach@1292
|
290 |
*
|
jtulach@1292
|
291 |
* @param e object to be compared for equality with this set
|
jtulach@1292
|
292 |
* @return <tt>true</tt> if the specified object is equal to this set
|
jtulach@1292
|
293 |
*/
|
jtulach@1292
|
294 |
public boolean equals(Object o) {
|
jtulach@1292
|
295 |
if (!(o instanceof RegularEnumSet))
|
jtulach@1292
|
296 |
return super.equals(o);
|
jtulach@1292
|
297 |
|
jtulach@1292
|
298 |
RegularEnumSet es = (RegularEnumSet)o;
|
jtulach@1292
|
299 |
if (es.elementType != elementType)
|
jtulach@1292
|
300 |
return elements == 0 && es.elements == 0;
|
jtulach@1292
|
301 |
return es.elements == elements;
|
jtulach@1292
|
302 |
}
|
jtulach@1292
|
303 |
}
|