~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/java/lang/Enum.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
import cli.System.Runtime.Serialization.IObjectReference;
 
34
import cli.System.Runtime.Serialization.SerializationException;
 
35
import cli.System.Runtime.Serialization.SerializationInfo;
 
36
import cli.System.Runtime.Serialization.StreamingContext;
 
37
 
 
38
/**
 
39
 * This is the common base class of all Java language enumeration types.
 
40
 *
 
41
 * More information about enums, including descriptions of the
 
42
 * implicitly declared methods synthesized by the compiler, can be
 
43
 * found in section 8.9 of
 
44
 * <cite>The Java&trade; Language Specification</cite>.
 
45
 *
 
46
 * <p> Note that when using an enumeration type as the type of a set
 
47
 * or as the type of the keys in a map, specialized and efficient
 
48
 * {@linkplain java.util.EnumSet set} and {@linkplain
 
49
 * java.util.EnumMap map} implementations are available.
 
50
 *
 
51
 * @param <E> The enum type subclass
 
52
 * @author  Josh Bloch
 
53
 * @author  Neal Gafter
 
54
 * @see     Class#getEnumConstants()
 
55
 * @see     java.util.EnumSet
 
56
 * @see     java.util.EnumMap
 
57
 * @since   1.5
 
58
 */
 
59
@cli.System.SerializableAttribute.Annotation
 
60
public abstract class Enum<E extends Enum<E>>
 
