151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project 351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.lang; 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.Serializable; 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.IOException; 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.InvalidObjectException; 3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectInputStream; 3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectStreamException; 34de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebskiimport java.lang.reflect.InvocationTargetException; 35de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebskiimport java.lang.reflect.Method; 36de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebskiimport libcore.util.BasicLruCache; 37de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebskiimport libcore.util.EmptyArray; 3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This is the common base class of all Java language enumeration types. 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * More information about enums, including descriptions of the 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implicitly declared methods synthesized by the compiler, can be 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * found in section 8.9 of 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <cite>The Java™ Language Specification</cite>. 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Note that when using an enumeration type as the type of a set 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or as the type of the keys in a map, specialized and efficient 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain java.util.EnumSet set} and {@linkplain 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.util.EnumMap map} implementations are available. 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param <E> The enum type subclass 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Josh Bloch 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Neal Gafter 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Class#getEnumConstants() 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.util.EnumSet 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.util.EnumMap 5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic abstract class Enum<E extends Enum<E>> 6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski implements Comparable<E>, Serializable { 6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The name of this enum constant, as declared in the enum declaration. 6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Most programmers should use the {@link #toString} method rather than 6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accessing this field. 6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private final String name; 6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the name of this enum constant, exactly as declared in its 7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enum declaration. 7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <b>Most programmers should use the {@link #toString} method in 7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * preference to this one, as the toString method may return 7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a more user-friendly name.</b> This method is designed primarily for 7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * use in specialized situations where correctness depends on getting the 7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exact name, which will not vary from release to release. 7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the name of this enum constant 8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final String name() { 8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The ordinal of this enumeration constant (its position 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in the enum declaration, where the initial constant is assigned 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an ordinal of zero). 8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Most programmers will have no use for this field. It is designed 9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for use by sophisticated enum-based data structures, such as 9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.util.EnumSet} and {@link java.util.EnumMap}. 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private final int ordinal; 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the ordinal of this enumeration constant (its position 9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in its enum declaration, where the initial constant is assigned 9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an ordinal of zero). 10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Most programmers will have no use for this method. It is 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * designed for use by sophisticated enum-based data structures, such 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as {@link java.util.EnumSet} and {@link java.util.EnumMap}. 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the ordinal of this enumeration constant 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final int ordinal() { 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ordinal; 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sole constructor. Programmers cannot invoke this constructor. 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * It is for use by code emitted by the compiler in response to 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enum type declarations. 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name - The name of this enum constant, which is the identifier 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * used to declare it. 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param ordinal - The ordinal of this enumeration constant (its position 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in the enum declaration, where the initial constant is assigned 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an ordinal of zero). 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected Enum(String name, int ordinal) { 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.name = name; 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.ordinal = ordinal; 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the name of this enum constant, as contained in the 12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * declaration. This method may be overridden, though it typically 13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * isn't necessary or desirable. An enum type should override this 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method when a more "programmer-friendly" string form exists. 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the name of this enum constant 13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public String toString() { 13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return name; 13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns true if the specified object is equal to this 14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enum constant. 14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param other the object to be compared for equality with this object. 14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return true if the specified object is equal to this 14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enum constant. 14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final boolean equals(Object other) { 14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return this==other; 14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code for this enum constant. 15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a hash code for this enum constant. 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final int hashCode() { 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return super.hashCode(); 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Throws CloneNotSupportedException. This guarantees that enums 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are never cloned, which is necessary to preserve their "singleton" 16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * status. 16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return (never returns) 16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected final Object clone() throws CloneNotSupportedException { 16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new CloneNotSupportedException(); 16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Compares this enum with the specified object for order. Returns a 17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * negative integer, zero, or a positive integer as this object is less 17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * than, equal to, or greater than the specified object. 17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Enum constants are only comparable to other enum constants of the 17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * same enum type. The natural order implemented by this 17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method is the order in which the constants are declared. 17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final int compareTo(E o) { 18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Enum other = (Enum)o; 18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Enum self = this; 18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (self.getClass() != other.getClass() && // optimization 18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski self.getDeclaringClass() != other.getDeclaringClass()) 18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ClassCastException(); 18651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return self.ordinal - other.ordinal; 18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the Class object corresponding to this enum constant's 19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enum type. Two enum constants e1 and e2 are of the 19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * same enum type if and only if 19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * e1.getDeclaringClass() == e2.getDeclaringClass(). 19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (The value returned by this method may differ from the one returned 19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link Object#getClass} method for enum constants with 19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constant-specific class bodies.) 19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the Class object corresponding to this enum constant's 19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enum type 20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final Class<E> getDeclaringClass() { 20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class clazz = getClass(); 20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class zuper = clazz.getSuperclass(); 20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (zuper == Enum.class) ? clazz : zuper; 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the enum constant of the specified enum type with the 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name. The name must match exactly an identifier used 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to declare an enum constant in this type. (Extraneous whitespace 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * characters are not permitted.) 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Note that for a particular enum type {@code T}, the 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implicitly declared {@code public static T valueOf(String)} 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on that enum may be used instead of this method to map 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * from a name to the corresponding enum constant. All the 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * constants of an enum type can be obtained by calling the 21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implicit {@code public static T[] values()} method of that 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type. 22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param <T> The enum type whose constant is to be returned 22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param enumType the {@code Class} object of the enum type from which 22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to return a constant 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the name of the constant to return 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the enum constant of the specified enum type with the 22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified name 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if the specified enum type has 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * no constant with the specified name, or the specified 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * class object does not represent an enum type 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if {@code enumType} or {@code name} 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is null 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T extends Enum<T>> T valueOf(Class<T> enumType, 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String name) { 236de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski if (enumType == null) 237de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski throw new NullPointerException("enumType == null"); 238f28158fc458ad4175d27bd515626d1064577f22cNarayan Kamath if (name == null) 239f28158fc458ad4175d27bd515626d1064577f22cNarayan Kamath throw new NullPointerException("Name is null"); 240de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski T[] values = getSharedConstants(enumType); 241de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski T result = null; 242de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski if (values != null) { 243de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski for (T value : values) { 244de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski if (name.equals(value.name())) { 245de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski result = value; 246de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } 247de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } 248f28158fc458ad4175d27bd515626d1064577f22cNarayan Kamath } else { 249f28158fc458ad4175d27bd515626d1064577f22cNarayan Kamath throw new IllegalArgumentException(enumType.toString() + " is not an enum type."); 250de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } 251de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski 25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (result != null) 25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException( 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski "No enum constant " + enumType.getCanonicalName() + "." + name); 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 258de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski private static final BasicLruCache<Class<? extends Enum>, Object[]> sharedConstantsCache 259de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski = new BasicLruCache<Class<? extends Enum>, Object[]>(64) { 260de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski @Override protected Object[] create(Class<? extends Enum> enumType) { 261de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski if (!enumType.isEnum()) { 262de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski return null; 263de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } 264de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski try { 265de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski Method method = enumType.getDeclaredMethod("values", EmptyArray.CLASS); 266de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski method.setAccessible(true); 267de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski return (Object[]) method.invoke((Object[]) null); 268de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } catch (NoSuchMethodException impossible) { 269de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski throw new AssertionError("impossible", impossible); 270de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } catch (IllegalAccessException impossible) { 271de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski throw new AssertionError("impossible", impossible); 272de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } catch (InvocationTargetException impossible) { 273de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski throw new AssertionError("impossible", impossible); 274de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } 275de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } 276de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski }; 277de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski 278de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski /** 279de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski * Returns a shared, mutable array containing the constants of this enum. It 280de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski * is an error to modify the returned array. 281de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski * 282de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski * @hide 283de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski */ 284de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski @SuppressWarnings("unchecked") // the cache always returns the type matching enumType 285de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski public static <T extends Enum<T>> T[] getSharedConstants(Class<T> enumType) { 286de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski return (T[]) sharedConstantsCache.get(enumType); 287de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski } 288de626ec8a109ea18283d96c720cc57e2f32f67faPiotr Jastrzebski 28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enum classes cannot have finalize methods. 29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski protected final void finalize() { } 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prevent default deserialization 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private void readObject(ObjectInputStream in) throws IOException, 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ClassNotFoundException { 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new InvalidObjectException("can't deserialize enum"); 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private void readObjectNoData() throws ObjectStreamException { 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new InvalidObjectException("can't deserialize enum"); 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 306