2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
29 * Private implementation class for EnumSet, for "jumbo" enum types
30 * (i.e., those with more than 64 elements).
36 class JumboEnumSet<E extends Enum<E>> extends EnumSet<E> {
37 private static final long serialVersionUID = 334349849919042784L;
40 * Bit vector representation of this set. The ith bit of the jth
41 * element of this array represents the presence of universe[64*j +i]
44 private long elements[];
46 // Redundant - maintained for performance
49 JumboEnumSet(Class<E>elementType, Enum[] universe) {
50 super(elementType, universe);
51 elements = new long[(universe.length + 63) >>> 6];
54 void addRange(E from, E to) {
55 int fromIndex = from.ordinal() >>> 6;
56 int toIndex = to.ordinal() >>> 6;
58 if (fromIndex == toIndex) {
59 elements[fromIndex] = (-1L >>> (from.ordinal() - to.ordinal() - 1))
62 elements[fromIndex] = (-1L << from.ordinal());
63 for (int i = fromIndex + 1; i < toIndex; i++)
65 elements[toIndex] = -1L >>> (63 - to.ordinal());
67 size = to.ordinal() - from.ordinal() + 1;
71 for (int i = 0; i < elements.length; i++)
73 elements[elements.length - 1] >>>= -universe.length;
74 size = universe.length;
78 for (int i = 0; i < elements.length; i++)
79 elements[i] = ~elements[i];
80 elements[elements.length - 1] &= (-1L >>> -universe.length);
81 size = universe.length - size;
85 * Returns an iterator over the elements contained in this set. The
86 * iterator traverses the elements in their <i>natural order</i> (which is
87 * the order in which the enum constants are declared). The returned
88 * Iterator is a "weakly consistent" iterator that will never throw {@link
89 * ConcurrentModificationException}.
91 * @return an iterator over the elements contained in this set
93 public Iterator<E> iterator() {
94 return new EnumSetIterator<>();
97 private class EnumSetIterator<E extends Enum<E>> implements Iterator<E> {
99 * A bit vector representing the elements in the current "word"
100 * of the set not yet returned by this iterator.
105 * The index corresponding to unseen in the elements array.
110 * The bit representing the last element returned by this iterator
111 * but not removed, or zero if no such element exists.
113 long lastReturned = 0;
116 * The index corresponding to lastReturned in the elements array.
118 int lastReturnedIndex = 0;
121 unseen = elements[0];
124 public boolean hasNext() {
125 while (unseen == 0 && unseenIndex < elements.length - 1)
126 unseen = elements[++unseenIndex];
132 throw new NoSuchElementException();
133 lastReturned = unseen & -unseen;
134 lastReturnedIndex = unseenIndex;
135 unseen -= lastReturned;
136 return (E) universe[(lastReturnedIndex << 6)
137 + Long.numberOfTrailingZeros(lastReturned)];
140 public void remove() {
141 if (lastReturned == 0)
142 throw new IllegalStateException();
143 final long oldElements = elements[lastReturnedIndex];
144 elements[lastReturnedIndex] &= ~lastReturned;
145 if (oldElements != elements[lastReturnedIndex]) {
153 * Returns the number of elements in this set.
155 * @return the number of elements in this set
162 * Returns <tt>true</tt> if this set contains no elements.
164 * @return <tt>true</tt> if this set contains no elements
166 public boolean isEmpty() {
171 * Returns <tt>true</tt> if this set contains the specified element.
173 * @param e element to be checked for containment in this collection
174 * @return <tt>true</tt> if this set contains the specified element
176 public boolean contains(Object e) {
179 Class eClass = e.getClass();
180 if (eClass != elementType && eClass.getSuperclass() != elementType)
183 int eOrdinal = ((Enum)e).ordinal();
184 return (elements[eOrdinal >>> 6] & (1L << eOrdinal)) != 0;
187 // Modification Operations
190 * Adds the specified element to this set if it is not already present.
192 * @param e element to be added to this set
193 * @return <tt>true</tt> if the set changed as a result of the call
195 * @throws NullPointerException if <tt>e</tt> is null
197 public boolean add(E e) {
200 int eOrdinal = e.ordinal();
201 int eWordNum = eOrdinal >>> 6;
203 long oldElements = elements[eWordNum];
204 elements[eWordNum] |= (1L << eOrdinal);
205 boolean result = (elements[eWordNum] != oldElements);
212 * Removes the specified element from this set if it is present.
214 * @param e element to be removed from this set, if present
215 * @return <tt>true</tt> if the set contained the specified element
217 public boolean remove(Object e) {
220 Class eClass = e.getClass();
221 if (eClass != elementType && eClass.getSuperclass() != elementType)
223 int eOrdinal = ((Enum)e).ordinal();
224 int eWordNum = eOrdinal >>> 6;
226 long oldElements = elements[eWordNum];
227 elements[eWordNum] &= ~(1L << eOrdinal);
228 boolean result = (elements[eWordNum] != oldElements);
237 * Returns <tt>true</tt> if this set contains all of the elements
238 * in the specified collection.
240 * @param c collection to be checked for containment in this set
241 * @return <tt>true</tt> if this set contains all of the elements
242 * in the specified collection
243 * @throws NullPointerException if the specified collection is null
245 public boolean containsAll(Collection<?> c) {
246 if (!(c instanceof JumboEnumSet))
247 return super.containsAll(c);
249 JumboEnumSet es = (JumboEnumSet)c;
250 if (es.elementType != elementType)
253 for (int i = 0; i < elements.length; i++)
254 if ((es.elements[i] & ~elements[i]) != 0)
260 * Adds all of the elements in the specified collection to this set.
262 * @param c collection whose elements are to be added to this set
263 * @return <tt>true</tt> if this set changed as a result of the call
264 * @throws NullPointerException if the specified collection or any of
265 * its elements are null
267 public boolean addAll(Collection<? extends E> c) {
268 if (!(c instanceof JumboEnumSet))
269 return super.addAll(c);
271 JumboEnumSet es = (JumboEnumSet)c;
272 if (es.elementType != elementType) {
276 throw new ClassCastException(
277 es.elementType + " != " + elementType);
280 for (int i = 0; i < elements.length; i++)
281 elements[i] |= es.elements[i];
282 return recalculateSize();
286 * Removes from this set all of its elements that are contained in
287 * the specified collection.
289 * @param c elements to be removed from this set
290 * @return <tt>true</tt> if this set changed as a result of the call
291 * @throws NullPointerException if the specified collection is null
293 public boolean removeAll(Collection<?> c) {
294 if (!(c instanceof JumboEnumSet))
295 return super.removeAll(c);
297 JumboEnumSet es = (JumboEnumSet)c;
298 if (es.elementType != elementType)
301 for (int i = 0; i < elements.length; i++)
302 elements[i] &= ~es.elements[i];
303 return recalculateSize();
307 * Retains only the elements in this set that are contained in the
308 * specified collection.
310 * @param c elements to be retained in this set
311 * @return <tt>true</tt> if this set changed as a result of the call
312 * @throws NullPointerException if the specified collection is null
314 public boolean retainAll(Collection<?> c) {
315 if (!(c instanceof JumboEnumSet))
316 return super.retainAll(c);
318 JumboEnumSet<?> es = (JumboEnumSet<?>)c;
319 if (es.elementType != elementType) {
320 boolean changed = (size != 0);
325 for (int i = 0; i < elements.length; i++)
326 elements[i] &= es.elements[i];
327 return recalculateSize();
331 * Removes all of the elements from this set.
333 public void clear() {
334 Arrays.fill(elements, 0);
339 * Compares the specified object with this set for equality. Returns
340 * <tt>true</tt> if the given object is also a set, the two sets have
341 * the same size, and every member of the given set is contained in
344 * @param e object to be compared for equality with this set
345 * @return <tt>true</tt> if the specified object is equal to this set
347 public boolean equals(Object o) {
348 if (!(o instanceof JumboEnumSet))
349 return super.equals(o);
351 JumboEnumSet es = (JumboEnumSet)o;
352 if (es.elementType != elementType)
353 return size == 0 && es.size == 0;
355 return Arrays.equals(es.elements, elements);
359 * Recalculates the size of the set. Returns true if it's changed.
361 private boolean recalculateSize() {
364 for (long elt : elements)
365 size += Long.bitCount(elt);
367 return size != oldSize;
370 public EnumSet<E> clone() {
371 JumboEnumSet<E> result = (JumboEnumSet<E>) super.clone();
372 result.elements = result.elements.clone();