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 "jumbo" enum types
|
jtulach@1292
|
30 |
* (i.e., those with more than 64 elements).
|
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 JumboEnumSet<E extends Enum<E>> extends EnumSet<E> {
|
jtulach@1292
|
37 |
private static final long serialVersionUID = 334349849919042784L;
|
jtulach@1292
|
38 |
|
jtulach@1292
|
39 |
/**
|
jtulach@1292
|
40 |
* Bit vector representation of this set. The ith bit of the jth
|
jtulach@1292
|
41 |
* element of this array represents the presence of universe[64*j +i]
|
jtulach@1292
|
42 |
* in this set.
|
jtulach@1292
|
43 |
*/
|
jtulach@1292
|
44 |
private long elements[];
|
jtulach@1292
|
45 |
|
jtulach@1292
|
46 |
// Redundant - maintained for performance
|
jtulach@1292
|
47 |
private int size = 0;
|
jtulach@1292
|
48 |
|
jtulach@1292
|
49 |
JumboEnumSet(Class<E>elementType, Enum[] universe) {
|
jtulach@1292
|
50 |
super(elementType, universe);
|
jtulach@1292
|
51 |
elements = new long[(universe.length + 63) >>> 6];
|
jtulach@1292
|
52 |
}
|
jtulach@1292
|
53 |
|
jtulach@1292
|
54 |
void addRange(E from, E to) {
|
jtulach@1292
|
55 |
int fromIndex = from.ordinal() >>> 6;
|
jtulach@1292
|
56 |
int toIndex = to.ordinal() >>> 6;
|
jtulach@1292
|
57 |
|
jtulach@1292
|
58 |
if (fromIndex == toIndex) {
|
jtulach@1292
|
59 |
elements[fromIndex] = (-1L >>> (from.ordinal() - to.ordinal() - 1))
|
jtulach@1292
|
60 |
<< from.ordinal();
|
jtulach@1292
|
61 |
} else {
|
jtulach@1292
|
62 |
elements[fromIndex] = (-1L << from.ordinal());
|
jtulach@1292
|
63 |
for (int i = fromIndex + 1; i < toIndex; i++)
|
jtulach@1292
|
64 |
elements[i] = -1;
|
jtulach@1292
|
65 |
elements[toIndex] = -1L >>> (63 - to.ordinal());
|
jtulach@1292
|
66 |
}
|
jtulach@1292
|
67 |
size = to.ordinal() - from.ordinal() + 1;
|
jtulach@1292
|
68 |
}
|
jtulach@1292
|
69 |
|
jtulach@1292
|
70 |
void addAll() {
|
jtulach@1292
|
71 |
for (int i = 0; i < elements.length; i++)
|
jtulach@1292
|
72 |
elements[i] = -1;
|
jtulach@1292
|
73 |
elements[elements.length - 1] >>>= -universe.length;
|
jtulach@1292
|
74 |
size = universe.length;
|
jtulach@1292
|
75 |
}
|
jtulach@1292
|
76 |
|
jtulach@1292
|
77 |
void complement() {
|
jtulach@1292
|
78 |
for (int i = 0; i < elements.length; i++)
|
jtulach@1292
|
79 |
elements[i] = ~elements[i];
|
jtulach@1292
|
80 |
elements[elements.length - 1] &= (-1L >>> -universe.length);
|
jtulach@1292
|
81 |
size = universe.length - size;
|
jtulach@1292
|
82 |
}
|
jtulach@1292
|
83 |
|
jtulach@1292
|
84 |
/**
|
jtulach@1292
|
85 |
* Returns an iterator over the elements contained in this set. The
|
jtulach@1292
|
86 |
* iterator traverses the elements in their <i>natural order</i> (which is
|
jtulach@1292
|
87 |
* the order in which the enum constants are declared). The returned
|
jtulach@1292
|
88 |
* Iterator is a "weakly consistent" iterator that will never throw {@link
|
jtulach@1292
|
89 |
* ConcurrentModificationException}.
|
jtulach@1292
|
90 |
*
|
jtulach@1292
|
91 |
* @return an iterator over the elements contained in this set
|
jtulach@1292
|
92 |
*/
|
jtulach@1292
|
93 |
public Iterator<E> iterator() {
|
jtulach@1292
|
94 |
return new EnumSetIterator<>();
|
jtulach@1292
|
95 |
}
|
jtulach@1292
|
96 |
|
jtulach@1292
|
97 |
private class EnumSetIterator<E extends Enum<E>> implements Iterator<E> {
|
jtulach@1292
|
98 |
/**
|
jtulach@1292
|
99 |
* A bit vector representing the elements in the current "word"
|
jtulach@1292
|
100 |
* of the set not yet returned by this iterator.
|
jtulach@1292
|
101 |
*/
|
jtulach@1292
|
102 |
long unseen;
|
jtulach@1292
|
103 |
|
jtulach@1292
|
104 |
/**
|
jtulach@1292
|
105 |
* The index corresponding to unseen in the elements array.
|
jtulach@1292
|
106 |
*/
|
jtulach@1292
|
107 |
int unseenIndex = 0;
|
jtulach@1292
|
108 |
|
jtulach@1292
|
109 |
/**
|
jtulach@1292
|
110 |
* The bit representing the last element returned by this iterator
|
jtulach@1292
|
111 |
* but not removed, or zero if no such element exists.
|
jtulach@1292
|
112 |
*/
|
jtulach@1292
|
113 |
long lastReturned = 0;
|
jtulach@1292
|
114 |
|
jtulach@1292
|
115 |
/**
|
jtulach@1292
|
116 |
* The index corresponding to lastReturned in the elements array.
|
jtulach@1292
|
117 |
*/
|
jtulach@1292
|
118 |
int lastReturnedIndex = 0;
|
jtulach@1292
|
119 |
|
jtulach@1292
|
120 |
EnumSetIterator() {
|
jtulach@1292
|
121 |
unseen = elements[0];
|
jtulach@1292
|
122 |
}
|
jtulach@1292
|
123 |
|
jtulach@1292
|
124 |
public boolean hasNext() {
|
jtulach@1292
|
125 |
while (unseen == 0 && unseenIndex < elements.length - 1)
|
jtulach@1292
|
126 |
unseen = elements[++unseenIndex];
|
jtulach@1292
|
127 |
return unseen != 0;
|
jtulach@1292
|
128 |
}
|
jtulach@1292
|
129 |
|
jtulach@1292
|
130 |
public E next() {
|
jtulach@1292
|
131 |
if (!hasNext())
|
jtulach@1292
|
132 |
throw new NoSuchElementException();
|
jtulach@1292
|
133 |
lastReturned = unseen & -unseen;
|
jtulach@1292
|
134 |
lastReturnedIndex = unseenIndex;
|
jtulach@1292
|
135 |
unseen -= lastReturned;
|
jtulach@1292
|
136 |
return (E) universe[(lastReturnedIndex << 6)
|
jtulach@1292
|
137 |
+ Long.numberOfTrailingZeros(lastReturned)];
|
jtulach@1292
|
138 |
}
|
jtulach@1292
|
139 |
|
jtulach@1292
|
140 |
public void remove() {
|
jtulach@1292
|
141 |
if (lastReturned == 0)
|
jtulach@1292
|
142 |
throw new IllegalStateException();
|
jtulach@1292
|
143 |
final long oldElements = elements[lastReturnedIndex];
|
jtulach@1292
|
144 |
elements[lastReturnedIndex] &= ~lastReturned;
|
jtulach@1292
|
145 |
if (oldElements != elements[lastReturnedIndex]) {
|
jtulach@1292
|
146 |
size--;
|
jtulach@1292
|
147 |
}
|
jtulach@1292
|
148 |
lastReturned = 0;
|
jtulach@1292
|
149 |
}
|
jtulach@1292
|
150 |
}
|
jtulach@1292
|
151 |
|
jtulach@1292
|
152 |
/**
|
jtulach@1292
|
153 |
* Returns the number of elements in this set.
|
jtulach@1292
|
154 |
*
|
jtulach@1292
|
155 |
* @return the number of elements in this set
|
jtulach@1292
|
156 |
*/
|
jtulach@1292
|
157 |
public int size() {
|
jtulach@1292
|
158 |
return size;
|
jtulach@1292
|
159 |
}
|
jtulach@1292
|
160 |
|
jtulach@1292
|
161 |
/**
|
jtulach@1292
|
162 |
* Returns <tt>true</tt> if this set contains no elements.
|
jtulach@1292
|
163 |
*
|
jtulach@1292
|
164 |
* @return <tt>true</tt> if this set contains no elements
|
jtulach@1292
|
165 |
*/
|
jtulach@1292
|
166 |
public boolean isEmpty() {
|
jtulach@1292
|
167 |
return size == 0;
|
jtulach@1292
|
168 |
}
|
jtulach@1292
|
169 |
|
jtulach@1292
|
170 |
/**
|
jtulach@1292
|
171 |
* Returns <tt>true</tt> if this set contains the specified element.
|
jtulach@1292
|
172 |
*
|
jtulach@1292
|
173 |
* @param e element to be checked for containment in this collection
|
jtulach@1292
|
174 |
* @return <tt>true</tt> if this set contains the specified element
|
jtulach@1292
|
175 |
*/
|
jtulach@1292
|
176 |
public boolean contains(Object e) {
|
jtulach@1292
|
177 |
if (e == null)
|
jtulach@1292
|
178 |
return false;
|
jtulach@1292
|
179 |
Class eClass = e.getClass();
|
jtulach@1292
|
180 |
if (eClass != elementType && eClass.getSuperclass() != elementType)
|
jtulach@1292
|
181 |
return false;
|
jtulach@1292
|
182 |
|
jtulach@1292
|
183 |
int eOrdinal = ((Enum)e).ordinal();
|
jtulach@1292
|
184 |
return (elements[eOrdinal >>> 6] & (1L << eOrdinal)) != 0;
|
jtulach@1292
|
185 |
}
|
jtulach@1292
|
186 |
|
jtulach@1292
|
187 |
// Modification Operations
|
jtulach@1292
|
188 |
|
jtulach@1292
|
189 |
/**
|
jtulach@1292
|
190 |
* Adds the specified element to this set if it is not already present.
|
jtulach@1292
|
191 |
*
|
jtulach@1292
|
192 |
* @param e element to be added to this set
|
jtulach@1292
|
193 |
* @return <tt>true</tt> if the set changed as a result of the call
|
jtulach@1292
|
194 |
*
|
jtulach@1292
|
195 |
* @throws NullPointerException if <tt>e</tt> is null
|
jtulach@1292
|
196 |
*/
|
jtulach@1292
|
197 |
public boolean add(E e) {
|
jtulach@1292
|
198 |
typeCheck(e);
|
jtulach@1292
|
199 |
|
jtulach@1292
|
200 |
int eOrdinal = e.ordinal();
|
jtulach@1292
|
201 |
int eWordNum = eOrdinal >>> 6;
|
jtulach@1292
|
202 |
|
jtulach@1292
|
203 |
long oldElements = elements[eWordNum];
|
jtulach@1292
|
204 |
elements[eWordNum] |= (1L << eOrdinal);
|
jtulach@1292
|
205 |
boolean result = (elements[eWordNum] != oldElements);
|
jtulach@1292
|
206 |
if (result)
|
jtulach@1292
|
207 |
size++;
|
jtulach@1292
|
208 |
return result;
|
jtulach@1292
|
209 |
}
|
jtulach@1292
|
210 |
|
jtulach@1292
|
211 |
/**
|
jtulach@1292
|
212 |
* Removes the specified element from this set if it is present.
|
jtulach@1292
|
213 |
*
|
jtulach@1292
|
214 |
* @param e element to be removed from this set, if present
|
jtulach@1292
|
215 |
* @return <tt>true</tt> if the set contained the specified element
|
jtulach@1292
|
216 |
*/
|
jtulach@1292
|
217 |
public boolean remove(Object e) {
|
jtulach@1292
|
218 |
if (e == null)
|
jtulach@1292
|
219 |
return false;
|
jtulach@1292
|
220 |
Class eClass = e.getClass();
|
jtulach@1292
|
221 |
if (eClass != elementType && eClass.getSuperclass() != elementType)
|
jtulach@1292
|
222 |
return false;
|
jtulach@1292
|
223 |
int eOrdinal = ((Enum)e).ordinal();
|
jtulach@1292
|
224 |
int eWordNum = eOrdinal >>> 6;
|
jtulach@1292
|
225 |
|
jtulach@1292
|
226 |
long oldElements = elements[eWordNum];
|
jtulach@1292
|
227 |
elements[eWordNum] &= ~(1L << eOrdinal);
|
jtulach@1292
|
228 |
boolean result = (elements[eWordNum] != oldElements);
|
jtulach@1292
|
229 |
if (result)
|
jtulach@1292
|
230 |
size--;
|
jtulach@1292
|
231 |
return result;
|
jtulach@1292
|
232 |
}
|
jtulach@1292
|
233 |
|
jtulach@1292
|
234 |
// Bulk Operations
|
jtulach@1292
|
235 |
|
jtulach@1292
|
236 |
/**
|
jtulach@1292
|
237 |
* Returns <tt>true</tt> if this set contains all of the elements
|
jtulach@1292
|
238 |
* in the specified collection.
|
jtulach@1292
|
239 |
*
|
jtulach@1292
|
240 |
* @param c collection to be checked for containment in this set
|
jtulach@1292
|
241 |
* @return <tt>true</tt> if this set contains all of the elements
|
jtulach@1292
|
242 |
* in the specified collection
|
jtulach@1292
|
243 |
* @throws NullPointerException if the specified collection is null
|
jtulach@1292
|
244 |
*/
|
jtulach@1292
|
245 |
public boolean containsAll(Collection<?> c) {
|
jtulach@1292
|
246 |
if (!(c instanceof JumboEnumSet))
|
jtulach@1292
|
247 |
return super.containsAll(c);
|
jtulach@1292
|
248 |
|
jtulach@1292
|
249 |
JumboEnumSet es = (JumboEnumSet)c;
|
jtulach@1292
|
250 |
if (es.elementType != elementType)
|
jtulach@1292
|
251 |
return es.isEmpty();
|
jtulach@1292
|
252 |
|
jtulach@1292
|
253 |
for (int i = 0; i < elements.length; i++)
|
jtulach@1292
|
254 |
if ((es.elements[i] & ~elements[i]) != 0)
|
jtulach@1292
|
255 |
return false;
|
jtulach@1292
|
256 |
return true;
|
jtulach@1292
|
257 |
}
|
jtulach@1292
|
258 |
|
jtulach@1292
|
259 |
/**
|
jtulach@1292
|
260 |
* Adds all of the elements in the specified collection to this set.
|
jtulach@1292
|
261 |
*
|
jtulach@1292
|
262 |
* @param c collection whose elements are to be added to this set
|
jtulach@1292
|
263 |
* @return <tt>true</tt> if this set changed as a result of the call
|
jtulach@1292
|
264 |
* @throws NullPointerException if the specified collection or any of
|
jtulach@1292
|
265 |
* its elements are null
|
jtulach@1292
|
266 |
*/
|
jtulach@1292
|
267 |
public boolean addAll(Collection<? extends E> c) {
|
jtulach@1292
|
268 |
if (!(c instanceof JumboEnumSet))
|
jtulach@1292
|
269 |
return super.addAll(c);
|
jtulach@1292
|
270 |
|
jtulach@1292
|
271 |
JumboEnumSet es = (JumboEnumSet)c;
|
jtulach@1292
|
272 |
if (es.elementType != elementType) {
|
jtulach@1292
|
273 |
if (es.isEmpty())
|
jtulach@1292
|
274 |
return false;
|
jtulach@1292
|
275 |
else
|
jtulach@1292
|
276 |
throw new ClassCastException(
|
jtulach@1292
|
277 |
es.elementType + " != " + elementType);
|
jtulach@1292
|
278 |
}
|
jtulach@1292
|
279 |
|
jtulach@1292
|
280 |
for (int i = 0; i < elements.length; i++)
|
jtulach@1292
|
281 |
elements[i] |= es.elements[i];
|
jtulach@1292
|
282 |
return recalculateSize();
|
jtulach@1292
|
283 |
}
|
jtulach@1292
|
284 |
|
jtulach@1292
|
285 |
/**
|
jtulach@1292
|
286 |
* Removes from this set all of its elements that are contained in
|
jtulach@1292
|
287 |
* the specified collection.
|
jtulach@1292
|
288 |
*
|
jtulach@1292
|
289 |
* @param c elements to be removed from this set
|
jtulach@1292
|
290 |
* @return <tt>true</tt> if this set changed as a result of the call
|
jtulach@1292
|
291 |
* @throws NullPointerException if the specified collection is null
|
jtulach@1292
|
292 |
*/
|
jtulach@1292
|
293 |
public boolean removeAll(Collection<?> c) {
|
jtulach@1292
|
294 |
if (!(c instanceof JumboEnumSet))
|
jtulach@1292
|
295 |
return super.removeAll(c);
|
jtulach@1292
|
296 |
|
jtulach@1292
|
297 |
JumboEnumSet es = (JumboEnumSet)c;
|
jtulach@1292
|
298 |
if (es.elementType != elementType)
|
jtulach@1292
|
299 |
return false;
|
jtulach@1292
|
300 |
|
jtulach@1292
|
301 |
for (int i = 0; i < elements.length; i++)
|
jtulach@1292
|
302 |
elements[i] &= ~es.elements[i];
|
jtulach@1292
|
303 |
return recalculateSize();
|
jtulach@1292
|
304 |
}
|
jtulach@1292
|
305 |
|
jtulach@1292
|
306 |
/**
|
jtulach@1292
|
307 |
* Retains only the elements in this set that are contained in the
|
jtulach@1292
|
308 |
* specified collection.
|
jtulach@1292
|
309 |
*
|
jtulach@1292
|
310 |
* @param c elements to be retained in this set
|
jtulach@1292
|
311 |
* @return <tt>true</tt> if this set changed as a result of the call
|
jtulach@1292
|
312 |
* @throws NullPointerException if the specified collection is null
|
jtulach@1292
|
313 |
*/
|
jtulach@1292
|
314 |
public boolean retainAll(Collection<?> c) {
|
jtulach@1292
|
315 |
if (!(c instanceof JumboEnumSet))
|
jtulach@1292
|
316 |
return super.retainAll(c);
|
jtulach@1292
|
317 |
|
jtulach@1292
|
318 |
JumboEnumSet<?> es = (JumboEnumSet<?>)c;
|
jtulach@1292
|
319 |
if (es.elementType != elementType) {
|
jtulach@1292
|
320 |
boolean changed = (size != 0);
|
jtulach@1292
|
321 |
clear();
|
jtulach@1292
|
322 |
return changed;
|
jtulach@1292
|
323 |
}
|
jtulach@1292
|
324 |
|
jtulach@1292
|
325 |
for (int i = 0; i < elements.length; i++)
|
jtulach@1292
|
326 |
elements[i] &= es.elements[i];
|
jtulach@1292
|
327 |
return recalculateSize();
|
jtulach@1292
|
328 |
}
|
jtulach@1292
|
329 |
|
jtulach@1292
|
330 |
/**
|
jtulach@1292
|
331 |
* Removes all of the elements from this set.
|
jtulach@1292
|
332 |
*/
|
jtulach@1292
|
333 |
public void clear() {
|
jtulach@1292
|
334 |
Arrays.fill(elements, 0);
|
jtulach@1292
|
335 |
size = 0;
|
jtulach@1292
|
336 |
}
|
jtulach@1292
|
337 |
|
jtulach@1292
|
338 |
/**
|
jtulach@1292
|
339 |
* Compares the specified object with this set for equality. Returns
|
jtulach@1292
|
340 |
* <tt>true</tt> if the given object is also a set, the two sets have
|
jtulach@1292
|
341 |
* the same size, and every member of the given set is contained in
|
jtulach@1292
|
342 |
* this set.
|
jtulach@1292
|
343 |
*
|
jtulach@1292
|
344 |
* @param e object to be compared for equality with this set
|
jtulach@1292
|
345 |
* @return <tt>true</tt> if the specified object is equal to this set
|
jtulach@1292
|
346 |
*/
|
jtulach@1292
|
347 |
public boolean equals(Object o) {
|
jtulach@1292
|
348 |
if (!(o instanceof JumboEnumSet))
|
jtulach@1292
|
349 |
return super.equals(o);
|
jtulach@1292
|
350 |
|
jtulach@1292
|
351 |
JumboEnumSet es = (JumboEnumSet)o;
|
jtulach@1292
|
352 |
if (es.elementType != elementType)
|
jtulach@1292
|
353 |
return size == 0 && es.size == 0;
|
jtulach@1292
|
354 |
|
jtulach@1292
|
355 |
return Arrays.equals(es.elements, elements);
|
jtulach@1292
|
356 |
}
|
jtulach@1292
|
357 |
|
jtulach@1292
|
358 |
/**
|
jtulach@1292
|
359 |
* Recalculates the size of the set. Returns true if it's changed.
|
jtulach@1292
|
360 |
*/
|
jtulach@1292
|
361 |
private boolean recalculateSize() {
|
jtulach@1292
|
362 |
int oldSize = size;
|
jtulach@1292
|
363 |
size = 0;
|
jtulach@1292
|
364 |
for (long elt : elements)
|
jtulach@1292
|
365 |
size += Long.bitCount(elt);
|
jtulach@1292
|
366 |
|
jtulach@1292
|
367 |
return size != oldSize;
|
jtulach@1292
|
368 |
}
|
jtulach@1292
|
369 |
|
jtulach@1292
|
370 |
public EnumSet<E> clone() {
|
jtulach@1292
|
371 |
JumboEnumSet<E> result = (JumboEnumSet<E>) super.clone();
|
jtulach@1292
|
372 |
result.elements = result.elements.clone();
|
jtulach@1292
|
373 |
return result;
|
jtulach@1292
|
374 |
}
|
jtulach@1292
|
375 |
}
|