jdk/src/share/classes/java/util/EnumSet.java
author bpb
Mon, 15 Jul 2013 14:37:01 -0700
changeset 18829 ec84f0c313b0
parent 14342 8435a30053c1
child 23010 6dadb192ad81
permissions -rw-r--r--
8020409: Clean up doclint problems in java.util package, part 1 Summary: Clean up doclint problems in java.util package, part 1 Reviewed-by: darcy Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12448
diff changeset
     2
 * Copyright (c) 2003, 2012, 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
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
    91
    final Enum<?>[] universe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
    93
    private static Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
    95
    EnumSet(Class<E>elementType, Enum<?>[] universe) {
2
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
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   103
     * @param <E> The class of the elements in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param elementType the class object of the element type for this enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *     set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   106
     * @return An empty enum set of the specified type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws NullPointerException if <tt>elementType</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   110
        Enum<?>[] universe = getUniverse(elementType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (universe == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new ClassCastException(elementType + " not an enum");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (universe.length <= 64)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   115
            return new RegularEnumSet<>(elementType, universe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        else
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   117
            return new JumboEnumSet<>(elementType, universe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Creates an enum set containing all of the elements in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * element type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   124
     * @param <E> The class of the elements in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param elementType the class object of the element type for this enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *     set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   127
     * @return An enum set containing all the elements in the specified type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @throws NullPointerException if <tt>elementType</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        EnumSet<E> result = noneOf(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        result.addAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Adds all of the elements from the appropriate enum type to this enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * set, which is empty prior to the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    abstract void addAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Creates an enum set with the same element type as the specified enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * set, initially containing the same elements (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   146
     * @param <E> The class of the elements in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param s the enum set from which to initialize this enum set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   148
     * @return A copy of the specified enum set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws NullPointerException if <tt>s</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return s.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Creates an enum set initialized from the specified collection.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * the specified collection is an <tt>EnumSet</tt> instance, this static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * factory method behaves identically to {@link #copyOf(EnumSet)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Otherwise, the specified collection must contain at least one element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * (in order to determine the new enum set's element type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   162
     * @param <E> The class of the elements in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param c the collection from which to initialize this enum set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   164
     * @return An enum set initialized from the given collection.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @throws IllegalArgumentException if <tt>c</tt> is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *     <tt>EnumSet</tt> instance and contains no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @throws NullPointerException if <tt>c</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (c instanceof EnumSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return ((EnumSet<E>)c).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (c.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw new IllegalArgumentException("Collection is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            Iterator<E> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            E first = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            EnumSet<E> result = EnumSet.of(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                result.add(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Creates an enum set with the same element type as the specified enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * set, initially containing all the elements of this type that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <i>not</i> contained in the specified set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   189
     * @param <E> The class of the elements in the enum set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param s the enum set from whose complement to initialize this enum set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   191
     * @return The complement of the specified set in this set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws NullPointerException if <tt>s</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        EnumSet<E> result = copyOf(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        result.complement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Creates an enum set initially containing the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   209
     * @param <E> The class of the specified element and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param e the element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @throws NullPointerException if <tt>e</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @return an enum set initially containing the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public static <E extends Enum<E>> EnumSet<E> of(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        EnumSet<E> result = noneOf(e.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        result.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   229
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   251
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param e3 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        result.add(e3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   275
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param e3 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param e4 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        result.add(e3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        result.add(e4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   301
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param e3 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param e4 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param e5 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                                    E e5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        result.add(e3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        result.add(e4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        result.add(e5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * This factory, whose parameter list uses the varargs feature, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * be used to create an enum set initially containing an arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * number of elements, but it is likely to run slower than the overloadings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   329
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param first an element that the set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param rest the remaining elements the set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @throws NullPointerException if any of the specified elements are null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *     or if <tt>rest</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 7803
diff changeset
   336
    @SafeVarargs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        EnumSet<E> result = noneOf(first.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        result.add(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        for (E e : rest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            result.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Creates an enum set initially containing all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * range defined by the two specified endpoints.  The returned set will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * contain the endpoints themselves, which may be identical but must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * be out of order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   351
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param from the first element in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param to the last element in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @throws NullPointerException if {@code from} or {@code to} are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @throws IllegalArgumentException if {@code from.compareTo(to) > 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return an enum set initially containing all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *         range defined by the two specified endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (from.compareTo(to) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        EnumSet<E> result = noneOf(from.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        result.addRange(from, to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return result;
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
     * Adds the specified range to this enum set, which is empty prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * to the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    abstract void addRange(E from, E to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Returns a copy of this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return a copy of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   378
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public EnumSet<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return (EnumSet<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } catch(CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Complements the contents of this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    abstract void complement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Throws an exception if e is not of the correct type for this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    final void typeCheck(E e) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   396
        Class<?> eClass = e.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (eClass != elementType && eClass.getSuperclass() != elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw new ClassCastException(eClass + " != " + elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns all of the values comprising E.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * The result is uncloned, cached, and shared by all callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return SharedSecrets.getJavaLangAccess()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                        .getEnumConstantsShared(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * This class is used to serialize all EnumSet instances, regardless of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * implementation type.  It captures their "logical contents" and they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * are reconstructed using public static factories.  This is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * to ensure that the existence of a particular implementation type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * an implementation detail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private static class SerializationProxy <E extends Enum<E>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         * The element type of this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        private final Class<E> elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * The elements contained in this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   434
        private final Enum<?>[] elements;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        SerializationProxy(EnumSet<E> set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            elementType = set.elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   441
        // instead of cast to E, we should perhaps use elementType.cast()
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   442
        // to avoid injection of forged stream, but it will slow the implementation
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   443
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            EnumSet<E> result = EnumSet.noneOf(elementType);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   446
            for (Enum<?> e : elements)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                result.add((E)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        private static final long serialVersionUID = 362491234563181265L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    Object writeReplace() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   455
        return new SerializationProxy<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   457
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   458
    // readObject method for the serialization proxy pattern
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   459
    // See Effective Java, Second Ed., Item 78.
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   460
    private void readObject(java.io.ObjectInputStream stream)
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   461
        throws java.io.InvalidObjectException {
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   462
        throw new java.io.InvalidObjectException("Proxy required");
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   463
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
}