jaroslav@68: /* jaroslav@68: * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved. jaroslav@68: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@68: * jaroslav@68: * This code is free software; you can redistribute it and/or modify it jaroslav@68: * under the terms of the GNU General Public License version 2 only, as jaroslav@68: * published by the Free Software Foundation. Oracle designates this jaroslav@68: * particular file as subject to the "Classpath" exception as provided jaroslav@68: * by Oracle in the LICENSE file that accompanied this code. jaroslav@68: * jaroslav@68: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@68: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@68: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@68: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@68: * accompanied this code). jaroslav@68: * jaroslav@68: * You should have received a copy of the GNU General Public License version jaroslav@68: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@68: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@68: * jaroslav@68: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@68: * or visit www.oracle.com if you need additional information or have any jaroslav@68: * questions. jaroslav@68: */ jaroslav@68: jaroslav@68: package java.lang; jaroslav@68: jaroslav@68: import java.io.Serializable; jaroslav@68: import java.io.IOException; jaroslav@68: jaroslav@68: /** jaroslav@68: * This is the common base class of all Java language enumeration types. jaroslav@68: * jaroslav@68: * More information about enums, including descriptions of the jaroslav@68: * implicitly declared methods synthesized by the compiler, can be jaroslav@68: * found in section 8.9 of jaroslav@68: * The Java™ Language Specification. jaroslav@68: * jaroslav@68: *

Note that when using an enumeration type as the type of a set jaroslav@68: * or as the type of the keys in a map, specialized and efficient jaroslav@68: * {@linkplain java.util.EnumSet set} and {@linkplain jaroslav@68: * java.util.EnumMap map} implementations are available. jaroslav@68: * jaroslav@68: * @param The enum type subclass jaroslav@68: * @author Josh Bloch jaroslav@68: * @author Neal Gafter jaroslav@68: * @see Class#getEnumConstants() jaroslav@68: * @see java.util.EnumSet jaroslav@68: * @see java.util.EnumMap jaroslav@68: * @since 1.5 jaroslav@68: */ jaroslav@68: public abstract class Enum> jaroslav@68: implements Comparable, Serializable { jaroslav@68: /** jaroslav@68: * The name of this enum constant, as declared in the enum declaration. jaroslav@68: * Most programmers should use the {@link #toString} method rather than jaroslav@68: * accessing this field. jaroslav@68: */ jaroslav@68: private final String name; jaroslav@68: jaroslav@68: /** jaroslav@68: * Returns the name of this enum constant, exactly as declared in its jaroslav@68: * enum declaration. jaroslav@68: * jaroslav@68: * Most programmers should use the {@link #toString} method in jaroslav@68: * preference to this one, as the toString method may return jaroslav@68: * a more user-friendly name. This method is designed primarily for jaroslav@68: * use in specialized situations where correctness depends on getting the jaroslav@68: * exact name, which will not vary from release to release. jaroslav@68: * jaroslav@68: * @return the name of this enum constant jaroslav@68: */ jaroslav@68: public final String name() { jaroslav@68: return name; jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * The ordinal of this enumeration constant (its position jaroslav@68: * in the enum declaration, where the initial constant is assigned jaroslav@68: * an ordinal of zero). jaroslav@68: * jaroslav@68: * Most programmers will have no use for this field. It is designed jaroslav@68: * for use by sophisticated enum-based data structures, such as jaroslav@68: * {@link java.util.EnumSet} and {@link java.util.EnumMap}. jaroslav@68: */ jaroslav@68: private final int ordinal; jaroslav@68: jaroslav@68: /** jaroslav@68: * Returns the ordinal of this enumeration constant (its position jaroslav@68: * in its enum declaration, where the initial constant is assigned jaroslav@68: * an ordinal of zero). jaroslav@68: * jaroslav@68: * Most programmers will have no use for this method. It is jaroslav@68: * designed for use by sophisticated enum-based data structures, such jaroslav@68: * as {@link java.util.EnumSet} and {@link java.util.EnumMap}. jaroslav@68: * jaroslav@68: * @return the ordinal of this enumeration constant jaroslav@68: */ jaroslav@68: public final int ordinal() { jaroslav@68: return ordinal; jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * Sole constructor. Programmers cannot invoke this constructor. jaroslav@68: * It is for use by code emitted by the compiler in response to jaroslav@68: * enum type declarations. jaroslav@68: * jaroslav@68: * @param name - The name of this enum constant, which is the identifier jaroslav@68: * used to declare it. jaroslav@68: * @param ordinal - The ordinal of this enumeration constant (its position jaroslav@68: * in the enum declaration, where the initial constant is assigned jaroslav@68: * an ordinal of zero). jaroslav@68: */ jaroslav@68: protected Enum(String name, int ordinal) { jaroslav@68: this.name = name; jaroslav@68: this.ordinal = ordinal; jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * Returns the name of this enum constant, as contained in the jaroslav@68: * declaration. This method may be overridden, though it typically jaroslav@68: * isn't necessary or desirable. An enum type should override this jaroslav@68: * method when a more "programmer-friendly" string form exists. jaroslav@68: * jaroslav@68: * @return the name of this enum constant jaroslav@68: */ jaroslav@68: public String toString() { jaroslav@68: return name; jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * Returns true if the specified object is equal to this jaroslav@68: * enum constant. jaroslav@68: * jaroslav@68: * @param other the object to be compared for equality with this object. jaroslav@68: * @return true if the specified object is equal to this jaroslav@68: * enum constant. jaroslav@68: */ jaroslav@68: public final boolean equals(Object other) { jaroslav@68: return this==other; jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * Returns a hash code for this enum constant. jaroslav@68: * jaroslav@68: * @return a hash code for this enum constant. jaroslav@68: */ jaroslav@68: public final int hashCode() { jaroslav@68: return super.hashCode(); jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * Throws CloneNotSupportedException. This guarantees that enums jaroslav@68: * are never cloned, which is necessary to preserve their "singleton" jaroslav@68: * status. jaroslav@68: * jaroslav@68: * @return (never returns) jaroslav@68: */ jaroslav@68: protected final Object clone() throws CloneNotSupportedException { jaroslav@68: throw new CloneNotSupportedException(); jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * Compares this enum with the specified object for order. Returns a jaroslav@68: * negative integer, zero, or a positive integer as this object is less jaroslav@68: * than, equal to, or greater than the specified object. jaroslav@68: * jaroslav@68: * Enum constants are only comparable to other enum constants of the jaroslav@68: * same enum type. The natural order implemented by this jaroslav@68: * method is the order in which the constants are declared. jaroslav@68: */ jaroslav@68: public final int compareTo(E o) { jaroslav@68: Enum other = (Enum)o; jaroslav@68: Enum self = this; jaroslav@68: if (self.getClass() != other.getClass() && // optimization jaroslav@68: self.getDeclaringClass() != other.getDeclaringClass()) jaroslav@68: throw new ClassCastException(); jaroslav@68: return self.ordinal - other.ordinal; jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * Returns the Class object corresponding to this enum constant's jaroslav@68: * enum type. Two enum constants e1 and e2 are of the jaroslav@68: * same enum type if and only if jaroslav@68: * e1.getDeclaringClass() == e2.getDeclaringClass(). jaroslav@68: * (The value returned by this method may differ from the one returned jaroslav@68: * by the {@link Object#getClass} method for enum constants with jaroslav@68: * constant-specific class bodies.) jaroslav@68: * jaroslav@68: * @return the Class object corresponding to this enum constant's jaroslav@68: * enum type jaroslav@68: */ jaroslav@68: public final Class getDeclaringClass() { jaroslav@68: Class clazz = getClass(); jaroslav@68: Class zuper = clazz.getSuperclass(); jaroslav@68: return (zuper == Enum.class) ? clazz : zuper; jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * Returns the enum constant of the specified enum type with the jaroslav@68: * specified name. The name must match exactly an identifier used jaroslav@68: * to declare an enum constant in this type. (Extraneous whitespace jaroslav@68: * characters are not permitted.) jaroslav@68: * jaroslav@68: *

Note that for a particular enum type {@code T}, the jaroslav@68: * implicitly declared {@code public static T valueOf(String)} jaroslav@68: * method on that enum may be used instead of this method to map jaroslav@68: * from a name to the corresponding enum constant. All the jaroslav@68: * constants of an enum type can be obtained by calling the jaroslav@68: * implicit {@code public static T[] values()} method of that jaroslav@68: * type. jaroslav@68: * jaroslav@68: * @param The enum type whose constant is to be returned jaroslav@68: * @param enumType the {@code Class} object of the enum type from which jaroslav@68: * to return a constant jaroslav@68: * @param name the name of the constant to return jaroslav@68: * @return the enum constant of the specified enum type with the jaroslav@68: * specified name jaroslav@68: * @throws IllegalArgumentException if the specified enum type has jaroslav@68: * no constant with the specified name, or the specified jaroslav@68: * class object does not represent an enum type jaroslav@68: * @throws NullPointerException if {@code enumType} or {@code name} jaroslav@68: * is null jaroslav@68: * @since 1.5 jaroslav@68: */ jaroslav@68: public static > T valueOf(Class enumType, jaroslav@68: String name) { jaroslav@84: throw new UnsupportedOperationException(); jaroslav@84: // T result = enumType.enumConstantDirectory().get(name); jaroslav@84: // if (result != null) jaroslav@84: // return result; jaroslav@84: // if (name == null) jaroslav@84: // throw new NullPointerException("Name is null"); jaroslav@84: // throw new IllegalArgumentException( jaroslav@84: // "No enum constant " + enumType.getCanonicalName() + "." + name); jaroslav@68: } jaroslav@68: jaroslav@68: /** jaroslav@68: * enum classes cannot have finalize methods. jaroslav@68: */ jaroslav@68: protected final void finalize() { } jaroslav@68: jaroslav@68: /** jaroslav@68: * prevent default deserialization jaroslav@68: */ jaroslav@84: // private void readObject(ObjectInputStream in) throws IOException, jaroslav@84: // ClassNotFoundException { jaroslav@84: // throw new InvalidObjectException("can't deserialize enum"); jaroslav@84: // } jaroslav@84: // jaroslav@84: // private void readObjectNoData() throws ObjectStreamException { jaroslav@84: // throw new InvalidObjectException("can't deserialize enum"); jaroslav@84: // } jaroslav@68: }