src/java.base/share/classes/java/lang/Enum.java
author bchristi
Thu, 09 Nov 2017 11:13:50 -0800
changeset 47818 2f6ab27efb60
parent 47216 71c04702a3d5
child 51413 43e41800d579
permissions -rw-r--r--
8185925: StackFrameInfo::walker field can be replaced with bitmap to save footprint 8153682: StackFrameInfo.declaringClass could be removed Reviewed-by: coleenp, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2017, 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: 4043
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: 4043
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: 4043
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4043
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4043
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.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectStreamException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This is the common base class of all Java language enumeration types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
1948
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
    37
 * More information about enums, including descriptions of the
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
    38
 * implicitly declared methods synthesized by the compiler, can be
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 5506
diff changeset
    39
 * found in section 8.9 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 5506
diff changeset
    40
 * <cite>The Java&trade; Language Specification</cite>.
1938
23526f724210 6327048: Enum javadoc could link to JLS
darcy
parents: 2
diff changeset
    41
 *
4043
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    42
 * <p> Note that when using an enumeration type as the type of a set
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    43
 * or as the type of the keys in a map, specialized and efficient
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    44
 * {@linkplain java.util.EnumSet set} and {@linkplain
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    45
 * java.util.EnumMap map} implementations are available.
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    46
 *
1948
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
    47
 * @param <E> The enum type subclass
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see     Class#getEnumConstants()
4043
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    51
 * @see     java.util.EnumSet
d9543f5b7f71 6349921: (enum) Include links from java.lang.Enum to EnumSet and EnumMap
darcy
parents: 1948
diff changeset
    52
 * @see     java.util.EnumMap
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
22109
94e298e5a08b 8031210: Remove serial warning from java.lang.Enum
darcy
parents: 14342
diff changeset
    55
@SuppressWarnings("serial") // No serialVersionUID needed due to
94e298e5a08b 8031210: Remove serial warning from java.lang.Enum
darcy
parents: 14342
diff changeset
    56
                            // special-casing of enum types.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public abstract class Enum<E extends Enum<E>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        implements Comparable<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The name of this enum constant, as declared in the enum declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Most programmers should use the {@link #toString} method rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * accessing this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Returns the name of this enum constant, exactly as declared in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * enum declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <b>Most programmers should use the {@link #toString} method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * preference to this one, as the toString method may return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * a more user-friendly name.</b>  This method is designed primarily for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * use in specialized situations where correctness depends on getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * exact name, which will not vary from release to release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @return the name of this enum constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public final String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * The ordinal of this enumeration constant (its position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * in the enum declaration, where the initial constant is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * an ordinal of zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Most programmers will have no use for this field.  It is designed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * for use by sophisticated enum-based data structures, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private final int ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Returns the ordinal of this enumeration constant (its position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * in its enum declaration, where the initial constant is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * an ordinal of zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Most programmers will have no use for this method.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * designed for use by sophisticated enum-based data structures, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @return the ordinal of this enumeration constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public final int ordinal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Sole constructor.  Programmers cannot invoke this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * It is for use by code emitted by the compiler in response to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * enum type declarations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param name - The name of this enum constant, which is the identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *               used to declare it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param ordinal - The ordinal of this enumeration constant (its position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *         in the enum declaration, where the initial constant is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *         an ordinal of zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    protected Enum(String name, int ordinal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.ordinal = ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Returns the name of this enum constant, as contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * declaration.  This method may be overridden, though it typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * isn't necessary or desirable.  An enum type should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * method when a more "programmer-friendly" string form exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @return the name of this enum constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Returns true if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param other the object to be compared for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return  true if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *          enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public final boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return this==other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Returns a hash code for this enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return a hash code for this enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public final int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return super.hashCode();
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
     * Throws CloneNotSupportedException.  This guarantees that enums
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * are never cloned, which is necessary to preserve their "singleton"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return (never returns)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    protected final Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        throw new CloneNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Compares this enum with the specified object for order.  Returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * negative integer, zero, or a positive integer as this object is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * than, equal to, or greater than the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Enum constants are only comparable to other enum constants of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * same enum type.  The natural order implemented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * method is the order in which the constants are declared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public final int compareTo(E o) {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   178
        Enum<?> other = (Enum<?>)o;
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   179
        Enum<E> self = this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (self.getClass() != other.getClass() && // optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            self.getDeclaringClass() != other.getDeclaringClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return self.ordinal - other.ordinal;
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
     * Returns the Class object corresponding to this enum constant's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * enum type.  Two enum constants e1 and  e2 are of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * same enum type if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *   e1.getDeclaringClass() == e2.getDeclaringClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * (The value returned by this method may differ from the one returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * by the {@link Object#getClass} method for enum constants with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * constant-specific class bodies.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return the Class object corresponding to this enum constant's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *     enum type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   198
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public final Class<E> getDeclaringClass() {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   200
        Class<?> clazz = getClass();
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   201
        Class<?> zuper = clazz.getSuperclass();
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   202
        return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns the enum constant of the specified enum type with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * specified name.  The name must match exactly an identifier used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * to declare an enum constant in this type.  (Extraneous whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * characters are not permitted.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
1948
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   211
     * <p>Note that for a particular enum type {@code T}, the
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   212
     * implicitly declared {@code public static T valueOf(String)}
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   213
     * method on that enum may be used instead of this method to map
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   214
     * from a name to the corresponding enum constant.  All the
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   215
     * constants of an enum type can be obtained by calling the
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   216
     * implicit {@code public static T[] values()} method of that
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   217
     * type.
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   218
     *
5c23ac167e12 6548433: (enum spec) java.lang.Enum docs should explain about values() and valueOf(String)
darcy
parents: 1938
diff changeset
   219
     * @param <T> The enum type whose constant is to be returned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param enumType the {@code Class} object of the enum type from which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *      to return a constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param name the name of the constant to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return the enum constant of the specified enum type with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *      specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws IllegalArgumentException if the specified enum type has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *         no constant with the specified name, or the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *         class object does not represent an enum type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @throws NullPointerException if {@code enumType} or {@code name}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *         is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public static <T extends Enum<T>> T valueOf(Class<T> enumType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                                String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        T result = enumType.enumConstantDirectory().get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new NullPointerException("Name is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        throw new IllegalArgumentException(
1938
23526f724210 6327048: Enum javadoc could link to JLS
darcy
parents: 2
diff changeset
   240
            "No enum constant " + enumType.getCanonicalName() + "." + name);
2
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
     * enum classes cannot have finalize methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 25859
diff changeset
   246
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    protected final void finalize() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * prevent default deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private void readObject(ObjectInputStream in) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        ClassNotFoundException {
1938
23526f724210 6327048: Enum javadoc could link to JLS
darcy
parents: 2
diff changeset
   254
        throw new InvalidObjectException("can't deserialize enum");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private void readObjectNoData() throws ObjectStreamException {
1938
23526f724210 6327048: Enum javadoc could link to JLS
darcy
parents: 2
diff changeset
   258
        throw new InvalidObjectException("can't deserialize enum");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
}