src/java.base/share/classes/java/util/EnumSet.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55697
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 52887
diff changeset
     2
 * Copyright (c) 2003, 2019, 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
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49433
diff changeset
    28
import jdk.internal.access.SharedSecrets;
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    37
 * alternative to traditional {@code int}-based "bit flags."  Even bulk
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    38
 * operations (such as {@code containsAll} and {@code retainAll}) should
2
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
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    41
 * <p>The iterator returned by the {@code iterator} method traverses the
2
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
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    53
 * <P>Like most collection implementations, {@code EnumSet} is not
2
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
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48230
diff changeset
    72
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    implements Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
{
55697
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 52887
diff changeset
    82
    // declare EnumSet.class serialization compatibility with JDK 8
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55697
diff changeset
    83
    @java.io.Serial
55697
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 52887
diff changeset
    84
    private static final long serialVersionUID = 1009687484059888093L;
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 52887
diff changeset
    85
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The class of all the elements of this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
    89
    final transient Class<E> elementType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
    92
     * All of the values comprising E.  (Cached for performance.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
    94
    final transient Enum<?>[] universe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
    96
    EnumSet(Class<E>elementType, Enum<?>[] universe) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.elementType = elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.universe    = universe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Creates an empty enum set with the specified element type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   104
     * @param <E> The class of the elements in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param elementType the class object of the element type for this enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *     set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   107
     * @return An empty enum set of the specified type.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   108
     * @throws NullPointerException if {@code elementType} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    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
   111
        Enum<?>[] universe = getUniverse(elementType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (universe == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new ClassCastException(elementType + " not an enum");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (universe.length <= 64)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   116
            return new RegularEnumSet<>(elementType, universe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        else
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   118
            return new JumboEnumSet<>(elementType, universe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Creates an enum set containing all of the elements in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * element type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   125
     * @param <E> The class of the elements in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param elementType the class object of the element type for this enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *     set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   128
     * @return An enum set containing all the elements in the specified type.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   129
     * @throws NullPointerException if {@code elementType} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        EnumSet<E> result = noneOf(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        result.addAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Adds all of the elements from the appropriate enum type to this enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * set, which is empty prior to the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    abstract void addAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Creates an enum set with the same element type as the specified enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * set, initially containing the same elements (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   147
     * @param <E> The class of the elements in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @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
   149
     * @return A copy of the specified enum set.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   150
     * @throws NullPointerException if {@code s} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return s.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Creates an enum set initialized from the specified collection.  If
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   158
     * the specified collection is an {@code EnumSet} instance, this static
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * factory method behaves identically to {@link #copyOf(EnumSet)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Otherwise, the specified collection must contain at least one element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * (in order to determine the new enum set's element type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   163
     * @param <E> The class of the elements in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @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
   165
     * @return An enum set initialized from the given collection.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   166
     * @throws IllegalArgumentException if {@code c} is not an
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   167
     *     {@code EnumSet} instance and contains no elements
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   168
     * @throws NullPointerException if {@code c} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (c instanceof EnumSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return ((EnumSet<E>)c).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (c.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                throw new IllegalArgumentException("Collection is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            Iterator<E> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            E first = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            EnumSet<E> result = EnumSet.of(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                result.add(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return result;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Creates an enum set with the same element type as the specified enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * set, initially containing all the elements of this type that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <i>not</i> contained in the specified set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   190
     * @param <E> The class of the elements in the enum set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @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
   192
     * @return The complement of the specified set in this set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   193
     * @throws NullPointerException if {@code s} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        EnumSet<E> result = copyOf(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        result.complement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Creates an enum set initially containing the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   210
     * @param <E> The class of the specified element and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param e the element that this set is to contain initially
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   212
     * @throws NullPointerException if {@code e} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return an enum set initially containing the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public static <E extends Enum<E>> EnumSet<E> of(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        EnumSet<E> result = noneOf(e.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        result.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   230
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   252
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param e3 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        result.add(e3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   276
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param e3 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param e4 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        result.add(e3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        result.add(e4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Overloadings of this method exist to initialize an enum set with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * one through five elements.  A sixth overloading is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * uses the varargs feature.  This overloading may be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * an enum set initially containing an arbitrary number of elements, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * is likely to run slower than the overloadings that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   302
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param e1 an element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param e2 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param e3 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param e4 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param e5 another element that this set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @throws NullPointerException if any parameters are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                                    E e5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        EnumSet<E> result = noneOf(e1.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        result.add(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        result.add(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        result.add(e3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        result.add(e4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        result.add(e5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Creates an enum set initially containing the specified elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * This factory, whose parameter list uses the varargs feature, may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * be used to create an enum set initially containing an arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * number of elements, but it is likely to run slower than the overloadings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * that do not use varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   330
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param first an element that the set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param rest the remaining elements the set is to contain initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws NullPointerException if any of the specified elements are null,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   334
     *     or if {@code rest} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return an enum set initially containing the specified elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 7803
diff changeset
   337
    @SafeVarargs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        EnumSet<E> result = noneOf(first.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        result.add(first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        for (E e : rest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            result.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Creates an enum set initially containing all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * range defined by the two specified endpoints.  The returned set will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * contain the endpoints themselves, which may be identical but must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * be out of order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 14342
diff changeset
   352
     * @param <E> The class of the parameter elements and of the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param from the first element in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param to the last element in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @throws NullPointerException if {@code from} or {@code to} are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @throws IllegalArgumentException if {@code from.compareTo(to) > 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return an enum set initially containing all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *         range defined by the two specified endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (from.compareTo(to) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new IllegalArgumentException(from + " > " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        EnumSet<E> result = noneOf(from.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        result.addRange(from, to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Adds the specified range to this enum set, which is empty prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * to the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    abstract void addRange(E from, E to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Returns a copy of this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @return a copy of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   379
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public EnumSet<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return (EnumSet<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } catch(CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            throw new AssertionError(e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Complements the contents of this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    abstract void complement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Throws an exception if e is not of the correct type for this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    final void typeCheck(E e) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   397
        Class<?> eClass = e.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (eClass != elementType && eClass.getSuperclass() != elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw new ClassCastException(eClass + " != " + elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Returns all of the values comprising E.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * The result is uncloned, cached, and shared by all callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return SharedSecrets.getJavaLangAccess()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                        .getEnumConstantsShared(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * This class is used to serialize all EnumSet instances, regardless of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * implementation type.  It captures their "logical contents" and they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * are reconstructed using public static factories.  This is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * to ensure that the existence of a particular implementation type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * an implementation detail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   420
    private static class SerializationProxy<E extends Enum<E>>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    {
35314
a7e13065a7a0 8148044: Remove Enum[0] constants from EnumSet and EnumMap
redestad
parents: 32834
diff changeset
   423
a7e13065a7a0 8148044: Remove Enum[0] constants from EnumSet and EnumMap
redestad
parents: 32834
diff changeset
   424
        private static final Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0];
a7e13065a7a0 8148044: Remove Enum[0] constants from EnumSet and EnumMap
redestad
parents: 32834
diff changeset
   425
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * The element type of this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        private final Class<E> elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * The elements contained in this enum set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   438
        private final Enum<?>[] elements;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        SerializationProxy(EnumSet<E> set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            elementType = set.elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   445
        /**
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   446
         * Returns an {@code EnumSet} object with initial state
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   447
         * held by this proxy.
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   448
         *
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   449
         * @return a {@code EnumSet} object with initial state
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   450
         * held by this proxy
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   451
         */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   452
        @SuppressWarnings("unchecked")
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55697
diff changeset
   453
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        private Object readResolve() {
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   455
            // instead of cast to E, we should perhaps use elementType.cast()
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   456
            // to avoid injection of forged stream, but it will slow the
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   457
            // implementation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            EnumSet<E> result = EnumSet.noneOf(elementType);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   459
            for (Enum<?> e : elements)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                result.add((E)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55697
diff changeset
   464
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        private static final long serialVersionUID = 362491234563181265L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   468
    /**
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   469
     * Returns a
52887
0d89f672c62e 8211882: Broken links in serialized-form.html
smarks
parents: 52427
diff changeset
   470
     * <a href="{@docRoot}/serialized-form.html#java.util.EnumSet.SerializationProxy">
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   471
     * SerializationProxy</a>
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   472
     * representing the state of this instance.
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   473
     *
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   474
     * @return a {@link SerializationProxy}
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   475
     * representing the state of this instance
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   476
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55697
diff changeset
   477
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    Object writeReplace() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   479
        return new SerializationProxy<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   481
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   482
    /**
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   483
     * @param s the stream
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   484
     * @throws java.io.InvalidObjectException always
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   485
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55697
diff changeset
   486
    @java.io.Serial
48230
d0e8542ef650 8192935: Fix EnumSet's SerializationProxy javadoc
martin
parents: 47216
diff changeset
   487
    private void readObject(java.io.ObjectInputStream s)
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   488
        throws java.io.InvalidObjectException {
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   489
        throw new java.io.InvalidObjectException("Proxy required");
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents: 2
diff changeset
   490
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
}