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