0N/A/*
2362N/A * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang;
0N/A
0N/Aimport java.io.Serializable;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InvalidObjectException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectStreamException;
0N/A
0N/A/**
0N/A * This is the common base class of all Java language enumeration types.
0N/A *
856N/A * More information about enums, including descriptions of the
856N/A * implicitly declared methods synthesized by the compiler, can be
4008N/A * found in section 8.9 of
4008N/A * <cite>The Java&trade; Language Specification</cite>.
846N/A *
1750N/A * <p> Note that when using an enumeration type as the type of a set
1750N/A * or as the type of the keys in a map, specialized and efficient
1750N/A * {@linkplain java.util.EnumSet set} and {@linkplain
1750N/A * java.util.EnumMap map} implementations are available.
1750N/A *
856N/A * @param <E> The enum type subclass
0N/A * @author Josh Bloch
0N/A * @author Neal Gafter
0N/A * @see Class#getEnumConstants()
1750N/A * @see java.util.EnumSet
1750N/A * @see java.util.EnumMap
0N/A * @since 1.5
0N/A */
0N/Apublic abstract class Enum<E extends Enum<E>>
0N/A implements Comparable<E>, Serializable {
0N/A /**
0N/A * The name of this enum constant, as declared in the enum declaration.
0N/A * Most programmers should use the {@link #toString} method rather than
0N/A * accessing this field.
0N/A */
0N/A private final String name;
0N/A
0N/A /**
0N/A * Returns the name of this enum constant, exactly as declared in its
0N/A * enum declaration.
0N/A *
0N/A * <b>Most programmers should use the {@link #toString} method in
0N/A * preference to this one, as the toString method may return
0N/A * a more user-friendly name.</b> This method is designed primarily for
0N/A * use in specialized situations where correctness depends on getting the
0N/A * exact name, which will not vary from release to release.
0N/A *
0N/A * @return the name of this enum constant
0N/A */
0N/A public final String name() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * The ordinal of this enumeration constant (its position
0N/A * in the enum declaration, where the initial constant is assigned
0N/A * an ordinal of zero).
0N/A *
0N/A * Most programmers will have no use for this field. It is designed
0N/A * for use by sophisticated enum-based data structures, such as
0N/A * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
0N/A */
0N/A private final int ordinal;
0N/A
0N/A /**
0N/A * Returns the ordinal of this enumeration constant (its position
0N/A * in its enum declaration, where the initial constant is assigned
0N/A * an ordinal of zero).
0N/A *
0N/A * Most programmers will have no use for this method. It is
0N/A * designed for use by sophisticated enum-based data structures, such
0N/A * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
0N/A *
0N/A * @return the ordinal of this enumeration constant
0N/A */
0N/A public final int ordinal() {
0N/A return ordinal;
0N/A }
0N/A
0N/A /**
0N/A * Sole constructor. Programmers cannot invoke this constructor.
0N/A * It is for use by code emitted by the compiler in response to
0N/A * enum type declarations.
0N/A *
0N/A * @param name - The name of this enum constant, which is the identifier
0N/A * used to declare it.
0N/A * @param ordinal - The ordinal of this enumeration constant (its position
0N/A * in the enum declaration, where the initial constant is assigned
0N/A * an ordinal of zero).
0N/A */
0N/A protected Enum(String name, int ordinal) {
0N/A this.name = name;
0N/A this.ordinal = ordinal;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of this enum constant, as contained in the
0N/A * declaration. This method may be overridden, though it typically
0N/A * isn't necessary or desirable. An enum type should override this
0N/A * method when a more "programmer-friendly" string form exists.
0N/A *
0N/A * @return the name of this enum constant
0N/A */
0N/A public String toString() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the specified object is equal to this
0N/A * enum constant.
0N/A *
0N/A * @param other the object to be compared for equality with this object.
0N/A * @return true if the specified object is equal to this
0N/A * enum constant.
0N/A */
0N/A public final boolean equals(Object other) {
0N/A return this==other;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash code for this enum constant.
0N/A *
0N/A * @return a hash code for this enum constant.
0N/A */
0N/A public final int hashCode() {
0N/A return super.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Throws CloneNotSupportedException. This guarantees that enums
0N/A * are never cloned, which is necessary to preserve their "singleton"
0N/A * status.
0N/A *
0N/A * @return (never returns)
0N/A */
0N/A protected final Object clone() throws CloneNotSupportedException {
0N/A throw new CloneNotSupportedException();
0N/A }
0N/A
0N/A /**
0N/A * Compares this enum with the specified object for order. Returns a
0N/A * negative integer, zero, or a positive integer as this object is less
0N/A * than, equal to, or greater than the specified object.
0N/A *
0N/A * Enum constants are only comparable to other enum constants of the
0N/A * same enum type. The natural order implemented by this
0N/A * method is the order in which the constants are declared.
0N/A */
0N/A public final int compareTo(E o) {
0N/A Enum other = (Enum)o;
0N/A Enum self = this;
0N/A if (self.getClass() != other.getClass() && // optimization
0N/A self.getDeclaringClass() != other.getDeclaringClass())
0N/A throw new ClassCastException();
0N/A return self.ordinal - other.ordinal;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Class object corresponding to this enum constant's
0N/A * enum type. Two enum constants e1 and e2 are of the
0N/A * same enum type if and only if
0N/A * e1.getDeclaringClass() == e2.getDeclaringClass().
0N/A * (The value returned by this method may differ from the one returned
0N/A * by the {@link Object#getClass} method for enum constants with
0N/A * constant-specific class bodies.)
0N/A *
0N/A * @return the Class object corresponding to this enum constant's
0N/A * enum type
0N/A */
0N/A public final Class<E> getDeclaringClass() {
0N/A Class clazz = getClass();
0N/A Class zuper = clazz.getSuperclass();
0N/A return (zuper == Enum.class) ? clazz : zuper;
0N/A }
0N/A
0N/A /**
0N/A * Returns the enum constant of the specified enum type with the
0N/A * specified name. The name must match exactly an identifier used
0N/A * to declare an enum constant in this type. (Extraneous whitespace
0N/A * characters are not permitted.)
0N/A *
856N/A * <p>Note that for a particular enum type {@code T}, the
856N/A * implicitly declared {@code public static T valueOf(String)}
856N/A * method on that enum may be used instead of this method to map
856N/A * from a name to the corresponding enum constant. All the
856N/A * constants of an enum type can be obtained by calling the
856N/A * implicit {@code public static T[] values()} method of that
856N/A * type.
856N/A *
856N/A * @param <T> The enum type whose constant is to be returned
0N/A * @param enumType the {@code Class} object of the enum type from which
0N/A * to return a constant
0N/A * @param name the name of the constant to return
0N/A * @return the enum constant of the specified enum type with the
0N/A * specified name
0N/A * @throws IllegalArgumentException if the specified enum type has
0N/A * no constant with the specified name, or the specified
0N/A * class object does not represent an enum type
0N/A * @throws NullPointerException if {@code enumType} or {@code name}
0N/A * is null
0N/A * @since 1.5
0N/A */
0N/A public static <T extends Enum<T>> T valueOf(Class<T> enumType,
0N/A String name) {
0N/A T result = enumType.enumConstantDirectory().get(name);
0N/A if (result != null)
0N/A return result;
0N/A if (name == null)
0N/A throw new NullPointerException("Name is null");
0N/A throw new IllegalArgumentException(
846N/A "No enum constant " + enumType.getCanonicalName() + "." + name);
0N/A }
0N/A
0N/A /**
0N/A * enum classes cannot have finalize methods.
0N/A */
0N/A protected final void finalize() { }
0N/A
0N/A /**
0N/A * prevent default deserialization
0N/A */
0N/A private void readObject(ObjectInputStream in) throws IOException,
0N/A ClassNotFoundException {
846N/A throw new InvalidObjectException("can't deserialize enum");
0N/A }
0N/A
0N/A private void readObjectNoData() throws ObjectStreamException {
846N/A throw new InvalidObjectException("can't deserialize enum");
0N/A }
0N/A}