001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2005 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package javolution.lang;
010:
011: import j2me.io.Serializable;
012: import j2me.lang.Comparable;
013: import j2mex.realtime.MemoryArea;
014:
015: import javolution.util.FastMap;
016:
017: /**
018: * <p> This class is equivalent to <code>java.lang.Enum</code>
019: * and is moved (refactored) to the <code>java.lang</code> system
020: * package for applications targetting the J2SE 5.0+ run-time.</p>
021: *
022: * <p> This is a clean-room implementation of the Enum base class.</p>
023: *
024: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
025: * @version 2.0, November 26, 2004
026: */
027: public abstract class Enum implements Comparable, Serializable {
028:
029: /**
030: * Holds the class to enum mapping.
031: */
032: private static final FastMap CLASS_TO_ENUMS = new FastMap();
033:
034: /**
035: * Holds the enum's name.
036: */
037: private final String _name;
038:
039: /**
040: * Holds the enum's position.
041: */
042: private final int _ordinal;
043:
044: /**
045: * Returns the name of this enum.
046: *
047: * @return the enum's name.
048: */
049: public final String name() {
050: return _name;
051: }
052:
053: /**
054: * Returns the position of this enum in the enumeration (starting
055: * at <code>0</code>).
056: *
057: * @return the enum's position.
058: */
059: public final int ordinal() {
060: return _ordinal;
061: }
062:
063: /**
064: * Enum's base constructor.
065: *
066: * @param name the enum's name.
067: * @param ordinal the enum's position.
068: */
069: protected Enum(String name, int ordinal) {
070: _name = name;
071: _ordinal = ordinal;
072: synchronized (CLASS_TO_ENUMS) {
073: MemoryArea.getMemoryArea(CLASS_TO_ENUMS).executeInArea(
074: new Runnable() {
075: public void run() {
076: FastMap nameToEnum = (FastMap) CLASS_TO_ENUMS
077: .get(Enum.this .getClass());
078: if (nameToEnum == null) {
079: nameToEnum = new FastMap();
080: CLASS_TO_ENUMS.put(
081: Enum.this .getClass(),
082: nameToEnum);
083: }
084: Object prev = nameToEnum.put(_name,
085: Enum.this );
086: if (prev != null) {
087: throw new IllegalArgumentException(
088: "Duplicate enum " + _name);
089: }
090: }
091: });
092: }
093: }
094:
095: /**
096: * Returns the <code>String</code> representation of this enum.
097: *
098: * @return the enum's name.
099: */
100: public String toString() {
101: return _name;
102: }
103:
104: /**
105: * Indicates if two enums are equals.
106: *
107: * @param that the enum to be compared for equality.
108: * @return <code>this == that</code>
109: */
110: public final boolean equals(Object that) {
111: return this == that;
112: }
113:
114: /**
115: * Returns the enums' hashcode.
116: *
117: * @return <code>System.identityHashCode(this)</code>
118: */
119: public final int hashCode() {
120: return System.identityHashCode(this );
121: }
122:
123: /**
124: * Compares the position of two enum from the same enumeration.
125: *
126: * @param that the enum to be compared with.
127: * @return a negative value, zero, or a positive value as this enum
128: * is less than, equal to, or greater than the specified enum.
129: * @throws ClassCastException if both enum do not belong to the same
130: * enumeration.
131: */
132: public final int compareTo(Object that) {
133: Enum e = (Enum) that;
134: if (this .getClass() == that.getClass()) {
135: return this ._ordinal - e._ordinal;
136: } else {
137: throw new ClassCastException();
138: }
139: }
140:
141: /**
142: * Returns this enum's class.
143: *
144: * @return <code>this.getClass()</code>
145: */
146: public final Class getDeclaringClass() {
147: return this .getClass();
148: }
149:
150: /**
151: * Returns the enum from the specified enum's class with the specified
152: * name.
153: *
154: * @param enumType the enum's declaring class.
155: * @param name the name of the enum to return.
156: * @return the corresponding enum.
157: * @throws IllegalArgumentException if the enum does not exist.
158: */
159: public static Enum valueOf(Class enumType, String name) {
160: FastMap nameToEnum = (FastMap) CLASS_TO_ENUMS.get(enumType);
161: if (nameToEnum != null) {
162: Enum e = (Enum) nameToEnum.get(name);
163: if (e != null) {
164: return e;
165: }
166: }
167: throw new IllegalArgumentException(enumType + "." + name
168: + " not found");
169: }
170:
171: }
|