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