1 /* 2 * Copyright 2003-2009 Sun Microsystems, Inc. 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. Sun designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 22 * CA 95054 USA or visit www.sun.com if you need additional information or 23 * have any questions. 24 */ 25 26 package java.lang; 27 28 import java.io.Serializable; 29 import java.io.IOException; 30 import java.io.InvalidObjectException; 31 import java.io.ObjectInputStream; 32 import java.io.ObjectStreamException; 33 34 /** 35 * This is the common base class of all Java language enumeration types. 36 * 37 * More information about enums, including implicit methods synthesised 38 * by the compiler, can be found in <i>The Java™ Language 39 * Specification, Third Edition</i>, <a 40 * href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9">§8.9</a>. 41 * 42 * @author Josh Bloch 43 * @author Neal Gafter 44 * @see Class#getEnumConstants() 45 * @since 1.5 46 */ 47 public abstract class Enum<E extends Enum<E>> 48 implements Comparable<E>, Serializable { 49 /** 50 * The name of this enum constant, as declared in the enum declaration. 51 * Most programmers should use the {@link #toString} method rather than 52 * accessing this field. 53 */ 54 private final String name; 55 56 /** 57 * Returns the name of this enum constant, exactly as declared in its 58 * enum declaration. 59 * 60 * <b>Most programmers should use the {@link #toString} method in 61 * preference to this one, as the toString method may return 62 * a more user-friendly name.</b> This method is designed primarily for 63 * use in specialized situations where correctness depends on getting the 64 * exact name, which will not vary from release to release. 65 * 66 * @return the name of this enum constant 67 */ 68 public final String name() { 69 return name; 70 } 71 72 /** 73 * The ordinal of this enumeration constant (its position 74 * in the enum declaration, where the initial constant is assigned 75 * an ordinal of zero). 76 * 77 * Most programmers will have no use for this field. It is designed 78 * for use by sophisticated enum-based data structures, such as 79 * {@link java.util.EnumSet} and {@link java.util.EnumMap}. 80 */ 81 private final int ordinal; 82 83 /** 84 * Returns the ordinal of this enumeration constant (its position 85 * in its enum declaration, where the initial constant is assigned 86 * an ordinal of zero). 87 * 88 * Most programmers will have no use for this method. It is 89 * designed for use by sophisticated enum-based data structures, such 90 * as {@link java.util.EnumSet} and {@link java.util.EnumMap}. 91 * 92 * @return the ordinal of this enumeration constant 93 */ 94 public final int ordinal() { 95 return ordinal; 96 } 97 98 /** 99 * Sole constructor. Programmers cannot invoke this constructor. 100 * It is for use by code emitted by the compiler in response to 101 * enum type declarations. 102 * 103 * @param name - The name of this enum constant, which is the identifier 104 * used to declare it. 105 * @param ordinal - The ordinal of this enumeration constant (its position 106 * in the enum declaration, where the initial constant is assigned 107 * an ordinal of zero). 108 */ 109 protected Enum(String name, int ordinal) { 110 this.name = name; 111 this.ordinal = ordinal; 112 } 113 114 /** 115 * Returns the name of this enum constant, as contained in the 116 * declaration. This method may be overridden, though it typically 117 * isn't necessary or desirable. An enum type should override this 118 * method when a more "programmer-friendly" string form exists. 119 * 120 * @return the name of this enum constant 121 */ 122 public String toString() { 123 return name; 124 } 125 126 /** 127 * Returns true if the specified object is equal to this 128 * enum constant. 129 * 130 * @param other the object to be compared for equality with this object. 131 * @return true if the specified object is equal to this 132 * enum constant. 133 */ 134 public final boolean equals(Object other) { 135 return this==other; 136 } 137 138 /** 139 * Returns a hash code for this enum constant. 140 * 141 * @return a hash code for this enum constant. 142 */ 143 public final int hashCode() { 144 return super.hashCode(); 145 } 146 147 /** 148 * Throws CloneNotSupportedException. This guarantees that enums 149 * are never cloned, which is necessary to preserve their "singleton" 150 * status. 151 * 152 * @return (never returns) 153 */ 154 protected final Object clone() throws CloneNotSupportedException { 155 throw new CloneNotSupportedException(); 156 } 157 158 /** 159 * Compares this enum with the specified object for order. Returns a 160 * negative integer, zero, or a positive integer as this object is less 161 * than, equal to, or greater than the specified object. 162 * 163 * Enum constants are only comparable to other enum constants of the 164 * same enum type. The natural order implemented by this 165 * method is the order in which the constants are declared. 166 */ 167 public final int compareTo(E o) { 168 Enum other = (Enum)o; 169 Enum self = this; 170 if (self.getClass() != other.getClass() && // optimization 171 self.getDeclaringClass() != other.getDeclaringClass()) 172 throw new ClassCastException(); 173 return self.ordinal - other.ordinal; 174 } 175 176 /** 177 * Returns the Class object corresponding to this enum constant's 178 * enum type. Two enum constants e1 and e2 are of the 179 * same enum type if and only if 180 * e1.getDeclaringClass() == e2.getDeclaringClass(). 181 * (The value returned by this method may differ from the one returned 182 * by the {@link Object#getClass} method for enum constants with 183 * constant-specific class bodies.) 184 * 185 * @return the Class object corresponding to this enum constant's 186 * enum type 187 */ 188 public final Class<E> getDeclaringClass() { 189 Class clazz = getClass(); 190 Class zuper = clazz.getSuperclass(); 191 return (zuper == Enum.class) ? clazz : zuper; 192 } 193 194 /** 195 * Returns the enum constant of the specified enum type with the 196 * specified name. The name must match exactly an identifier used 197 * to declare an enum constant in this type. (Extraneous whitespace 198 * characters are not permitted.) 199 * 200 * @param enumType the {@code Class} object of the enum type from which 201 * to return a constant 202 * @param name the name of the constant to return 203 * @return the enum constant of the specified enum type with the 204 * specified name 205 * @throws IllegalArgumentException if the specified enum type has 206 * no constant with the specified name, or the specified 207 * class object does not represent an enum type 208 * @throws NullPointerException if {@code enumType} or {@code name} 209 * is null 210 * @since 1.5 211 */ 212 public static <T extends Enum<T>> T valueOf(Class<T> enumType, 213 String name) { 214 T result = enumType.enumConstantDirectory().get(name); 215 if (result != null) 216 return result; 217 if (name == null) 218 throw new NullPointerException("Name is null"); 219 throw new IllegalArgumentException( 220 "No enum constant " + enumType.getCanonicalName() + "." + name); 221 } 222 223 /** 224 * enum classes cannot have finalize methods. 225 */ 226 protected final void finalize() { } 227 228 /** 229 * prevent default deserialization 230 */ 231 private void readObject(ObjectInputStream in) throws IOException, 232 ClassNotFoundException { 233 throw new InvalidObjectException("can't deserialize enum"); 234 } 235 236 private void readObjectNoData() throws ObjectStreamException { 237 throw new InvalidObjectException("can't deserialize enum"); 238 } 239 }