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/Aimport sun.misc.SharedSecrets;
0N/A
0N/A/**
0N/A * A specialized {@link Set} implementation for use with enum types. All of
0N/A * the elements in an enum set must come from a single enum type that is
0N/A * specified, explicitly or implicitly, when the set is created. Enum sets
0N/A * are represented internally as bit vectors. This representation is
0N/A * extremely compact and efficient. The space and time performance of this
0N/A * class should be good enough to allow its use as a high-quality, typesafe
0N/A * alternative to traditional <tt>int</tt>-based "bit flags." Even bulk
0N/A * operations (such as <tt>containsAll</tt> and <tt>retainAll</tt>) should
0N/A * run very quickly if their argument is also an enum set.
0N/A *
0N/A * <p>The iterator returned by the <tt>iterator</tt> method traverses the
0N/A * elements in their <i>natural order</i> (the order in which the enum
0N/A * constants are declared). The returned iterator is <i>weakly
0N/A * consistent</i>: it will never throw {@link ConcurrentModificationException}
0N/A * and it may or may not show the effects of any modifications to the set that
0N/A * occur while the iteration is in progress.
0N/A *
0N/A * <p>Null elements are not permitted. Attempts to insert a null element
0N/A * will throw {@link NullPointerException}. Attempts to test for the
0N/A * presence of a null element or to remove one will, however, function
0N/A * properly.
0N/A *
0N/A * <P>Like most collection implementations, <tt>EnumSet</tt> is not
0N/A * synchronized. If multiple threads access an enum set concurrently, and at
0N/A * least one of the threads modifies the set, it should be synchronized
0N/A * externally. This is typically accomplished by synchronizing on some
0N/A * object that naturally encapsulates the enum set. If no such object exists,
0N/A * the set should be "wrapped" using the {@link Collections#synchronizedSet}
0N/A * method. This is best done at creation time, to prevent accidental
0N/A * unsynchronized access:
0N/A *
0N/A * <pre>
0N/A * Set&lt;MyEnum&gt; s = Collections.synchronizedSet(EnumSet.noneOf(MyEnum.class));
0N/A * </pre>
0N/A *
0N/A * <p>Implementation note: All basic operations execute in constant time.
0N/A * They are likely (though not guaranteed) to be much faster than their
0N/A * {@link HashSet} counterparts. Even bulk operations execute in
0N/A * constant time if their argument is also an enum set.
0N/A *
0N/A * <p>This class is a member of the
0N/A * <a href="{@docRoot}/../technotes/guides/collections/index.html">
0N/A * Java Collections Framework</a>.
0N/A *
0N/A * @author Josh Bloch
0N/A * @since 1.5
0N/A * @see EnumMap
0N/A * @serial exclude
0N/A */
0N/Apublic abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
0N/A implements Cloneable, java.io.Serializable
0N/A{
0N/A /**
0N/A * The class of all the elements of this set.
0N/A */
0N/A final Class<E> elementType;
0N/A
0N/A /**
0N/A * All of the values comprising T. (Cached for performance.)
0N/A */
0N/A final Enum[] universe;
0N/A
0N/A private static Enum[] ZERO_LENGTH_ENUM_ARRAY = new Enum[0];
0N/A
0N/A EnumSet(Class<E>elementType, Enum[] universe) {
0N/A this.elementType = elementType;
0N/A this.universe = universe;
0N/A }
0N/A
0N/A /**
0N/A * Creates an empty enum set with the specified element type.
0N/A *
0N/A * @param elementType the class object of the element type for this enum
0N/A * set
0N/A * @throws NullPointerException if <tt>elementType</tt> is null
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
0N/A Enum[] universe = getUniverse(elementType);
0N/A if (universe == null)
0N/A throw new ClassCastException(elementType + " not an enum");
0N/A
0N/A if (universe.length <= 64)
3323N/A return new RegularEnumSet<>(elementType, universe);
0N/A else
3323N/A return new JumboEnumSet<>(elementType, universe);
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set containing all of the elements in the specified
0N/A * element type.
0N/A *
0N/A * @param elementType the class object of the element type for this enum
0N/A * set
0N/A * @throws NullPointerException if <tt>elementType</tt> is null
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) {
0N/A EnumSet<E> result = noneOf(elementType);
0N/A result.addAll();
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Adds all of the elements from the appropriate enum type to this enum
0N/A * set, which is empty prior to the call.
0N/A */
0N/A abstract void addAll();
0N/A
0N/A /**
0N/A * Creates an enum set with the same element type as the specified enum
0N/A * set, initially containing the same elements (if any).
0N/A *
0N/A * @param s the enum set from which to initialize this enum set
0N/A * @throws NullPointerException if <tt>s</tt> is null
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) {
0N/A return s.clone();
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set initialized from the specified collection. If
0N/A * the specified collection is an <tt>EnumSet</tt> instance, this static
0N/A * factory method behaves identically to {@link #copyOf(EnumSet)}.
0N/A * Otherwise, the specified collection must contain at least one element
0N/A * (in order to determine the new enum set's element type).
0N/A *
0N/A * @param c the collection from which to initialize this enum set
0N/A * @throws IllegalArgumentException if <tt>c</tt> is not an
0N/A * <tt>EnumSet</tt> instance and contains no elements
0N/A * @throws NullPointerException if <tt>c</tt> is null
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) {
0N/A if (c instanceof EnumSet) {
0N/A return ((EnumSet<E>)c).clone();
0N/A } else {
0N/A if (c.isEmpty())
0N/A throw new IllegalArgumentException("Collection is empty");
0N/A Iterator<E> i = c.iterator();
0N/A E first = i.next();
0N/A EnumSet<E> result = EnumSet.of(first);
0N/A while (i.hasNext())
0N/A result.add(i.next());
0N/A return result;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set with the same element type as the specified enum
0N/A * set, initially containing all the elements of this type that are
0N/A * <i>not</i> contained in the specified set.
0N/A *
0N/A * @param s the enum set from whose complement to initialize this enum set
0N/A * @throws NullPointerException if <tt>s</tt> is null
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) {
0N/A EnumSet<E> result = copyOf(s);
0N/A result.complement();
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set initially containing the specified element.
0N/A *
0N/A * Overloadings of this method exist to initialize an enum set with
0N/A * one through five elements. A sixth overloading is provided that
0N/A * uses the varargs feature. This overloading may be used to create
0N/A * an enum set initially containing an arbitrary number of elements, but
0N/A * is likely to run slower than the overloadings that do not use varargs.
0N/A *
0N/A * @param e the element that this set is to contain initially
0N/A * @throws NullPointerException if <tt>e</tt> is null
0N/A * @return an enum set initially containing the specified element
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> of(E e) {
0N/A EnumSet<E> result = noneOf(e.getDeclaringClass());
0N/A result.add(e);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set initially containing the specified elements.
0N/A *
0N/A * Overloadings of this method exist to initialize an enum set with
0N/A * one through five elements. A sixth overloading is provided that
0N/A * uses the varargs feature. This overloading may be used to create
0N/A * an enum set initially containing an arbitrary number of elements, but
0N/A * is likely to run slower than the overloadings that do not use varargs.
0N/A *
0N/A * @param e1 an element that this set is to contain initially
0N/A * @param e2 another element that this set is to contain initially
0N/A * @throws NullPointerException if any parameters are null
0N/A * @return an enum set initially containing the specified elements
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {
0N/A EnumSet<E> result = noneOf(e1.getDeclaringClass());
0N/A result.add(e1);
0N/A result.add(e2);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set initially containing the specified elements.
0N/A *
0N/A * Overloadings of this method exist to initialize an enum set with
0N/A * one through five elements. A sixth overloading is provided that
0N/A * uses the varargs feature. This overloading may be used to create
0N/A * an enum set initially containing an arbitrary number of elements, but
0N/A * is likely to run slower than the overloadings that do not use varargs.
0N/A *
0N/A * @param e1 an element that this set is to contain initially
0N/A * @param e2 another element that this set is to contain initially
0N/A * @param e3 another element that this set is to contain initially
0N/A * @throws NullPointerException if any parameters are null
0N/A * @return an enum set initially containing the specified elements
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3) {
0N/A EnumSet<E> result = noneOf(e1.getDeclaringClass());
0N/A result.add(e1);
0N/A result.add(e2);
0N/A result.add(e3);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set initially containing the specified elements.
0N/A *
0N/A * Overloadings of this method exist to initialize an enum set with
0N/A * one through five elements. A sixth overloading is provided that
0N/A * uses the varargs feature. This overloading may be used to create
0N/A * an enum set initially containing an arbitrary number of elements, but
0N/A * is likely to run slower than the overloadings that do not use varargs.
0N/A *
0N/A * @param e1 an element that this set is to contain initially
0N/A * @param e2 another element that this set is to contain initially
0N/A * @param e3 another element that this set is to contain initially
0N/A * @param e4 another element that this set is to contain initially
0N/A * @throws NullPointerException if any parameters are null
0N/A * @return an enum set initially containing the specified elements
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4) {
0N/A EnumSet<E> result = noneOf(e1.getDeclaringClass());
0N/A result.add(e1);
0N/A result.add(e2);
0N/A result.add(e3);
0N/A result.add(e4);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set initially containing the specified elements.
0N/A *
0N/A * Overloadings of this method exist to initialize an enum set with
0N/A * one through five elements. A sixth overloading is provided that
0N/A * uses the varargs feature. This overloading may be used to create
0N/A * an enum set initially containing an arbitrary number of elements, but
0N/A * is likely to run slower than the overloadings that do not use varargs.
0N/A *
0N/A * @param e1 an element that this set is to contain initially
0N/A * @param e2 another element that this set is to contain initially
0N/A * @param e3 another element that this set is to contain initially
0N/A * @param e4 another element that this set is to contain initially
0N/A * @param e5 another element that this set is to contain initially
0N/A * @throws NullPointerException if any parameters are null
0N/A * @return an enum set initially containing the specified elements
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4,
0N/A E e5)
0N/A {
0N/A EnumSet<E> result = noneOf(e1.getDeclaringClass());
0N/A result.add(e1);
0N/A result.add(e2);
0N/A result.add(e3);
0N/A result.add(e4);
0N/A result.add(e5);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set initially containing the specified elements.
0N/A * This factory, whose parameter list uses the varargs feature, may
0N/A * be used to create an enum set initially containing an arbitrary
0N/A * number of elements, but it is likely to run slower than the overloadings
0N/A * that do not use varargs.
0N/A *
0N/A * @param first an element that the set is to contain initially
0N/A * @param rest the remaining elements the set is to contain initially
0N/A * @throws NullPointerException if any of the specified elements are null,
0N/A * or if <tt>rest</tt> is null
0N/A * @return an enum set initially containing the specified elements
0N/A */
3464N/A @SafeVarargs
0N/A public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) {
0N/A EnumSet<E> result = noneOf(first.getDeclaringClass());
0N/A result.add(first);
0N/A for (E e : rest)
0N/A result.add(e);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Creates an enum set initially containing all of the elements in the
0N/A * range defined by the two specified endpoints. The returned set will
0N/A * contain the endpoints themselves, which may be identical but must not
0N/A * be out of order.
0N/A *
0N/A * @param from the first element in the range
0N/A * @param to the last element in the range
0N/A * @throws NullPointerException if {@code from} or {@code to} are null
0N/A * @throws IllegalArgumentException if {@code from.compareTo(to) > 0}
0N/A * @return an enum set initially containing all of the elements in the
0N/A * range defined by the two specified endpoints
0N/A */
0N/A public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {
0N/A if (from.compareTo(to) > 0)
0N/A throw new IllegalArgumentException(from + " > " + to);
0N/A EnumSet<E> result = noneOf(from.getDeclaringClass());
0N/A result.addRange(from, to);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified range to this enum set, which is empty prior
0N/A * to the call.
0N/A */
0N/A abstract void addRange(E from, E to);
0N/A
0N/A /**
0N/A * Returns a copy of this set.
0N/A *
0N/A * @return a copy of this set
0N/A */
0N/A public EnumSet<E> clone() {
0N/A try {
0N/A return (EnumSet<E>) super.clone();
0N/A } catch(CloneNotSupportedException e) {
0N/A throw new AssertionError(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Complements the contents of this enum set.
0N/A */
0N/A abstract void complement();
0N/A
0N/A /**
0N/A * Throws an exception if e is not of the correct type for this enum set.
0N/A */
0N/A final void typeCheck(E e) {
0N/A Class eClass = e.getClass();
0N/A if (eClass != elementType && eClass.getSuperclass() != elementType)
0N/A throw new ClassCastException(eClass + " != " + elementType);
0N/A }
0N/A
0N/A /**
0N/A * Returns all of the values comprising E.
0N/A * The result is uncloned, cached, and shared by all callers.
0N/A */
0N/A private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {
0N/A return SharedSecrets.getJavaLangAccess()
0N/A .getEnumConstantsShared(elementType);
0N/A }
0N/A
0N/A /**
0N/A * This class is used to serialize all EnumSet instances, regardless of
0N/A * implementation type. It captures their "logical contents" and they
0N/A * are reconstructed using public static factories. This is necessary
0N/A * to ensure that the existence of a particular implementation type is
0N/A * an implementation detail.
0N/A *
0N/A * @serial include
0N/A */
0N/A private static class SerializationProxy <E extends Enum<E>>
0N/A implements java.io.Serializable
0N/A {
0N/A /**
0N/A * The element type of this enum set.
0N/A *
0N/A * @serial
0N/A */
0N/A private final Class<E> elementType;
0N/A
0N/A /**
0N/A * The elements contained in this enum set.
0N/A *
0N/A * @serial
0N/A */
0N/A private final Enum[] elements;
0N/A
0N/A SerializationProxy(EnumSet<E> set) {
0N/A elementType = set.elementType;
0N/A elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);
0N/A }
0N/A
0N/A private Object readResolve() {
0N/A EnumSet<E> result = EnumSet.noneOf(elementType);
0N/A for (Enum e : elements)
0N/A result.add((E)e);
0N/A return result;
0N/A }
0N/A
0N/A private static final long serialVersionUID = 362491234563181265L;
0N/A }
0N/A
0N/A Object writeReplace() {
3323N/A return new SerializationProxy<>(this);
0N/A }
500N/A
500N/A // readObject method for the serialization proxy pattern
500N/A // See Effective Java, Second Ed., Item 78.
500N/A private void readObject(java.io.ObjectInputStream stream)
500N/A throws java.io.InvalidObjectException {
500N/A throw new java.io.InvalidObjectException("Proxy required");
500N/A }
0N/A}