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