jdk/src/share/classes/java/util/EnumSet.java
author darcy
Fri, 19 Aug 2011 17:42:24 -0700
changeset 10350 6d009f117062
parent 9035 1255eb81cc2f
child 12448 b95438b17098
permissions -rw-r--r--
4748706: typos in java.util.Observable documentation Reviewed-by: iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8151
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.misc.SharedSecrets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A specialized {@link Set} implementation for use with enum types.  All of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * the elements in an enum set must come from a single enum type that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * specified, explicitly or implicitly, when the set is created.  Enum sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * are represented internally as bit vectors.  This representation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * extremely compact and efficient. The space and time performance of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * class should be good enough to allow its use as a high-quality, typesafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * alternative to traditional <tt>int</tt>-based "bit flags."  Even bulk
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * operations (such as <tt>containsAll</tt> and <tt>retainAll</tt>) should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * run very quickly if their argument is also an enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>The iterator returned by the <tt>iterator</tt> method traverses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * elements in their <i>natural order</i> (the order in which the enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * constants are declared).  The returned iterator is <i>weakly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * consistent</i>: it will never throw {@link ConcurrentModificationException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * and it may or may not show the effects of any modifications to the set that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * occur while the iteration is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Null elements are not permitted.  Attempts to insert a null element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * will throw {@link NullPointerException}.  Attempts to test for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * presence of a null element or to remove one will, however, function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <P>Like most collection implementations, <tt>EnumSet</tt> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * synchronized.  If multiple threads access an enum set concurrently, and at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * least one of the threads modifies the set, it should be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * externally.  This is typically accomplished by synchronizing on some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * object that naturally encapsulates the enum set.  If no such object exists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the set should be "wrapped" using the {@link Collections#synchronizedSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * unsynchronized access:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Set&lt;MyEnum&gt; s = Collections.synchronizedSet(EnumSet.noneOf(MyEnum.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>Implementation note: All basic operations execute in constant time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * They are likely (though not guaranteed) to be much faster than their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * {@link HashSet} counterparts.  Even bulk operations execute in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * constant time if their argument is also an enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @see EnumMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    implements Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The class of all the elements of this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    final Class<E> elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * All of the values comprising T.  (Cached for performance.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    final Enum[] universe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static Enum[] ZERO_LENGTH_ENUM_ARRAY = new Enum[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    EnumSet(Class<E>elementType, Enum[] universe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.elementType = elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.universe    = universe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Creates an empty enum set with the specified element type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param elementType the class object of the element type for this enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *     set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @throws NullPointerException if <tt>elementType</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        Enum[] universe = getUniverse(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (universe == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new ClassCastException(elementType + " not an enum");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (universe.length <= 64)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   113
            return new RegularEnumSet<>(elementType, universe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        else
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   115
            return new JumboEnumSet<>(elementType, universe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Creates an enum set containing all of the elements in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * element type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param elementType the class object of the element type for this enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *     set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @throws NullPointerException if <tt>elementType</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        EnumSet<E> result = noneOf(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        result.addAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Adds all of the elements from the appropriate enum type to this enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * set, which is empty prior to the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    abstract void addAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Creates an enum set with the same element type as the specified enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * set, initially containing the same elements (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param s the enum set from which to initialize this enum set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws NullPointerException if <tt>s</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return s.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Creates an enum set initialized from the specified collection.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * the specified collection is an <tt>EnumSet</tt> instance, this static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * factory method behaves identically to {@link #copyOf(EnumSet)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Otherwise, the specified collection must contain at least one element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * (in order to determine the new enum set's element type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param c the collection from which to initialize this enum set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws IllegalArgumentException if <tt>c</tt> is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *     <tt>EnumSet</tt> instance and contains no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws NullPointerException if <tt>c</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (c instanceof EnumSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return ((EnumSet<E>)c).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (c.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                throw new IllegalArgumentException("Collection is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            Iterator<E> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            E first = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            EnumSet<E> result = EnumSet.of(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                result.add(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Creates an enum set with the same element type as the specified enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * set, initially containing all the elements of this type that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <i>not</i> contained in the specified set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param s the enum set from whose complement to initialize this enum set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws NullPointerException if <tt>s</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        EnumSet<E> result = copyOf(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        result.complement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Creates an enum set initially containing the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param e the element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @throws NullPointerException if <tt>e</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return an enum set initially containing the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public static <E extends Enum<E>> EnumSet<E> of(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        EnumSet<E> result = noneOf(e.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        result.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param e3 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        result.add(e3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param e3 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param e4 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        result.add(e3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        result.add(e4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param e3 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param e4 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param e5 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                                    E e5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        result.add(e3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        result.add(e4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        result.add(e5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * This factory, whose parameter list uses the varargs feature, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * be used to create an enum set initially containing an arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * number of elements, but it is likely to run slower than the overloadings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param first an element that the set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param rest the remaining elements the set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws NullPointerException if any of the specified elements are null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *     or if <tt>rest</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 7803
diff changeset
   320
    @SafeVarargs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        EnumSet<E> result = noneOf(first.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        result.add(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        for (E e : rest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            result.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Creates an enum set initially containing all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * range defined by the two specified endpoints.  The returned set will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * contain the endpoints themselves, which may be identical but must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * be out of order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param from the first element in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param to the last element in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws NullPointerException if {@code from} or {@code to} are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @throws IllegalArgumentException if {@code from.compareTo(to) > 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @return an enum set initially containing all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *         range defined by the two specified endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (from.compareTo(to) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        EnumSet<E> result = noneOf(from.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        result.addRange(from, to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Adds the specified range to this enum set, which is empty prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * to the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    abstract void addRange(E from, E to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Returns a copy of this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return a copy of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public EnumSet<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return (EnumSet<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        } catch(CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Complements the contents of this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    abstract void complement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Throws an exception if e is not of the correct type for this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    final void typeCheck(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        Class eClass = e.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (eClass != elementType && eClass.getSuperclass() != elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new ClassCastException(eClass + " != " + elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Returns all of the values comprising E.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * The result is uncloned, cached, and shared by all callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return SharedSecrets.getJavaLangAccess()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                        .getEnumConstantsShared(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * This class is used to serialize all EnumSet instances, regardless of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * implementation type.  It captures their "logical contents" and they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * are reconstructed using public static factories.  This is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * to ensure that the existence of a particular implementation type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * an implementation detail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    private static class SerializationProxy <E extends Enum<E>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * The element type of this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        private final Class<E> elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * The elements contained in this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        private final Enum[] elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        SerializationProxy(EnumSet<E> set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            elementType = set.elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            EnumSet<E> result = EnumSet.noneOf(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            for (Enum e : elements)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                result.add((E)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        private static final long serialVersionUID = 362491234563181265L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    Object writeReplace() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   434
        return new SerializationProxy<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   436
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   437
    // readObject method for the serialization proxy pattern
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   438
    // See Effective Java, Second Ed., Item 78.
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   439
    private void readObject(java.io.ObjectInputStream stream)
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   440
        throws java.io.InvalidObjectException {
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   441
        throw new java.io.InvalidObjectException("Proxy required");
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   442
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
}