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