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