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