KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > lang > Enum


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.lang;
10
11 import j2me.io.Serializable;
12 import j2me.lang.Comparable;
13 import j2mex.realtime.MemoryArea;
14
15 import javolution.util.FastMap;
16
17 /**
18  * <p> This class is equivalent to <code>j2me.lang.Enum</code>
19  * and is moved (refactored) to the <code>j2me.lang</code> system
20  * package for applications targetting the J2SE 5.0+ run-time.</p>
21  *
22  * <p> This is a clean-room implementation of the Enum base class.</p>
23  *
24  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
25  * @version 2.0, November 26, 2004
26  */

27 public abstract class Enum implements Comparable JavaDoc, Serializable {
28
29     /**
30      * Holds the class to enum mapping.
31      */

32     private static final FastMap CLASS_TO_ENUMS = new FastMap();
33
34     /**
35      * Holds the enum's name.
36      */

37     private final String JavaDoc _name;
38
39     /**
40      * Holds the enum's position.
41      */

42     private final int _ordinal;
43
44     /**
45      * Returns the name of this enum.
46      *
47      * @return the enum's name.
48      */

49     public final String JavaDoc name() {
50         return _name;
51     }
52
53     /**
54      * Returns the position of this enum in the enumeration (starting
55      * at <code>0</code>).
56      *
57      * @return the enum's position.
58      */

59     public final int ordinal() {
60         return _ordinal;
61     }
62
63     /**
64      * Enum's base constructor.
65      *
66      * @param name the enum's name.
67      * @param ordinal the enum's position.
68      */

69     protected Enum(String JavaDoc name, int ordinal) {
70         _name = name;
71         _ordinal = ordinal;
72         synchronized (CLASS_TO_ENUMS) {
73             MemoryArea.getMemoryArea(CLASS_TO_ENUMS).executeInArea(
74                     new Runnable JavaDoc() {
75                         public void run() {
76                             FastMap nameToEnum = (FastMap) CLASS_TO_ENUMS
77                                     .get(Enum.this.getClass());
78                             if (nameToEnum == null) {
79                                 nameToEnum = new FastMap();
80                                 CLASS_TO_ENUMS.put(Enum.this.getClass(),
81                                         nameToEnum);
82                             }
83                             Object JavaDoc prev = nameToEnum.put(_name, Enum.this);
84                             if (prev != null) {
85                                 throw new IllegalArgumentException JavaDoc(
86                                         "Duplicate enum " + _name);
87                             }
88                         }
89                     });
90         }
91     }
92
93     /**
94      * Returns the <code>String</code> representation of this enum.
95      *
96      * @return the enum's name.
97      */

98     public String JavaDoc toString() {
99         return _name;
100     }
101
102     /**
103      * Indicates if two enums are equals.
104      *
105      * @param that the enum to be compared for equality.
106      * @return <code>this == that</code>
107      */

108     public final boolean equals(Object JavaDoc that) {
109         return this == that;
110     }
111
112     /**
113      * Returns the enums' hashcode.
114      *
115      * @return <code>System.identityHashCode(this)</code>
116      */

117     public final int hashCode() {
118         return System.identityHashCode(this);
119     }
120
121     /**
122      * Compares the position of two enum from the same enumeration.
123      *
124      * @param that the enum to be compared with.
125      * @return a negative value, zero, or a positive value as this enum
126      * is less than, equal to, or greater than the specified enum.
127      * @throws ClassCastException if both enum do not belong to the same
128      * enumeration.
129      */

130     public final int compareTo(Object JavaDoc that) {
131         Enum JavaDoc e = (Enum JavaDoc) that;
132         if (this.getClass() == that.getClass()) {
133             return this._ordinal - e._ordinal;
134         } else {
135             throw new ClassCastException JavaDoc();
136         }
137     }
138
139     /**
140      * Returns this enum's class.
141      *
142      * @return <code>this.getClass()</code>
143      */

144     public final Class JavaDoc getDeclaringClass() {
145         return this.getClass();
146     }
147
148     /**
149      * Returns the enum from the specified enum's class with the specified
150      * name.
151      *
152      * @param enumType the enum's declaring class.
153      * @param name the name of the enum to return.
154      * @return the corresponding enum.
155      * @throws IllegalArgumentException if the enum does not exist.
156      */

157     public static Enum JavaDoc valueOf(Class JavaDoc enumType, String JavaDoc name) {
158         FastMap nameToEnum = (FastMap) CLASS_TO_ENUMS.get(enumType);
159         if (nameToEnum != null) {
160             Enum JavaDoc e = (Enum JavaDoc) nameToEnum.get(name);
161             if (e != null) {
162                 return e;
163             }
164         }
165         throw new IllegalArgumentException JavaDoc(enumType + "." + name + " not found");
166     }
167
168 }
Popular Tags