History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/java/lang/Enum.java (1 of 18352 files). Show all

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t