jtulach@1292: /* jtulach@1292: * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. jtulach@1292: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1292: * jtulach@1292: * This code is free software; you can redistribute it and/or modify it jtulach@1292: * under the terms of the GNU General Public License version 2 only, as jtulach@1292: * published by the Free Software Foundation. Oracle designates this jtulach@1292: * particular file as subject to the "Classpath" exception as provided jtulach@1292: * by Oracle in the LICENSE file that accompanied this code. jtulach@1292: * jtulach@1292: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1292: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1292: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1292: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1292: * accompanied this code). jtulach@1292: * jtulach@1292: * You should have received a copy of the GNU General Public License version jtulach@1292: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1292: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1292: * jtulach@1292: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1292: * or visit www.oracle.com if you need additional information or have any jtulach@1292: * questions. jtulach@1292: */ jtulach@1292: jtulach@1292: package java.util; jtulach@1292: jtulach@1292: /** jtulach@1292: * Private implementation class for EnumSet, for "jumbo" enum types jtulach@1292: * (i.e., those with more than 64 elements). jtulach@1292: * jtulach@1292: * @author Josh Bloch jtulach@1292: * @since 1.5 jtulach@1292: * @serial exclude jtulach@1292: */ jtulach@1292: class JumboEnumSet> extends EnumSet { jtulach@1292: private static final long serialVersionUID = 334349849919042784L; jtulach@1292: jtulach@1292: /** jtulach@1292: * Bit vector representation of this set. The ith bit of the jth jtulach@1292: * element of this array represents the presence of universe[64*j +i] jtulach@1292: * in this set. jtulach@1292: */ jtulach@1292: private long elements[]; jtulach@1292: jtulach@1292: // Redundant - maintained for performance jtulach@1292: private int size = 0; jtulach@1292: jtulach@1292: JumboEnumSet(ClasselementType, Enum[] universe) { jtulach@1292: super(elementType, universe); jtulach@1292: elements = new long[(universe.length + 63) >>> 6]; jtulach@1292: } jtulach@1292: jtulach@1292: void addRange(E from, E to) { jtulach@1292: int fromIndex = from.ordinal() >>> 6; jtulach@1292: int toIndex = to.ordinal() >>> 6; jtulach@1292: jtulach@1292: if (fromIndex == toIndex) { jtulach@1292: elements[fromIndex] = (-1L >>> (from.ordinal() - to.ordinal() - 1)) jtulach@1292: << from.ordinal(); jtulach@1292: } else { jtulach@1292: elements[fromIndex] = (-1L << from.ordinal()); jtulach@1292: for (int i = fromIndex + 1; i < toIndex; i++) jtulach@1292: elements[i] = -1; jtulach@1292: elements[toIndex] = -1L >>> (63 - to.ordinal()); jtulach@1292: } jtulach@1292: size = to.ordinal() - from.ordinal() + 1; jtulach@1292: } jtulach@1292: jtulach@1292: void addAll() { jtulach@1292: for (int i = 0; i < elements.length; i++) jtulach@1292: elements[i] = -1; jtulach@1292: elements[elements.length - 1] >>>= -universe.length; jtulach@1292: size = universe.length; jtulach@1292: } jtulach@1292: jtulach@1292: void complement() { jtulach@1292: for (int i = 0; i < elements.length; i++) jtulach@1292: elements[i] = ~elements[i]; jtulach@1292: elements[elements.length - 1] &= (-1L >>> -universe.length); jtulach@1292: size = universe.length - size; jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Returns an iterator over the elements contained in this set. The jtulach@1292: * iterator traverses the elements in their natural order (which is jtulach@1292: * the order in which the enum constants are declared). The returned jtulach@1292: * Iterator is a "weakly consistent" iterator that will never throw {@link jtulach@1292: * ConcurrentModificationException}. jtulach@1292: * jtulach@1292: * @return an iterator over the elements contained in this set jtulach@1292: */ jtulach@1292: public Iterator iterator() { jtulach@1292: return new EnumSetIterator<>(); jtulach@1292: } jtulach@1292: jtulach@1292: private class EnumSetIterator> implements Iterator { jtulach@1292: /** jtulach@1292: * A bit vector representing the elements in the current "word" jtulach@1292: * of the set not yet returned by this iterator. jtulach@1292: */ jtulach@1292: long unseen; jtulach@1292: jtulach@1292: /** jtulach@1292: * The index corresponding to unseen in the elements array. jtulach@1292: */ jtulach@1292: int unseenIndex = 0; jtulach@1292: jtulach@1292: /** jtulach@1292: * The bit representing the last element returned by this iterator jtulach@1292: * but not removed, or zero if no such element exists. jtulach@1292: */ jtulach@1292: long lastReturned = 0; jtulach@1292: jtulach@1292: /** jtulach@1292: * The index corresponding to lastReturned in the elements array. jtulach@1292: */ jtulach@1292: int lastReturnedIndex = 0; jtulach@1292: jtulach@1292: EnumSetIterator() { jtulach@1292: unseen = elements[0]; jtulach@1292: } jtulach@1292: jtulach@1292: public boolean hasNext() { jtulach@1292: while (unseen == 0 && unseenIndex < elements.length - 1) jtulach@1292: unseen = elements[++unseenIndex]; jtulach@1292: return unseen != 0; jtulach@1292: } jtulach@1292: jtulach@1292: public E next() { jtulach@1292: if (!hasNext()) jtulach@1292: throw new NoSuchElementException(); jtulach@1292: lastReturned = unseen & -unseen; jtulach@1292: lastReturnedIndex = unseenIndex; jtulach@1292: unseen -= lastReturned; jtulach@1292: return (E) universe[(lastReturnedIndex << 6) jtulach@1292: + Long.numberOfTrailingZeros(lastReturned)]; jtulach@1292: } jtulach@1292: jtulach@1292: public void remove() { jtulach@1292: if (lastReturned == 0) jtulach@1292: throw new IllegalStateException(); jtulach@1292: final long oldElements = elements[lastReturnedIndex]; jtulach@1292: elements[lastReturnedIndex] &= ~lastReturned; jtulach@1292: if (oldElements != elements[lastReturnedIndex]) { jtulach@1292: size--; jtulach@1292: } jtulach@1292: lastReturned = 0; jtulach@1292: } jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Returns the number of elements in this set. jtulach@1292: * jtulach@1292: * @return the number of elements in this set jtulach@1292: */ jtulach@1292: public int size() { jtulach@1292: return size; jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Returns true if this set contains no elements. jtulach@1292: * jtulach@1292: * @return true if this set contains no elements jtulach@1292: */ jtulach@1292: public boolean isEmpty() { jtulach@1292: return size == 0; jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Returns true if this set contains the specified element. jtulach@1292: * jtulach@1292: * @param e element to be checked for containment in this collection jtulach@1292: * @return true if this set contains the specified element jtulach@1292: */ jtulach@1292: public boolean contains(Object e) { jtulach@1292: if (e == null) jtulach@1292: return false; jtulach@1292: Class eClass = e.getClass(); jtulach@1292: if (eClass != elementType && eClass.getSuperclass() != elementType) jtulach@1292: return false; jtulach@1292: jtulach@1292: int eOrdinal = ((Enum)e).ordinal(); jtulach@1292: return (elements[eOrdinal >>> 6] & (1L << eOrdinal)) != 0; jtulach@1292: } jtulach@1292: jtulach@1292: // Modification Operations jtulach@1292: jtulach@1292: /** jtulach@1292: * Adds the specified element to this set if it is not already present. jtulach@1292: * jtulach@1292: * @param e element to be added to this set jtulach@1292: * @return true if the set changed as a result of the call jtulach@1292: * jtulach@1292: * @throws NullPointerException if e is null jtulach@1292: */ jtulach@1292: public boolean add(E e) { jtulach@1292: typeCheck(e); jtulach@1292: jtulach@1292: int eOrdinal = e.ordinal(); jtulach@1292: int eWordNum = eOrdinal >>> 6; jtulach@1292: jtulach@1292: long oldElements = elements[eWordNum]; jtulach@1292: elements[eWordNum] |= (1L << eOrdinal); jtulach@1292: boolean result = (elements[eWordNum] != oldElements); jtulach@1292: if (result) jtulach@1292: size++; jtulach@1292: return result; jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Removes the specified element from this set if it is present. jtulach@1292: * jtulach@1292: * @param e element to be removed from this set, if present jtulach@1292: * @return true if the set contained the specified element jtulach@1292: */ jtulach@1292: public boolean remove(Object e) { jtulach@1292: if (e == null) jtulach@1292: return false; jtulach@1292: Class eClass = e.getClass(); jtulach@1292: if (eClass != elementType && eClass.getSuperclass() != elementType) jtulach@1292: return false; jtulach@1292: int eOrdinal = ((Enum)e).ordinal(); jtulach@1292: int eWordNum = eOrdinal >>> 6; jtulach@1292: jtulach@1292: long oldElements = elements[eWordNum]; jtulach@1292: elements[eWordNum] &= ~(1L << eOrdinal); jtulach@1292: boolean result = (elements[eWordNum] != oldElements); jtulach@1292: if (result) jtulach@1292: size--; jtulach@1292: return result; jtulach@1292: } jtulach@1292: jtulach@1292: // Bulk Operations jtulach@1292: jtulach@1292: /** jtulach@1292: * Returns true if this set contains all of the elements jtulach@1292: * in the specified collection. jtulach@1292: * jtulach@1292: * @param c collection to be checked for containment in this set jtulach@1292: * @return true if this set contains all of the elements jtulach@1292: * in the specified collection jtulach@1292: * @throws NullPointerException if the specified collection is null jtulach@1292: */ jtulach@1292: public boolean containsAll(Collection c) { jtulach@1292: if (!(c instanceof JumboEnumSet)) jtulach@1292: return super.containsAll(c); jtulach@1292: jtulach@1292: JumboEnumSet es = (JumboEnumSet)c; jtulach@1292: if (es.elementType != elementType) jtulach@1292: return es.isEmpty(); jtulach@1292: jtulach@1292: for (int i = 0; i < elements.length; i++) jtulach@1292: if ((es.elements[i] & ~elements[i]) != 0) jtulach@1292: return false; jtulach@1292: return true; jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Adds all of the elements in the specified collection to this set. jtulach@1292: * jtulach@1292: * @param c collection whose elements are to be added to this set jtulach@1292: * @return true if this set changed as a result of the call jtulach@1292: * @throws NullPointerException if the specified collection or any of jtulach@1292: * its elements are null jtulach@1292: */ jtulach@1292: public boolean addAll(Collection c) { jtulach@1292: if (!(c instanceof JumboEnumSet)) jtulach@1292: return super.addAll(c); jtulach@1292: jtulach@1292: JumboEnumSet es = (JumboEnumSet)c; jtulach@1292: if (es.elementType != elementType) { jtulach@1292: if (es.isEmpty()) jtulach@1292: return false; jtulach@1292: else jtulach@1292: throw new ClassCastException( jtulach@1292: es.elementType + " != " + elementType); jtulach@1292: } jtulach@1292: jtulach@1292: for (int i = 0; i < elements.length; i++) jtulach@1292: elements[i] |= es.elements[i]; jtulach@1292: return recalculateSize(); jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Removes from this set all of its elements that are contained in jtulach@1292: * the specified collection. jtulach@1292: * jtulach@1292: * @param c elements to be removed from this set jtulach@1292: * @return true if this set changed as a result of the call jtulach@1292: * @throws NullPointerException if the specified collection is null jtulach@1292: */ jtulach@1292: public boolean removeAll(Collection c) { jtulach@1292: if (!(c instanceof JumboEnumSet)) jtulach@1292: return super.removeAll(c); jtulach@1292: jtulach@1292: JumboEnumSet es = (JumboEnumSet)c; jtulach@1292: if (es.elementType != elementType) jtulach@1292: return false; jtulach@1292: jtulach@1292: for (int i = 0; i < elements.length; i++) jtulach@1292: elements[i] &= ~es.elements[i]; jtulach@1292: return recalculateSize(); jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Retains only the elements in this set that are contained in the jtulach@1292: * specified collection. jtulach@1292: * jtulach@1292: * @param c elements to be retained in this set jtulach@1292: * @return true if this set changed as a result of the call jtulach@1292: * @throws NullPointerException if the specified collection is null jtulach@1292: */ jtulach@1292: public boolean retainAll(Collection c) { jtulach@1292: if (!(c instanceof JumboEnumSet)) jtulach@1292: return super.retainAll(c); jtulach@1292: jtulach@1292: JumboEnumSet es = (JumboEnumSet)c; jtulach@1292: if (es.elementType != elementType) { jtulach@1292: boolean changed = (size != 0); jtulach@1292: clear(); jtulach@1292: return changed; jtulach@1292: } jtulach@1292: jtulach@1292: for (int i = 0; i < elements.length; i++) jtulach@1292: elements[i] &= es.elements[i]; jtulach@1292: return recalculateSize(); jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Removes all of the elements from this set. jtulach@1292: */ jtulach@1292: public void clear() { jtulach@1292: Arrays.fill(elements, 0); jtulach@1292: size = 0; jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Compares the specified object with this set for equality. Returns jtulach@1292: * true if the given object is also a set, the two sets have jtulach@1292: * the same size, and every member of the given set is contained in jtulach@1292: * this set. jtulach@1292: * jtulach@1292: * @param e object to be compared for equality with this set jtulach@1292: * @return true if the specified object is equal to this set jtulach@1292: */ jtulach@1292: public boolean equals(Object o) { jtulach@1292: if (!(o instanceof JumboEnumSet)) jtulach@1292: return super.equals(o); jtulach@1292: jtulach@1292: JumboEnumSet es = (JumboEnumSet)o; jtulach@1292: if (es.elementType != elementType) jtulach@1292: return size == 0 && es.size == 0; jtulach@1292: jtulach@1292: return Arrays.equals(es.elements, elements); jtulach@1292: } jtulach@1292: jtulach@1292: /** jtulach@1292: * Recalculates the size of the set. Returns true if it's changed. jtulach@1292: */ jtulach@1292: private boolean recalculateSize() { jtulach@1292: int oldSize = size; jtulach@1292: size = 0; jtulach@1292: for (long elt : elements) jtulach@1292: size += Long.bitCount(elt); jtulach@1292: jtulach@1292: return size != oldSize; jtulach@1292: } jtulach@1292: jtulach@1292: public EnumSet clone() { jtulach@1292: JumboEnumSet result = (JumboEnumSet) super.clone(); jtulach@1292: result.elements = result.elements.clone(); jtulach@1292: return result; jtulach@1292: } jtulach@1292: }