rt/emul/mini/src/main/java/java/lang/Enum.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 554 emul/mini/src/main/java/java/lang/Enum.java@05224402145d
child 960 4887e22cb810
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     1 /*
     2  * Copyright (c) 2003, 2009, Oracle and/or its affiliates. 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang;
    27 
    28 import java.io.Serializable;
    29 import java.io.IOException;
    30 
    31 /**
    32  * This is the common base class of all Java language enumeration types.
    33  *
    34  * More information about enums, including descriptions of the
    35  * implicitly declared methods synthesized by the compiler, can be
    36  * found in section 8.9 of
    37  * <cite>The Java&trade; Language Specification</cite>.
    38  *
    39  * <p> Note that when using an enumeration type as the type of a set
    40  * or as the type of the keys in a map, specialized and efficient
    41  * {@linkplain java.util.EnumSet set} and {@linkplain
    42  * java.util.EnumMap map} implementations are available.
    43  *
    44  * @param <E> The enum type subclass
    45  * @author  Josh Bloch
    46  * @author  Neal Gafter
    47  * @see     Class#getEnumConstants()
    48  * @see     java.util.EnumSet
    49  * @see     java.util.EnumMap
    50  * @since   1.5
    51  */
    52 public abstract class Enum<E extends Enum<E>>
    53         implements Comparable<E>, Serializable {
    54     /**
    55      * The name of this enum constant, as declared in the enum declaration.
    56      * Most programmers should use the {@link #toString} method rather than
    57      * accessing this field.
    58      */
    59     private final String name;
    60 
    61     /**
    62      * Returns the name of this enum constant, exactly as declared in its
    63      * enum declaration.
    64      *
    65      * <b>Most programmers should use the {@link #toString} method in
    66      * preference to this one, as the toString method may return
    67      * a more user-friendly name.</b>  This method is designed primarily for
    68      * use in specialized situations where correctness depends on getting the
    69      * exact name, which will not vary from release to release.
    70      *
    71      * @return the name of this enum constant
    72      */
    73     public final String name() {
    74         return name;
    75     }
    76 
    77     /**
    78      * The ordinal of this enumeration constant (its position
    79      * in the enum declaration, where the initial constant is assigned
    80      * an ordinal of zero).
    81      *
    82      * Most programmers will have no use for this field.  It is designed
    83      * for use by sophisticated enum-based data structures, such as
    84      * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
    85      */
    86     private final int ordinal;
    87 
    88     /**
    89      * Returns the ordinal of this enumeration constant (its position
    90      * in its enum declaration, where the initial constant is assigned
    91      * an ordinal of zero).
    92      *
    93      * Most programmers will have no use for this method.  It is
    94      * designed for use by sophisticated enum-based data structures, such
    95      * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
    96      *
    97      * @return the ordinal of this enumeration constant
    98      */
    99     public final int ordinal() {
   100         return ordinal;
   101     }
   102 
   103     /**
   104      * Sole constructor.  Programmers cannot invoke this constructor.
   105      * It is for use by code emitted by the compiler in response to
   106      * enum type declarations.
   107      *
   108      * @param name - The name of this enum constant, which is the identifier
   109      *               used to declare it.
   110      * @param ordinal - The ordinal of this enumeration constant (its position
   111      *         in the enum declaration, where the initial constant is assigned
   112      *         an ordinal of zero).
   113      */
   114     protected Enum(String name, int ordinal) {
   115         this.name = name;
   116         this.ordinal = ordinal;
   117     }
   118 
   119     /**
   120      * Returns the name of this enum constant, as contained in the
   121      * declaration.  This method may be overridden, though it typically
   122      * isn't necessary or desirable.  An enum type should override this
   123      * method when a more "programmer-friendly" string form exists.
   124      *
   125      * @return the name of this enum constant
   126      */
   127     public String toString() {
   128         return name;
   129     }
   130 
   131     /**
   132      * Returns true if the specified object is equal to this
   133      * enum constant.
   134      *
   135      * @param other the object to be compared for equality with this object.
   136      * @return  true if the specified object is equal to this
   137      *          enum constant.
   138      */
   139     public final boolean equals(Object other) {
   140         return this==other;
   141     }
   142 
   143     /**
   144      * Returns a hash code for this enum constant.
   145      *
   146      * @return a hash code for this enum constant.
   147      */
   148     public final int hashCode() {
   149         return super.hashCode();
   150     }
   151 
   152     /**
   153      * Throws CloneNotSupportedException.  This guarantees that enums
   154      * are never cloned, which is necessary to preserve their "singleton"
   155      * status.
   156      *
   157      * @return (never returns)
   158      */
   159     protected final Object clone() throws CloneNotSupportedException {
   160         throw new CloneNotSupportedException();
   161     }
   162 
   163     /**
   164      * Compares this enum with the specified object for order.  Returns a
   165      * negative integer, zero, or a positive integer as this object is less
   166      * than, equal to, or greater than the specified object.
   167      *
   168      * Enum constants are only comparable to other enum constants of the
   169      * same enum type.  The natural order implemented by this
   170      * method is the order in which the constants are declared.
   171      */
   172     public final int compareTo(E o) {
   173         Enum other = (Enum)o;
   174         Enum self = this;
   175         if (self.getClass() != other.getClass() && // optimization
   176             self.getDeclaringClass() != other.getDeclaringClass())
   177             throw new ClassCastException();
   178         return self.ordinal - other.ordinal;
   179     }
   180 
   181     /**
   182      * Returns the Class object corresponding to this enum constant's
   183      * enum type.  Two enum constants e1 and  e2 are of the
   184      * same enum type if and only if
   185      *   e1.getDeclaringClass() == e2.getDeclaringClass().
   186      * (The value returned by this method may differ from the one returned
   187      * by the {@link Object#getClass} method for enum constants with
   188      * constant-specific class bodies.)
   189      *
   190      * @return the Class object corresponding to this enum constant's
   191      *     enum type
   192      */
   193     public final Class<E> getDeclaringClass() {
   194         Class clazz = getClass();
   195         Class zuper = clazz.getSuperclass();
   196         return (zuper == Enum.class) ? clazz : zuper;
   197     }
   198 
   199     /**
   200      * Returns the enum constant of the specified enum type with the
   201      * specified name.  The name must match exactly an identifier used
   202      * to declare an enum constant in this type.  (Extraneous whitespace
   203      * characters are not permitted.)
   204      *
   205      * <p>Note that for a particular enum type {@code T}, the
   206      * implicitly declared {@code public static T valueOf(String)}
   207      * method on that enum may be used instead of this method to map
   208      * from a name to the corresponding enum constant.  All the
   209      * constants of an enum type can be obtained by calling the
   210      * implicit {@code public static T[] values()} method of that
   211      * type.
   212      *
   213      * @param <T> The enum type whose constant is to be returned
   214      * @param enumType the {@code Class} object of the enum type from which
   215      *      to return a constant
   216      * @param name the name of the constant to return
   217      * @return the enum constant of the specified enum type with the
   218      *      specified name
   219      * @throws IllegalArgumentException if the specified enum type has
   220      *         no constant with the specified name, or the specified
   221      *         class object does not represent an enum type
   222      * @throws NullPointerException if {@code enumType} or {@code name}
   223      *         is null
   224      * @since 1.5
   225      */
   226     public static <T extends Enum<T>> T valueOf(Class<T> enumType,
   227                                                 String name) {
   228         throw new UnsupportedOperationException();
   229 //        T result = enumType.enumConstantDirectory().get(name);
   230 //        if (result != null)
   231 //            return result;
   232 //        if (name == null)
   233 //            throw new NullPointerException("Name is null");
   234 //        throw new IllegalArgumentException(
   235 //            "No enum constant " + enumType.getCanonicalName() + "." + name);
   236     }
   237 
   238     /**
   239      * enum classes cannot have finalize methods.
   240      */
   241     protected final void finalize() { }
   242 
   243     /**
   244      * prevent default deserialization
   245      */
   246 //    private void readObject(ObjectInputStream in) throws IOException,
   247 //        ClassNotFoundException {
   248 //        throw new InvalidObjectException("can't deserialize enum");
   249 //    }
   250 //
   251 //    private void readObjectNoData() throws ObjectStreamException {
   252 //        throw new InvalidObjectException("can't deserialize enum");
   253 //    }
   254 }