01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package java.lang;
17:
18: import com.google.gwt.core.client.JavaScriptObject;
19:
20: import java.io.Serializable;
21:
22: /**
23: * The first-class representation of an enumeration.
24: *
25: * @param <E>
26: */
27: public abstract class Enum<E extends Enum<E>> implements Comparable<E>,
28: Serializable {
29:
30: protected static <T extends Enum<T>> T valueOf(
31: JavaScriptObject map, String name) {
32: T result = Enum.<T> valueOf0(map, "_" + name);
33: if (result == null) {
34: throw new IllegalArgumentException(name);
35: }
36: return result;
37: }
38:
39: private static native <T extends Enum<T>> T valueOf0(
40: JavaScriptObject map, String name) /*-{
41: return map[name] || null;
42: }-*/;
43:
44: private final String name;
45:
46: private final int ordinal;
47:
48: protected Enum(String name, int ordinal) {
49: this .name = name;
50: this .ordinal = ordinal;
51: }
52:
53: public final int compareTo(E other) {
54: // TODO: will a bridge method do the cast for us?
55: // if (GWT.getTypeName(this) != GWT.getTypeName(other)) {
56: // throw new ClassCastException();
57: // }
58: return this .ordinal - other.ordinal;
59: }
60:
61: @Override
62: public final boolean equals(Object other) {
63: return this == other;
64: }
65:
66: @SuppressWarnings("unchecked")
67: public final Class<E> getDeclaringClass() {
68: Class clazz = getClass();
69: Class super class = clazz.getSuperclass();
70:
71: return (super class == Enum.class) ? clazz : super class;
72: }
73:
74: @Override
75: public final int hashCode() {
76: return System.identityHashCode(this );
77: }
78:
79: public final String name() {
80: return name;
81: }
82:
83: public final int ordinal() {
84: return ordinal;
85: }
86:
87: @Override
88: public String toString() {
89: return name;
90: }
91:
92: }
|