Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/lang/Enum.java
38829 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627import java.io.Serializable;28import java.io.IOException;29import java.io.InvalidObjectException;30import java.io.ObjectInputStream;31import java.io.ObjectStreamException;3233/**34* This is the common base class of all Java language enumeration types.35*36* More information about enums, including descriptions of the37* implicitly declared methods synthesized by the compiler, can be38* found in section 8.9 of39* <cite>The Java™ Language Specification</cite>.40*41* <p> Note that when using an enumeration type as the type of a set42* or as the type of the keys in a map, specialized and efficient43* {@linkplain java.util.EnumSet set} and {@linkplain44* java.util.EnumMap map} implementations are available.45*46* @param <E> The enum type subclass47* @author Josh Bloch48* @author Neal Gafter49* @see Class#getEnumConstants()50* @see java.util.EnumSet51* @see java.util.EnumMap52* @since 1.553*/54public abstract class Enum<E extends Enum<E>>55implements Comparable<E>, Serializable {56/**57* The name of this enum constant, as declared in the enum declaration.58* Most programmers should use the {@link #toString} method rather than59* accessing this field.60*/61private final String name;6263/**64* Returns the name of this enum constant, exactly as declared in its65* enum declaration.66*67* <b>Most programmers should use the {@link #toString} method in68* preference to this one, as the toString method may return69* a more user-friendly name.</b> This method is designed primarily for70* use in specialized situations where correctness depends on getting the71* exact name, which will not vary from release to release.72*73* @return the name of this enum constant74*/75public final String name() {76return name;77}7879/**80* The ordinal of this enumeration constant (its position81* in the enum declaration, where the initial constant is assigned82* an ordinal of zero).83*84* Most programmers will have no use for this field. It is designed85* for use by sophisticated enum-based data structures, such as86* {@link java.util.EnumSet} and {@link java.util.EnumMap}.87*/88private final int ordinal;8990/**91* Returns the ordinal of this enumeration constant (its position92* in its enum declaration, where the initial constant is assigned93* an ordinal of zero).94*95* Most programmers will have no use for this method. It is96* designed for use by sophisticated enum-based data structures, such97* as {@link java.util.EnumSet} and {@link java.util.EnumMap}.98*99* @return the ordinal of this enumeration constant100*/101public final int ordinal() {102return ordinal;103}104105/**106* Sole constructor. Programmers cannot invoke this constructor.107* It is for use by code emitted by the compiler in response to108* enum type declarations.109*110* @param name - The name of this enum constant, which is the identifier111* used to declare it.112* @param ordinal - The ordinal of this enumeration constant (its position113* in the enum declaration, where the initial constant is assigned114* an ordinal of zero).115*/116protected Enum(String name, int ordinal) {117this.name = name;118this.ordinal = ordinal;119}120121/**122* Returns the name of this enum constant, as contained in the123* declaration. This method may be overridden, though it typically124* isn't necessary or desirable. An enum type should override this125* method when a more "programmer-friendly" string form exists.126*127* @return the name of this enum constant128*/129public String toString() {130return name;131}132133/**134* Returns true if the specified object is equal to this135* enum constant.136*137* @param other the object to be compared for equality with this object.138* @return true if the specified object is equal to this139* enum constant.140*/141public final boolean equals(Object other) {142return this==other;143}144145/**146* Returns a hash code for this enum constant.147*148* @return a hash code for this enum constant.149*/150public final int hashCode() {151return super.hashCode();152}153154/**155* Throws CloneNotSupportedException. This guarantees that enums156* are never cloned, which is necessary to preserve their "singleton"157* status.158*159* @return (never returns)160*/161protected final Object clone() throws CloneNotSupportedException {162throw new CloneNotSupportedException();163}164165/**166* Compares this enum with the specified object for order. Returns a167* negative integer, zero, or a positive integer as this object is less168* than, equal to, or greater than the specified object.169*170* Enum constants are only comparable to other enum constants of the171* same enum type. The natural order implemented by this172* method is the order in which the constants are declared.173*/174public final int compareTo(E o) {175Enum<?> other = (Enum<?>)o;176Enum<E> self = this;177if (self.getClass() != other.getClass() && // optimization178self.getDeclaringClass() != other.getDeclaringClass())179throw new ClassCastException();180return self.ordinal - other.ordinal;181}182183/**184* Returns the Class object corresponding to this enum constant's185* enum type. Two enum constants e1 and e2 are of the186* same enum type if and only if187* e1.getDeclaringClass() == e2.getDeclaringClass().188* (The value returned by this method may differ from the one returned189* by the {@link Object#getClass} method for enum constants with190* constant-specific class bodies.)191*192* @return the Class object corresponding to this enum constant's193* enum type194*/195@SuppressWarnings("unchecked")196public final Class<E> getDeclaringClass() {197Class<?> clazz = getClass();198Class<?> zuper = clazz.getSuperclass();199return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;200}201202/**203* Returns the enum constant of the specified enum type with the204* specified name. The name must match exactly an identifier used205* to declare an enum constant in this type. (Extraneous whitespace206* characters are not permitted.)207*208* <p>Note that for a particular enum type {@code T}, the209* implicitly declared {@code public static T valueOf(String)}210* method on that enum may be used instead of this method to map211* from a name to the corresponding enum constant. All the212* constants of an enum type can be obtained by calling the213* implicit {@code public static T[] values()} method of that214* type.215*216* @param <T> The enum type whose constant is to be returned217* @param enumType the {@code Class} object of the enum type from which218* to return a constant219* @param name the name of the constant to return220* @return the enum constant of the specified enum type with the221* specified name222* @throws IllegalArgumentException if the specified enum type has223* no constant with the specified name, or the specified224* class object does not represent an enum type225* @throws NullPointerException if {@code enumType} or {@code name}226* is null227* @since 1.5228*/229public static <T extends Enum<T>> T valueOf(Class<T> enumType,230String name) {231T result = enumType.enumConstantDirectory().get(name);232if (result != null)233return result;234if (name == null)235throw new NullPointerException("Name is null");236throw new IllegalArgumentException(237"No enum constant " + enumType.getCanonicalName() + "." + name);238}239240/**241* enum classes cannot have finalize methods.242*/243protected final void finalize() { }244245/**246* prevent default deserialization247*/248private void readObject(ObjectInputStream in) throws IOException,249ClassNotFoundException {250throw new InvalidObjectException("can't deserialize enum");251}252253private void readObjectNoData() throws ObjectStreamException {254throw new InvalidObjectException("can't deserialize enum");255}256}257258259