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