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  * <p> Note that when using an enumeration type in as the type of a
  44  * set or as the type of the keys in a map, specialized {@linkplain
  45  * java.util.EnumSet set} and {@linkplain java.util.EnumMap map}
  46  * implementations are available.
  47  *
  48  * @param <E> The enum type subclass
  49  * @author  Josh Bloch
  50  * @author  Neal Gafter
  51  * @see     Class#getEnumConstants()
  52  * @see     java.util.EnumSet
  53  * @see     java.util.EnumMap
  54  * @since   1.5
  55  */
  56 public abstract class Enum<E extends Enum<E>>
  57         implements Comparable<E>, Serializable {
  58     /**
  59      * The name of this enum constant, as declared in the enum declaration.
  60      * Most programmers should use the {@link #toString} method rather than
  61      * accessing this field.
  62      */
  63     private final String name;
  64 
  65     /**
  66      * Returns the name of this enum constant, exactly as declared in its
  67      * enum declaration.
  68      *
  69      * <b>Most programmers should use the {@link #toString} method in
  70      * preference to this one, as the toString method may return
  71      * a more user-friendly name.</b>  This method is designed primarily for
  72      * use in specialized situations where correctness depends on getting the
  73      * exact name, which will not vary from release to release.
  74      *
  75      * @return the name of this enum constant
  76      */
  77     public final String name() {
  78         return name;
  79     }
  80 
  81     /**
  82      * The ordinal of this enumeration constant (its position
  83      * in the enum declaration, where the initial constant is assigned
  84      * an ordinal of zero).
  85      *
  86      * Most programmers will have no use for this field.  It is designed
  87      * for use by sophisticated enum-based data structures, such as
  88      * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
  89      */
  90     private final int ordinal;
  91 
  92     /**
  93      * Returns the ordinal of this enumeration constant (its position
  94      * in its enum declaration, where the initial constant is assigned
  95      * an ordinal of zero).
  96      *
  97      * Most programmers will have no use for this method.  It is
  98      * designed for use by sophisticated enum-based data structures, such
  99      * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
 100      *
 101      * @return the ordinal of this enumeration constant
 102      */
 103     public final int ordinal() {
 104         return ordinal;
 105     }
 106 
 107     /**
 108      * Sole constructor.  Programmers cannot invoke this constructor.
 109      * It is for use by code emitted by the compiler in response to
 110      * enum type declarations.
 111      *
 112      * @param name - The name of this enum constant, which is the identifier
 113      *               used to declare it.
 114      * @param ordinal - The ordinal of this enumeration constant (its position
 115      *         in the enum declaration, where the initial constant is assigned
 116      *         an ordinal of zero).
 117      */
 118     protected Enum(String name, int ordinal) {
 119         this.name = name;
 120         this.ordinal = ordinal;
 121     }
 122 
 123     /**
 124      * Returns the name of this enum constant, as contained in the
 125      * declaration.  This method may be overridden, though it typically
 126      * isn't necessary or desirable.  An enum type should override this
 127      * method when a more "programmer-friendly" string form exists.
 128      *
 129      * @return the name of this enum constant
 130      */
 131     public String toString() {
 132         return name;
 133     }
 134 
 135     /**
 136      * Returns true if the specified object is equal to this
 137      * enum constant.
 138      *
 139      * @param other the object to be compared for equality with this object.
 140      * @return  true if the specified object is equal to this
 141      *          enum constant.
 142      */
 143     public final boolean equals(Object other) {
 144         return this==other;
 145     }
 146 
 147     /**
 148      * Returns a hash code for this enum constant.
 149      *
 150      * @return a hash code for this enum constant.
 151      */
 152     public final int hashCode() {
 153         return super.hashCode();
 154     }
 155 
 156     /**
 157      * Throws CloneNotSupportedException.  This guarantees that enums
 158      * are never cloned, which is necessary to preserve their "singleton"
 159      * status.
 160      *
 161      * @return (never returns)
 162      */
 163     protected final Object clone() throws CloneNotSupportedException {
 164         throw new CloneNotSupportedException();
 165     }
 166 
 167     /**
 168      * Compares this enum with the specified object for order.  Returns a
 169      * negative integer, zero, or a positive integer as this object is less
 170      * than, equal to, or greater than the specified object.
 171      *
 172      * Enum constants are only comparable to other enum constants of the
 173      * same enum type.  The natural order implemented by this
 174      * method is the order in which the constants are declared.
 175      */
 176     public final int compareTo(E o) {
 177         Enum other = (Enum)o;
 178         Enum self = this;
 179         if (self.getClass() != other.getClass() && // optimization
 180             self.getDeclaringClass() != other.getDeclaringClass())
 181             throw new ClassCastException();
 182         return self.ordinal - other.ordinal;
 183     }
 184 
 185     /**
 186      * Returns the Class object corresponding to this enum constant's
 187      * enum type.  Two enum constants e1 and  e2 are of the
 188      * same enum type if and only if
 189      *   e1.getDeclaringClass() == e2.getDeclaringClass().
 190      * (The value returned by this method may differ from the one returned
 191      * by the {@link Object#getClass} method for enum constants with
 192      * constant-specific class bodies.)
 193      *
 194      * @return the Class object corresponding to this enum constant's
 195      *     enum type
 196      */
 197     public final Class<E> getDeclaringClass() {
 198         Class clazz = getClass();
 199         Class zuper = clazz.getSuperclass();
 200         return (zuper == Enum.class) ? clazz : zuper;
 201     }
 202 
 203     /**
 204      * Returns the enum constant of the specified enum type with the
 205      * specified name.  The name must match exactly an identifier used
 206      * to declare an enum constant in this type.  (Extraneous whitespace
 207      * characters are not permitted.)
 208      *
 209      * <p>Note that for a particular enum type {@code T}, the
 210      * implicitly declared {@code public static T valueOf(String)}
 211      * method on that enum may be used instead of this method to map
 212      * from a name to the corresponding enum constant.  All the
 213      * constants of an enum type can be obtained by calling the
 214      * implicit {@code public static T[] values()} method of that
 215      * type.
 216      *
 217      * @param <T> The enum type whose constant is to be returned
 218      * @param enumType the {@code Class} object of the enum type from which
 219      *      to return a constant
 220      * @param name the name of the constant to return
 221      * @return the enum constant of the specified enum type with the
 222      *      specified name
 223      * @throws IllegalArgumentException if the specified enum type has
 224      *         no constant with the specified name, or the specified
 225      *         class object does not represent an enum type
 226      * @throws NullPointerException if {@code enumType} or {@code name}
 227      *         is null
 228      * @since 1.5
 229      */
 230     public static <T extends Enum<T>> T valueOf(Class<T> enumType,
 231                                                 String name) {
 232         T result = enumType.enumConstantDirectory().get(name);
 233         if (result != null)
 234             return result;
 235         if (name == null)
 236             throw new NullPointerException("Name is null");
 237         throw new IllegalArgumentException(
 238             "No enum constant " + enumType.getCanonicalName() + "." + name);
 239     }
 240 
 241     /**
 242      * enum classes cannot have finalize methods.
 243      */
 244     protected final void finalize() { }
 245 
 246     /**
 247      * prevent default deserialization
 248      */
 249     private void readObject(ObjectInputStream in) throws IOException,
 250         ClassNotFoundException {
 251         throw new InvalidObjectException("can't deserialize enum");
 252     }
 253 
 254     private void readObjectNoData() throws ObjectStreamException {
 255         throw new InvalidObjectException("can't deserialize enum");
 256     }
 257 }