RegularEnumSet.java revision 2362
0N/A/*
0N/A * Copyright (c) 2003, 2006, 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
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/A/**
0N/A * Private implementation class for EnumSet, for "regular sized" enum types
0N/A * (i.e., those with 64 or fewer enum constants).
0N/A *
0N/A * @author Josh Bloch
0N/A * @since 1.5
0N/A * @serial exclude
0N/A */
0N/Aclass RegularEnumSet<E extends Enum<E>> extends EnumSet<E> {
0N/A /**
0N/A * Bit vector representation of this set. The 2^k bit indicates the
0N/A * presence of universe[k] in this set.
0N/A */
0N/A private long elements = 0L;
0N/A
0N/A RegularEnumSet(Class<E>elementType, Enum[] universe) {
0N/A super(elementType, universe);
0N/A }
0N/A
0N/A void addRange(E from, E to) {
0N/A elements = (-1L >>> (from.ordinal() - to.ordinal() - 1)) << from.ordinal();
0N/A }
0N/A
0N/A void addAll() {
0N/A if (universe.length != 0)
0N/A elements = -1L >>> -universe.length;
0N/A }
0N/A
0N/A void complement() {
0N/A if (universe.length != 0) {
0N/A elements = ~elements;
0N/A elements &= -1L >>> -universe.length; // Mask unused bits
0N/A }
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 "snapshot" iterator that will never throw {@link
0N/A * ConcurrentModificationException}; the elements are traversed as they
0N/A * existed when this call was invoked.
0N/A *
0N/A * @return an iterator over the elements contained in this set
0N/A */
0N/A public Iterator<E> iterator() {
0N/A return new EnumSetIterator<E>();
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 set not yet
0N/A * returned by this iterator.
0N/A */
0N/A long unseen;
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 EnumSetIterator() {
0N/A unseen = elements;
0N/A }
0N/A
0N/A public boolean hasNext() {
0N/A return unseen != 0;
0N/A }
0N/A
0N/A public E next() {
0N/A if (unseen == 0)
0N/A throw new NoSuchElementException();
0N/A lastReturned = unseen & -unseen;
0N/A unseen -= lastReturned;
0N/A return (E) universe[Long.numberOfTrailingZeros(lastReturned)];
0N/A }
0N/A
0N/A public void remove() {
0N/A if (lastReturned == 0)
0N/A throw new IllegalStateException();
0N/A elements -= lastReturned;
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 Long.bitCount(elements);
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 elements == 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 return (elements & (1L << ((Enum)e).ordinal())) != 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 long oldElements = elements;
0N/A elements |= (1L << ((Enum)e).ordinal());
0N/A return elements != oldElements;
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
0N/A long oldElements = elements;
0N/A elements &= ~(1L << ((Enum)e).ordinal());
0N/A return elements != oldElements;
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 RegularEnumSet))
0N/A return super.containsAll(c);
0N/A
0N/A RegularEnumSet es = (RegularEnumSet)c;
0N/A if (es.elementType != elementType)
0N/A return es.isEmpty();
0N/A
0N/A return (es.elements & ~elements) == 0;
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
0N/A * of its elements are null
0N/A */
0N/A public boolean addAll(Collection<? extends E> c) {
0N/A if (!(c instanceof RegularEnumSet))
0N/A return super.addAll(c);
0N/A
0N/A RegularEnumSet es = (RegularEnumSet)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 long oldElements = elements;
0N/A elements |= es.elements;
0N/A return elements != oldElements;
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 RegularEnumSet))
0N/A return super.removeAll(c);
0N/A
0N/A RegularEnumSet es = (RegularEnumSet)c;
0N/A if (es.elementType != elementType)
0N/A return false;
0N/A
0N/A long oldElements = elements;
0N/A elements &= ~es.elements;
0N/A return elements != oldElements;
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 RegularEnumSet))
0N/A return super.retainAll(c);
0N/A
0N/A RegularEnumSet<?> es = (RegularEnumSet<?>)c;
0N/A if (es.elementType != elementType) {
0N/A boolean changed = (elements != 0);
0N/A elements = 0;
0N/A return changed;
0N/A }
0N/A
0N/A long oldElements = elements;
0N/A elements &= es.elements;
0N/A return elements != oldElements;
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 elements = 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 RegularEnumSet))
0N/A return super.equals(o);
0N/A
0N/A RegularEnumSet es = (RegularEnumSet)o;
0N/A if (es.elementType != elementType)
0N/A return elements == 0 && es.elements == 0;
0N/A return es.elements == elements;
0N/A }
0N/A}
0N/A