rt/emul/mini/src/main/java/java/lang/Enum.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 09 Jun 2014 18:21:37 +0200
changeset 1621 dd848ea32287
parent 1561 f6200b8decc4
child 1623 d9cdfd8ef694
permissions -rw-r--r--
Another two cases where it is safer to check for name of superclass, rather than equality. rt/emul/compact test randomly failed.
     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 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    31 
    32 /**
    33  * This is the common base class of all Java language enumeration types.
    34  *
    35  * More information about enums, including descriptions of the
    36  * implicitly declared methods synthesized by the compiler, can be
    37  * found in section 8.9 of
    38  * <cite>The Java&trade; Language Specification</cite>.
    39  *
    40  * <p> Note that when using an enumeration type as the type of a set
    41  * or as the type of the keys in a map, specialized and efficient
    42  * {@linkplain java.util.EnumSet set} and {@linkplain
    43  * java.util.EnumMap map} implementations are available.
    44  *
    45  * @param <E> The enum type subclass
    46  * @author  Josh Bloch
    47  * @author  Neal Gafter
    48  * @see     Class#getEnumConstants()
    49  * @see     java.util.EnumSet
    50  * @see     java.util.EnumMap
    51  * @since   1.5
    52  */
    53 public abstract class Enum<E extends Enum<E>>
    54         implements Comparable<E>, Serializable {
    55     /**
    56      * The name of this enum constant, as declared in the enum declaration.
    57      * Most programmers should use the {@link #toString} method rather than
    58      * accessing this field.
    59      */
    60     private final String name;
    61 
    62     /**
    63      * Returns the name of this enum constant, exactly as declared in its
    64      * enum declaration.
    65      *
    66      * <b>Most programmers should use the {@link #toString} method in
    67      * preference to this one, as the toString method may return
    68      * a more user-friendly name.</b>  This method is designed primarily for
    69      * use in specialized situations where correctness depends on getting the
    70      * exact name, which will not vary from release to release.
    71      *
    72      * @return the name of this enum constant
    73      */
    74     public final String name() {
    75         return name;
    76     }
    77 
    78     /**
    79      * The ordinal of this enumeration constant (its position
    80      * in the enum declaration, where the initial constant is assigned
    81      * an ordinal of zero).
    82      *
    83      * Most programmers will have no use for this field.  It is designed
    84      * for use by sophisticated enum-based data structures, such as
    85      * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
    86      */
    87     private final int ordinal;
    88 
    89     /**
    90      * Returns the ordinal of this enumeration constant (its position
    91      * in its enum declaration, where the initial constant is assigned
    92      * an ordinal of zero).
    93      *
    94      * Most programmers will have no use for this method.  It is
    95      * designed for use by sophisticated enum-based data structures, such
    96      * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
    97      *
    98      * @return the ordinal of this enumeration constant
    99      */
   100     public final int ordinal() {
   101         return ordinal;
   102     }
   103 
   104     /**
   105      * Sole constructor.  Programmers cannot invoke this constructor.
   106      * It is for use by code emitted by the compiler in response to
   107      * enum type declarations.
   108      *
   109      * @param name - The name of this enum constant, which is the identifier
   110      *               used to declare it.
   111      * @param ordinal - The ordinal of this enumeration constant (its position
   112      *         in the enum declaration, where the initial constant is assigned
   113      *         an ordinal of zero).
   114      */
   115     protected Enum(String name, int ordinal) {
   116         this.name = name;
   117         this.ordinal = ordinal;
   118     }
   119 
   120     /**
   121      * Returns the name of this enum constant, as contained in the
   122      * declaration.  This method may be overridden, though it typically
   123      * isn't necessary or desirable.  An enum type should override this
   124      * method when a more "programmer-friendly" string form exists.
   125      *
   126      * @return the name of this enum constant
   127      */
   128     public String toString() {
   129         return name;
   130     }
   131 
   132     /**
   133      * Returns true if the specified object is equal to this
   134      * enum constant.
   135      *
   136      * @param other the object to be compared for equality with this object.
   137      * @return  true if the specified object is equal to this
   138      *          enum constant.
   139      */
   140     public final boolean equals(Object other) {
   141         return this==other;
   142     }
   143 
   144     /**
   145      * Returns a hash code for this enum constant.
   146      *
   147      * @return a hash code for this enum constant.
   148      */
   149     public final int hashCode() {
   150         return super.hashCode();
   151     }
   152 
   153     /**
   154      * Throws CloneNotSupportedException.  This guarantees that enums
   155      * are never cloned, which is necessary to preserve their "singleton"
   156      * status.
   157      *
   158      * @return (never returns)
   159      */
   160     protected final Object clone() throws CloneNotSupportedException {
   161         throw new CloneNotSupportedException();
   162     }
   163 
   164     /**
   165      * Compares this enum with the specified object for order.  Returns a
   166      * negative integer, zero, or a positive integer as this object is less
   167      * than, equal to, or greater than the specified object.
   168      *
   169      * Enum constants are only comparable to other enum constants of the
   170      * same enum type.  The natural order implemented by this
   171      * method is the order in which the constants are declared.
   172      */
   173     public final int compareTo(E o) {
   174         Enum other = (Enum)o;
   175         Enum self = this;
   176         if (self.getClass() != other.getClass() && // optimization
   177             self.getDeclaringClass() != other.getDeclaringClass())
   178             throw new ClassCastException();
   179         return self.ordinal - other.ordinal;
   180     }
   181 
   182     /**
   183      * Returns the Class object corresponding to this enum constant's
   184      * enum type.  Two enum constants e1 and  e2 are of the
   185      * same enum type if and only if
   186      *   e1.getDeclaringClass() == e2.getDeclaringClass().
   187      * (The value returned by this method may differ from the one returned
   188      * by the {@link Object#getClass} method for enum constants with
   189      * constant-specific class bodies.)
   190      *
   191      * @return the Class object corresponding to this enum constant's
   192      *     enum type
   193      */
   194     public final Class<E> getDeclaringClass() {
   195         Class clazz = getClass();
   196         Class zuper = clazz.getSuperclass();
   197         return zuper.getName().equals("java.lang.Enum") ? clazz : zuper;
   198     }
   199 
   200     /**
   201      * Returns the enum constant of the specified enum type with the
   202      * specified name.  The name must match exactly an identifier used
   203      * to declare an enum constant in this type.  (Extraneous whitespace
   204      * characters are not permitted.)
   205      *
   206      * <p>Note that for a particular enum type {@code T}, the
   207      * implicitly declared {@code public static T valueOf(String)}
   208      * method on that enum may be used instead of this method to map
   209      * from a name to the corresponding enum constant.  All the
   210      * constants of an enum type can be obtained by calling the
   211      * implicit {@code public static T[] values()} method of that
   212      * type.
   213      *
   214      * @param <T> The enum type whose constant is to be returned
   215      * @param enumType the {@code Class} object of the enum type from which
   216      *      to return a constant
   217      * @param name the name of the constant to return
   218      * @return the enum constant of the specified enum type with the
   219      *      specified name
   220      * @throws IllegalArgumentException if the specified enum type has
   221      *         no constant with the specified name, or the specified
   222      *         class object does not represent an enum type
   223      * @throws NullPointerException if {@code enumType} or {@code name}
   224      *         is null
   225      * @since 1.5
   226      */
   227     public static <T extends Enum<T>> T valueOf(Class<T> enumType,
   228                                                 String name) {
   229         for (Object o : values(enumType)) {
   230             T t = enumType.cast(o);
   231             if (name.equals(((Enum)t).name)) {
   232                 return t;
   233             }
   234         }
   235         throw new IllegalArgumentException();
   236     }
   237     
   238     @JavaScriptBody(args = { "enumType" }, body = "return enumType.cnstr.prototype['_$VALUES']();")
   239     private static native Object[] values(Class<?> enumType);
   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 }