61
        implements Comparable<E>, Serializable {
 
62
    /**
 
63
     * The name of this enum constant, as declared in the enum declaration.
 
64
     * Most programmers should use the {@link #toString} method rather than
 
65
     * accessing this field.
 
66
     */
 
67
    private final String name;
 
68
 
 
69
    /**
 
70
     * Returns the name of this enum constant, exactly as declared in its
 
71
     * enum declaration.
 
72
     *
 
73
     * <b>Most programmers should use the {@link #toString} method in
 
74
     * preference to this one, as the toString method may return
 
75
     * a more user-friendly name.</b>  This method is designed primarily for
 
76
     * use in specialized situations where correctness depends on getting the
 
77
     * exact name, which will not vary from release to release.
 
78
     *
 
79
     * @return the name of this enum constant
 
80
     */
 
81
    public final String name() {
 
82
        return name;
 
83
    }
 
84
 
 
85
    /**
 
86
     * The ordinal of this enumeration constant (its position
 
87
     * in the enum declaration, where the initial constant is assigned
 
88
     * an ordinal of zero).
 
89
     *
 
90
     * Most programmers will have no use for this field.  It is designed
 
91
     * for use by sophisticated enum-based data structures, such as
 
92
     * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
 
93
     */
 
94
    private final int ordinal;
 
95
 
 
96
    /**
 
97
     * Returns the ordinal of this enumeration constant (its position
 
98
     * in its enum declaration, where the initial constant is assigned
 
99
     * an ordinal of zero).
 
100
     *
 
101
     * Most programmers will have no use for this method.  It is
 
102
     * designed for use by sophisticated enum-based data structures, such
 
103
     * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
 
104
     *
 
105
     * @return the ordinal of this enumeration constant
 
106
     */
 
107
    public final int ordinal() {
 
108
        return ordinal;
 
109
    }
 
110
 
 
111
    /**
 
112
     * Sole constructor.  Programmers cannot invoke this constructor.
 
113
     * It is for use by code emitted by the compiler in response to
 
114
     * enum type declarations.
 
115
     *
 
116
     * @param name - The name of this enum constant, which is the identifier
 
117
     *               used to declare it.
 
118
     * @param ordinal - The ordinal of this enumeration constant (its position
 
119
     *         in the enum declaration, where the initial constant is assigned
 
120
     *         an ordinal of zero).
 
121
     */
 
122
    protected Enum(String name, int ordinal) {
 
123
        this.name = name;
 
124
        this.ordinal = ordinal;
 
125
    }
 
126
 
 
127
    /**
 
128
     * Returns the name of this enum constant, as contained in the
 
129
     * declaration.  This method may be overridden, though it typically
 
130
     * isn't necessary or desirable.  An enum type should override this
 
131
     * method when a more "programmer-friendly" string form exists.
 
132
     *
 
133
     * @return the name of this enum constant
 
134
     */
 
135
    public String toString() {
 
136
        return name;
 
137
    }
 
138
 
 
139
    /**
 
140
     * Returns true if the specified object is equal to this
 
141
     * enum constant.
 
142
     *
 
143
     * @param other the object to be compared for equality with this object.
 
144
     * @return  true if the specified object is equal to this
 
145
     *          enum constant.
 
146
     */
 
147
    public final boolean equals(Object other) {
 
148
        return this==other;
 
149
    }
 
150
 
 
151
    /**
 
152
     * Returns a hash code for this enum constant.
 
153
     *
 
154
     * @return a hash code for this enum constant.
 
155
     */
 
156
    public final int hashCode() {
 
157
        return super.hashCode();
 
158
    }
 
159
 
 
160
    /**
 
161
     * Throws CloneNotSupportedException.  This guarantees that enums
 
162
     * are never cloned, which is necessary to preserve their "singleton"
 
163
     * status.
 
164
     *
 
165
     * @return (never returns)
 
166
     */
 
167
    protected final Object clone() throws CloneNotSupportedException {
 
168
        throw new CloneNotSupportedException();
 
169
    }
 
170
 
 
171
    /**
 
172
     * Compares this enum with the specified object for order.  Returns a
 
173
     * negative integer, zero, or a positive integer as this object is less
 
174
     * than, equal to, or greater than the specified object.
 
175
     *
 
176
     * Enum constants are only comparable to other enum constants of the
 
177
     * same enum type.  The natural order implemented by this
 
178
     * method is the order in which the constants are declared.
 
179
     */
 
180
    public final int compareTo(E o) {
 
181
        Enum other = (Enum)o;
 
182
        Enum self = this;
 
183
        if (self.getClass() != other.getClass() && // optimization
 
184
            self.getDeclaringClass() != other.getDeclaringClass())
 
185
            throw new ClassCastException();
 
186
        return self.ordinal - other.ordinal;
 
187
    }
 
188
 
 
189
    /**
 
190
     * Returns the Class object corresponding to this enum constant's
 
191
     * enum type.  Two enum constants e1 and  e2 are of the
 
192
     * same enum type if and only if
 
193
     *   e1.getDeclaringClass() == e2.getDeclaringClass().
 
194
     * (The value returned by this method may differ from the one returned
 
195
     * by the {@link Object#getClass} method for enum constants with
 
196
     * constant-specific class bodies.)
 
197
     *
 
198
     * @return the Class object corresponding to this enum constant's
 
199
     *     enum type
 
200
     */
 
201
    public final Class<E> getDeclaringClass() {
 
202
        Class clazz = getClass();
 
203
        Class zuper = clazz.getSuperclass();
 
204
        return (zuper == Enum.class) ? clazz : zuper;
 
205
    }
 
206
 
 
207
    /**
 
208
     * Returns the enum constant of the specified enum type with the
 
209
     * specified name.  The name must match exactly an identifier used
 
210
     * to declare an enum constant in this type.  (Extraneous whitespace
 
211
     * characters are not permitted.)
 
212
     *
 
213
     * <p>Note that for a particular enum type {@code T}, the
 
214
     * implicitly declared {@code public static T valueOf(String)}
 
215
     * method on that enum may be used instead of this method to map
 
216
     * from a name to the corresponding enum constant.  All the
 
217
     * constants of an enum type can be obtained by calling the
 
218
     * implicit {@code public static T[] values()} method of that
 
219
     * type.
 
220
     *
 
221
     * @param <T> The enum type whose constant is to be returned
 
222
     * @param enumType the {@code Class} object of the enum type from which
 
223
     *      to return a constant
 
224
     * @param name the name of the constant to return
 
225
     * @return the enum constant of the specified enum type with the
 
226
     *      specified name
 
227
     * @throws IllegalArgumentException if the specified enum type has
 
228
     *         no constant with the specified name, or the specified
 
229
     *         class object does not represent an enum type
 
230
     * @throws NullPointerException if {@code enumType} or {@code name}
 
231
     *         is null
 
232
     * @since 1.5
 
233
     */
 
234
    public static <T extends Enum<T>> T valueOf(Class<T> enumType,
 
235
                                                String name) {
 
236
        T result = enumType.enumConstantDirectory().get(name);
 
237
        if (result != null)
 
238
            return result;
 
239
        if (name == null)
 
240
            throw new NullPointerException("Name is null");
 
241
        throw new IllegalArgumentException(
 
242
            "No enum constant " + enumType.getCanonicalName() + "." + name);
 
243
    }
 
244
 
 
245
    /**
 
246
     * enum classes cannot have finalize methods.
 
247
     */
 
248
    protected final void finalize() { }
 
249
 
 
250
    /**
 
251
     * prevent default deserialization
 
252
     */
 
253
    private void readObject(ObjectInputStream in) throws IOException,
 
254
        ClassNotFoundException {
 
255
        throw new InvalidObjectException("can't deserialize enum");
 
256
    }
 
257
 
 
258
    private void readObjectNoData() throws ObjectStreamException {
 
259
        throw new InvalidObjectException("can't deserialize enum");
 
260
    }
 
261
    
 
262
    // [IKVM] .NET serialization support starts here
 
263
    // Note that we don't have a security demand, because the info is harmless.
 
264
    @cli.IKVM.Attributes.HideFromJavaAttribute.Annotation
 
265
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
266
    public final void GetObjectData(SerializationInfo info, StreamingContext context)
 
267
    {
 
268
        info.AddValue("enumType", getDeclaringClass());
 
269
        info.AddValue("name", name);
 
270
        info.SetType(ikvm.runtime.Util.getInstanceTypeFromClass(EnumSerializationProxy.class));
 
271
    }
 
272
}
 
273
 
 
274
@cli.System.SerializableAttribute.Annotation
 
275
final class EnumSerializationProxy implements IObjectReference
 
276
{
 
277
    private Class enumType;
 
278
    private String name;
 
279
    
 
280
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
281
    public Object GetRealObject(StreamingContext context)
 
282
    {
 
283
        try
 
284
        {
 
285
            return Enum.valueOf(enumType, name);
 
286
        }
 
287
        catch (IllegalArgumentException x)
 
288
        {
 
289
            ikvm.runtime.Util.throwException(new SerializationException("Enum value " + name + " not found in " + enumType, x));
 
290
            return null;
 
291
        }
 
292
    }
 
293
}