jdk/src/share/classes/java/util/EnumSet.java
author sherman
Wed, 21 Oct 2009 11:40:40 -0700
changeset 4161 679d00486dc6
parent 1247 b4c26443dee5
child 5506 202f599c92aa
permissions -rw-r--r--
6878475: Better syntax for the named capture group in regex Summary: Updated the syntax of the newly added named capture group Reviewed-by: martin, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1247
b4c26443dee5 6754988: Update copyright year
xdono
parents: 1088
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return new RegularEnumSet<E>(elementType, universe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return new JumboEnumSet<E>(elementType, universe);
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        EnumSet<E> result = noneOf(first.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        result.add(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        for (E e : rest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            result.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Creates an enum set initially containing all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * range defined by the two specified endpoints.  The returned set will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * contain the endpoints themselves, which may be identical but must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * be out of order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param from the first element in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param to the last element in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @throws NullPointerException if {@code from} or {@code to} are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws IllegalArgumentException if {@code from.compareTo(to) > 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return an enum set initially containing all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *         range defined by the two specified endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (from.compareTo(to) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        EnumSet<E> result = noneOf(from.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        result.addRange(from, to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Adds the specified range to this enum set, which is empty prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * to the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    abstract void addRange(E from, E to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns a copy of this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return a copy of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public EnumSet<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return (EnumSet<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } catch(CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
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
     * Complements the contents of this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    abstract void complement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Throws an exception if e is not of the correct type for this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    final void typeCheck(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        Class eClass = e.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (eClass != elementType && eClass.getSuperclass() != elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            throw new ClassCastException(eClass + " != " + elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Returns all of the values comprising E.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * The result is uncloned, cached, and shared by all callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return SharedSecrets.getJavaLangAccess()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                        .getEnumConstantsShared(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * This class is used to serialize all EnumSet instances, regardless of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * implementation type.  It captures their "logical contents" and they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * are reconstructed using public static factories.  This is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * to ensure that the existence of a particular implementation type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * an implementation detail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private static class SerializationProxy <E extends Enum<E>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         * The element type of this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        private final Class<E> elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * The elements contained in this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        private final Enum[] elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        SerializationProxy(EnumSet<E> set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            elementType = set.elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            EnumSet<E> result = EnumSet.noneOf(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            for (Enum e : elements)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                result.add((E)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        private static final long serialVersionUID = 362491234563181265L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    Object writeReplace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return new SerializationProxy<E>(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   435
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   436
    // readObject method for the serialization proxy pattern
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   437
    // See Effective Java, Second Ed., Item 78.
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   438
    private void readObject(java.io.ObjectInputStream stream)
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   439
        throws java.io.InvalidObjectException {
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   440
        throw new java.io.InvalidObjectException("Proxy required");
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   441
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
}