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 "regular sized" enum types jtulach@1292: * (i.e., those with 64 or fewer enum constants). jtulach@1292: * jtulach@1292: * @author Josh Bloch jtulach@1292: * @since 1.5 jtulach@1292: * @serial exclude jtulach@1292: */ jtulach@1292: class RegularEnumSet> extends EnumSet { jtulach@1292: private static final long serialVersionUID = 3411599620347842686L; jtulach@1292: /** jtulach@1292: * Bit vector representation of this set. The 2^k bit indicates the jtulach@1292: * presence of universe[k] in this set. jtulach@1292: */ jtulach@1292: private long elements = 0L; jtulach@1292: jtulach@1292: RegularEnumSet(ClasselementType, Enum[] universe) { jtulach@1292: super(elementType, universe); jtulach@1292: } jtulach@1292: jtulach@1292: void addRange(E from, E to) { jtulach@1292: elements = (-1L >>> (from.ordinal() - to.ordinal() - 1)) << from.ordinal(); jtulach@1292: } jtulach@1292: jtulach@1292: void addAll() { jtulach@1292: if (universe.length != 0) jtulach@1292: elements = -1L >>> -universe.length; jtulach@1292: } jtulach@1292: jtulach@1292: void complement() { jtulach@1292: if (universe.length != 0) { jtulach@1292: elements = ~elements; jtulach@1292: elements &= -1L >>> -universe.length; // Mask unused bits jtulach@1292: } 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 "snapshot" iterator that will never throw {@link jtulach@1292: * ConcurrentModificationException}; the elements are traversed as they jtulach@1292: * existed when this call was invoked. 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 set not yet jtulach@1292: * returned by this iterator. jtulach@1292: */ jtulach@1292: long unseen; 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: EnumSetIterator() { jtulach@1292: unseen = elements; jtulach@1292: } jtulach@1292: jtulach@1292: public boolean hasNext() { jtulach@1292: return unseen != 0; jtulach@1292: } jtulach@1292: jtulach@1292: public E next() { jtulach@1292: if (unseen == 0) jtulach@1292: throw new NoSuchElementException(); jtulach@1292: lastReturned = unseen & -unseen; jtulach@1292: unseen -= lastReturned; jtulach@1292: return (E) universe[Long.numberOfTrailingZeros(lastReturned)]; jtulach@1292: } jtulach@1292: jtulach@1292: public void remove() { jtulach@1292: if (lastReturned == 0) jtulach@1292: throw new IllegalStateException(); jtulach@1292: elements &= ~lastReturned; 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 Long.bitCount(elements); 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 elements == 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: return (elements & (1L << ((Enum)e).ordinal())) != 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: long oldElements = elements; jtulach@1292: elements |= (1L << ((Enum)e).ordinal()); jtulach@1292: return elements != oldElements; 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: jtulach@1292: long oldElements = elements; jtulach@1292: elements &= ~(1L << ((Enum)e).ordinal()); jtulach@1292: return elements != oldElements; 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 RegularEnumSet)) jtulach@1292: return super.containsAll(c); jtulach@1292: jtulach@1292: RegularEnumSet es = (RegularEnumSet)c; jtulach@1292: if (es.elementType != elementType) jtulach@1292: return es.isEmpty(); jtulach@1292: jtulach@1292: return (es.elements & ~elements) == 0; 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 jtulach@1292: * of its elements are null jtulach@1292: */ jtulach@1292: public boolean addAll(Collection c) { jtulach@1292: if (!(c instanceof RegularEnumSet)) jtulach@1292: return super.addAll(c); jtulach@1292: jtulach@1292: RegularEnumSet es = (RegularEnumSet)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: long oldElements = elements; jtulach@1292: elements |= es.elements; jtulach@1292: return elements != oldElements; 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 RegularEnumSet)) jtulach@1292: return super.removeAll(c); jtulach@1292: jtulach@1292: RegularEnumSet es = (RegularEnumSet)c; jtulach@1292: if (es.elementType != elementType) jtulach@1292: return false; jtulach@1292: jtulach@1292: long oldElements = elements; jtulach@1292: elements &= ~es.elements; jtulach@1292: return elements != oldElements; 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 RegularEnumSet)) jtulach@1292: return super.retainAll(c); jtulach@1292: jtulach@1292: RegularEnumSet es = (RegularEnumSet)c; jtulach@1292: if (es.elementType != elementType) { jtulach@1292: boolean changed = (elements != 0); jtulach@1292: elements = 0; jtulach@1292: return changed; jtulach@1292: } jtulach@1292: jtulach@1292: long oldElements = elements; jtulach@1292: elements &= es.elements; jtulach@1292: return elements != oldElements; 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: elements = 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 RegularEnumSet)) jtulach@1292: return super.equals(o); jtulach@1292: jtulach@1292: RegularEnumSet es = (RegularEnumSet)o; jtulach@1292: if (es.elementType != elementType) jtulach@1292: return elements == 0 && es.elements == 0; jtulach@1292: return es.elements == elements; jtulach@1292: } jtulach@1292: }