jdk/src/share/classes/java/lang/Enum.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 1938 23526f724210
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see     Class#getEnumConstants()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public abstract class Enum<E extends Enum<E>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        implements Comparable<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * The name of this enum constant, as declared in the enum declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Most programmers should use the {@link #toString} method rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * accessing this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Returns the name of this enum constant, exactly as declared in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * enum declaration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <b>Most programmers should use the {@link #toString} method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * preference to this one, as the toString method may return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * a more user-friendly name.</b>  This method is designed primarily for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * use in specialized situations where correctness depends on getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * exact name, which will not vary from release to release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @return the name of this enum constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public final String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The ordinal of this enumeration constant (its position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * in the enum declaration, where the initial constant is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * an ordinal of zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Most programmers will have no use for this field.  It is designed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * for use by sophisticated enum-based data structures, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final int ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Returns the ordinal of this enumeration constant (its position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * in its enum declaration, where the initial constant is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * an ordinal of zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Most programmers will have no use for this method.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * designed for use by sophisticated enum-based data structures, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return the ordinal of this enumeration constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public final int ordinal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Sole constructor.  Programmers cannot invoke this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * It is for use by code emitted by the compiler in response to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * enum type declarations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param name - The name of this enum constant, which is the identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *               used to declare it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param ordinal - The ordinal of this enumeration constant (its position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *         in the enum declaration, where the initial constant is assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *         an ordinal of zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected Enum(String name, int ordinal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.ordinal = ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns the name of this enum constant, as contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * declaration.  This method may be overridden, though it typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * isn't necessary or desirable.  An enum type should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * method when a more "programmer-friendly" string form exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the name of this enum constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return name;
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
     * Returns true if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param other the object to be compared for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @return  true if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *          enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public final boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return this==other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Returns a hash code for this enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return a hash code for this enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public final int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return super.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Throws CloneNotSupportedException.  This guarantees that enums
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * are never cloned, which is necessary to preserve their "singleton"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return (never returns)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected final Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        throw new CloneNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Compares this enum with the specified object for order.  Returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * negative integer, zero, or a positive integer as this object is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * than, equal to, or greater than the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Enum constants are only comparable to other enum constants of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * same enum type.  The natural order implemented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * method is the order in which the constants are declared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public final int compareTo(E o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        Enum other = (Enum)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        Enum self = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (self.getClass() != other.getClass() && // optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            self.getDeclaringClass() != other.getDeclaringClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return self.ordinal - other.ordinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Returns the Class object corresponding to this enum constant's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * enum type.  Two enum constants e1 and  e2 are of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * same enum type if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *   e1.getDeclaringClass() == e2.getDeclaringClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * (The value returned by this method may differ from the one returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * by the {@link Object#getClass} method for enum constants with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * constant-specific class bodies.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return the Class object corresponding to this enum constant's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *     enum type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public final Class<E> getDeclaringClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Class clazz = getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Class zuper = clazz.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return (zuper == Enum.class) ? clazz : zuper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Returns the enum constant of the specified enum type with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * specified name.  The name must match exactly an identifier used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * to declare an enum constant in this type.  (Extraneous whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * characters are not permitted.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param enumType the {@code Class} object of the enum type from which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *      to return a constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param name the name of the constant to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return the enum constant of the specified enum type with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *      specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @throws IllegalArgumentException if the specified enum type has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *         no constant with the specified name, or the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *         class object does not represent an enum type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @throws NullPointerException if {@code enumType} or {@code name}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *         is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public static <T extends Enum<T>> T valueOf(Class<T> enumType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                                String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        T result = enumType.enumConstantDirectory().get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new NullPointerException("Name is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            "No enum const " + enumType +"." + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * enum classes cannot have finalize methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    protected final void finalize() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * prevent default deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private void readObject(ObjectInputStream in) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new InvalidObjectException("can't deserialize enum");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private void readObjectNoData() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new InvalidObjectException("can't deserialize enum");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
}