rt/emul/mini/src/main/java/java/lang/Enum.java
changeset 772 d382dacfd73f
parent 554 05224402145d
child 960 4887e22cb810
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/mini/src/main/java/java/lang/Enum.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,254 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang;
    1.30 +
    1.31 +import java.io.Serializable;
    1.32 +import java.io.IOException;
    1.33 +
    1.34 +/**
    1.35 + * This is the common base class of all Java language enumeration types.
    1.36 + *
    1.37 + * More information about enums, including descriptions of the
    1.38 + * implicitly declared methods synthesized by the compiler, can be
    1.39 + * found in section 8.9 of
    1.40 + * <cite>The Java&trade; Language Specification</cite>.
    1.41 + *
    1.42 + * <p> Note that when using an enumeration type as the type of a set
    1.43 + * or as the type of the keys in a map, specialized and efficient
    1.44 + * {@linkplain java.util.EnumSet set} and {@linkplain
    1.45 + * java.util.EnumMap map} implementations are available.
    1.46 + *
    1.47 + * @param <E> The enum type subclass
    1.48 + * @author  Josh Bloch
    1.49 + * @author  Neal Gafter
    1.50 + * @see     Class#getEnumConstants()
    1.51 + * @see     java.util.EnumSet
    1.52 + * @see     java.util.EnumMap
    1.53 + * @since   1.5
    1.54 + */
    1.55 +public abstract class Enum<E extends Enum<E>>
    1.56 +        implements Comparable<E>, Serializable {
    1.57 +    /**
    1.58 +     * The name of this enum constant, as declared in the enum declaration.
    1.59 +     * Most programmers should use the {@link #toString} method rather than
    1.60 +     * accessing this field.
    1.61 +     */
    1.62 +    private final String name;
    1.63 +
    1.64 +    /**
    1.65 +     * Returns the name of this enum constant, exactly as declared in its
    1.66 +     * enum declaration.
    1.67 +     *
    1.68 +     * <b>Most programmers should use the {@link #toString} method in
    1.69 +     * preference to this one, as the toString method may return
    1.70 +     * a more user-friendly name.</b>  This method is designed primarily for
    1.71 +     * use in specialized situations where correctness depends on getting the
    1.72 +     * exact name, which will not vary from release to release.
    1.73 +     *
    1.74 +     * @return the name of this enum constant
    1.75 +     */
    1.76 +    public final String name() {
    1.77 +        return name;
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * The ordinal of this enumeration constant (its position
    1.82 +     * in the enum declaration, where the initial constant is assigned
    1.83 +     * an ordinal of zero).
    1.84 +     *
    1.85 +     * Most programmers will have no use for this field.  It is designed
    1.86 +     * for use by sophisticated enum-based data structures, such as
    1.87 +     * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
    1.88 +     */
    1.89 +    private final int ordinal;
    1.90 +
    1.91 +    /**
    1.92 +     * Returns the ordinal of this enumeration constant (its position
    1.93 +     * in its enum declaration, where the initial constant is assigned
    1.94 +     * an ordinal of zero).
    1.95 +     *
    1.96 +     * Most programmers will have no use for this method.  It is
    1.97 +     * designed for use by sophisticated enum-based data structures, such
    1.98 +     * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
    1.99 +     *
   1.100 +     * @return the ordinal of this enumeration constant
   1.101 +     */
   1.102 +    public final int ordinal() {
   1.103 +        return ordinal;
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * Sole constructor.  Programmers cannot invoke this constructor.
   1.108 +     * It is for use by code emitted by the compiler in response to
   1.109 +     * enum type declarations.
   1.110 +     *
   1.111 +     * @param name - The name of this enum constant, which is the identifier
   1.112 +     *               used to declare it.
   1.113 +     * @param ordinal - The ordinal of this enumeration constant (its position
   1.114 +     *         in the enum declaration, where the initial constant is assigned
   1.115 +     *         an ordinal of zero).
   1.116 +     */
   1.117 +    protected Enum(String name, int ordinal) {
   1.118 +        this.name = name;
   1.119 +        this.ordinal = ordinal;
   1.120 +    }
   1.121 +
   1.122 +    /**
   1.123 +     * Returns the name of this enum constant, as contained in the
   1.124 +     * declaration.  This method may be overridden, though it typically
   1.125 +     * isn't necessary or desirable.  An enum type should override this
   1.126 +     * method when a more "programmer-friendly" string form exists.
   1.127 +     *
   1.128 +     * @return the name of this enum constant
   1.129 +     */
   1.130 +    public String toString() {
   1.131 +        return name;
   1.132 +    }
   1.133 +
   1.134 +    /**
   1.135 +     * Returns true if the specified object is equal to this
   1.136 +     * enum constant.
   1.137 +     *
   1.138 +     * @param other the object to be compared for equality with this object.
   1.139 +     * @return  true if the specified object is equal to this
   1.140 +     *          enum constant.
   1.141 +     */
   1.142 +    public final boolean equals(Object other) {
   1.143 +        return this==other;
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Returns a hash code for this enum constant.
   1.148 +     *
   1.149 +     * @return a hash code for this enum constant.
   1.150 +     */
   1.151 +    public final int hashCode() {
   1.152 +        return super.hashCode();
   1.153 +    }
   1.154 +
   1.155 +    /**
   1.156 +     * Throws CloneNotSupportedException.  This guarantees that enums
   1.157 +     * are never cloned, which is necessary to preserve their "singleton"
   1.158 +     * status.
   1.159 +     *
   1.160 +     * @return (never returns)
   1.161 +     */
   1.162 +    protected final Object clone() throws CloneNotSupportedException {
   1.163 +        throw new CloneNotSupportedException();
   1.164 +    }
   1.165 +
   1.166 +    /**
   1.167 +     * Compares this enum with the specified object for order.  Returns a
   1.168 +     * negative integer, zero, or a positive integer as this object is less
   1.169 +     * than, equal to, or greater than the specified object.
   1.170 +     *
   1.171 +     * Enum constants are only comparable to other enum constants of the
   1.172 +     * same enum type.  The natural order implemented by this
   1.173 +     * method is the order in which the constants are declared.
   1.174 +     */
   1.175 +    public final int compareTo(E o) {
   1.176 +        Enum other = (Enum)o;
   1.177 +        Enum self = this;
   1.178 +        if (self.getClass() != other.getClass() && // optimization
   1.179 +            self.getDeclaringClass() != other.getDeclaringClass())
   1.180 +            throw new ClassCastException();
   1.181 +        return self.ordinal - other.ordinal;
   1.182 +    }
   1.183 +
   1.184 +    /**
   1.185 +     * Returns the Class object corresponding to this enum constant's
   1.186 +     * enum type.  Two enum constants e1 and  e2 are of the
   1.187 +     * same enum type if and only if
   1.188 +     *   e1.getDeclaringClass() == e2.getDeclaringClass().
   1.189 +     * (The value returned by this method may differ from the one returned
   1.190 +     * by the {@link Object#getClass} method for enum constants with
   1.191 +     * constant-specific class bodies.)
   1.192 +     *
   1.193 +     * @return the Class object corresponding to this enum constant's
   1.194 +     *     enum type
   1.195 +     */
   1.196 +    public final Class<E> getDeclaringClass() {
   1.197 +        Class clazz = getClass();
   1.198 +        Class zuper = clazz.getSuperclass();
   1.199 +        return (zuper == Enum.class) ? clazz : zuper;
   1.200 +    }
   1.201 +
   1.202 +    /**
   1.203 +     * Returns the enum constant of the specified enum type with the
   1.204 +     * specified name.  The name must match exactly an identifier used
   1.205 +     * to declare an enum constant in this type.  (Extraneous whitespace
   1.206 +     * characters are not permitted.)
   1.207 +     *
   1.208 +     * <p>Note that for a particular enum type {@code T}, the
   1.209 +     * implicitly declared {@code public static T valueOf(String)}
   1.210 +     * method on that enum may be used instead of this method to map
   1.211 +     * from a name to the corresponding enum constant.  All the
   1.212 +     * constants of an enum type can be obtained by calling the
   1.213 +     * implicit {@code public static T[] values()} method of that
   1.214 +     * type.
   1.215 +     *
   1.216 +     * @param <T> The enum type whose constant is to be returned
   1.217 +     * @param enumType the {@code Class} object of the enum type from which
   1.218 +     *      to return a constant
   1.219 +     * @param name the name of the constant to return
   1.220 +     * @return the enum constant of the specified enum type with the
   1.221 +     *      specified name
   1.222 +     * @throws IllegalArgumentException if the specified enum type has
   1.223 +     *         no constant with the specified name, or the specified
   1.224 +     *         class object does not represent an enum type
   1.225 +     * @throws NullPointerException if {@code enumType} or {@code name}
   1.226 +     *         is null
   1.227 +     * @since 1.5
   1.228 +     */
   1.229 +    public static <T extends Enum<T>> T valueOf(Class<T> enumType,
   1.230 +                                                String name) {
   1.231 +        throw new UnsupportedOperationException();
   1.232 +//        T result = enumType.enumConstantDirectory().get(name);
   1.233 +//        if (result != null)
   1.234 +//            return result;
   1.235 +//        if (name == null)
   1.236 +//            throw new NullPointerException("Name is null");
   1.237 +//        throw new IllegalArgumentException(
   1.238 +//            "No enum constant " + enumType.getCanonicalName() + "." + name);
   1.239 +    }
   1.240 +
   1.241 +    /**
   1.242 +     * enum classes cannot have finalize methods.
   1.243 +     */
   1.244 +    protected final void finalize() { }
   1.245 +
   1.246 +    /**
   1.247 +     * prevent default deserialization
   1.248 +     */
   1.249 +//    private void readObject(ObjectInputStream in) throws IOException,
   1.250 +//        ClassNotFoundException {
   1.251 +//        throw new InvalidObjectException("can't deserialize enum");
   1.252 +//    }
   1.253 +//
   1.254 +//    private void readObjectNoData() throws ObjectStreamException {
   1.255 +//        throw new InvalidObjectException("can't deserialize enum");
   1.256 +//    }
   1.257 +}