KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > enums > ValuedEnum


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.enums;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.commons.lang.ClassUtils;
22
23 /**
24  * <p>Abstract superclass for type-safe enums with integer values suitable
25  * for use in <code>switch</code> statements.</p>
26  *
27  * <p><em>NOTE:</em>Due to the way in which Java ClassLoaders work, comparing
28  * <code>Enum</code> objects should always be done using the equals() method,
29  * not <code>==</code>. The equals() method will try <code>==</code> first so
30  * in most cases the effect is the same.</p>
31  *
32  * <p>To use this class, it must be subclassed. For example:</p>
33  *
34  * <pre>
35  * public final class JavaVersionEnum extends ValuedEnum {
36  * //standard enums for version of JVM
37  * public static final int JAVA1_0_VALUE = 100;
38  * public static final int JAVA1_1_VALUE = 110;
39  * public static final int JAVA1_2_VALUE = 120;
40  * public static final int JAVA1_3_VALUE = 130;
41  * public static final JavaVersionEnum JAVA1_0 = new JavaVersionEnum( "Java 1.0", JAVA1_0_VALUE );
42  * public static final JavaVersionEnum JAVA1_1 = new JavaVersionEnum( "Java 1.1", JAVA1_1_VALUE );
43  * public static final JavaVersionEnum JAVA1_2 = new JavaVersionEnum( "Java 1.2", JAVA1_2_VALUE );
44  * public static final JavaVersionEnum JAVA1_3 = new JavaVersionEnum( "Java 1.3", JAVA1_3_VALUE );
45  *
46  * private JavaVersionEnum(String name, int value) {
47  * super( name, value );
48  * }
49  *
50  * public static JavaVersionEnum getEnum(String javaVersion) {
51  * return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
52  * }
53  *
54  * public static JavaVersionEnum getEnum(int javaVersion) {
55  * return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
56  * }
57  *
58  * public static Map getEnumMap() {
59  * return getEnumMap(JavaVersionEnum.class);
60  * }
61  *
62  * public static List getEnumList() {
63  * return getEnumList(JavaVersionEnum.class);
64  * }
65  *
66  * public static Iterator iterator() {
67  * return iterator(JavaVersionEnum.class);
68  * }
69  * }
70  * </pre>
71  *
72  * <p><em>NOTE:</em>These are declared <code>final</code>, so compilers may
73  * inline the code. Ensure you recompile everything when using final. </p>
74  *
75  * <p>The above class could then be used as follows:</p>
76  *
77  * <pre>
78  * public void doSomething(JavaVersion ver) {
79  * switch (ver.getValue()) {
80  * case JAVA1_0_VALUE:
81  * // ...
82  * break;
83  * case JAVA1_1_VALUE:
84  * // ...
85  * break;
86  * //...
87  * }
88  * }
89  * </pre>
90  *
91  * <p>As shown, each enum has a name and a value. These can be accessed using
92  * <code>getName</code> and <code>getValue</code>.</p>
93  *
94  * <p>The <code>getEnum</code> and <code>iterator</code> methods are recommended.
95  * Unfortunately, Java restrictions require these to be coded as shown in each subclass.
96  * An alternative choice is to use the {@link EnumUtils} class.</p>
97  *
98  * @author Apache Avalon project
99  * @author Stephen Colebourne
100  * @since 2.1 (class existed in enum package from v1.0)
101  * @version $Id: ValuedEnum.java 161243 2005-04-14 04:30:28Z ggregory $
102  */

103 public abstract class ValuedEnum extends Enum JavaDoc {
104     
105     /** Lang version 1.0.1 serial compatibility */
106     private static final long serialVersionUID = -7129650521543789085L;
107     
108     /**
109      * The value contained in enum.
110      */

111     private final int iValue;
112
113     /**
114      * Constructor for enum item.
115      *
116      * @param name the name of enum item
117      * @param value the value of enum item
118      */

119     protected ValuedEnum(String JavaDoc name, int value) {
120         super(name);
121         iValue = value;
122     }
123
124     /**
125      * <p>Gets an <code>Enum</code> object by class and value.</p>
126      *
127      * <p>This method loops through the list of <code>Enum</code>,
128      * thus if there are many <code>Enum</code>s this will be
129      * slow.</p>
130      *
131      * @param enumClass the class of the <code>Enum</code> to get
132      * @param value the value of the <code>Enum</code> to get
133      * @return the enum object, or null if the enum does not exist
134      * @throws IllegalArgumentException if the enum class is <code>null</code>
135      */

136     protected static Enum JavaDoc getEnum(Class JavaDoc enumClass, int value) {
137         if (enumClass == null) {
138             throw new IllegalArgumentException JavaDoc("The Enum Class must not be null");
139         }
140         List JavaDoc list = Enum.getEnumList(enumClass);
141         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
142             ValuedEnum enumeration = (ValuedEnum) it.next();
143             if (enumeration.getValue() == value) {
144                 return enumeration;
145             }
146         }
147         return null;
148     }
149
150     /**
151      * <p>Get value of enum item.</p>
152      *
153      * @return the enum item's value.
154      */

155     public final int getValue() {
156         return iValue;
157     }
158
159     /**
160      * <p>Tests for order.</p>
161      *
162      * <p>The default ordering is numeric by value, but this
163      * can be overridden by subclasses.</p>
164      *
165      * @see java.lang.Comparable#compareTo(Object)
166      * @param other the other object to compare to
167      * @return -ve if this is less than the other object, +ve if greater than,
168      * <code>0</code> of equal
169      * @throws ClassCastException if other is not an <code>Enum</code>
170      * @throws NullPointerException if other is <code>null</code>
171      */

172     public int compareTo(Object JavaDoc other) {
173         return iValue - ((ValuedEnum) other).iValue;
174     }
175
176     /**
177      * <p>Human readable description of this <code>Enum</code> item.</p>
178      *
179      * @return String in the form <code>type[name=value]</code>, for example:
180      * <code>JavaVersion[Java 1.0=100]</code>. Note that the package name is
181      * stripped from the type name.
182      */

183     public String JavaDoc toString() {
184         if (iToString == null) {
185             String JavaDoc shortName = ClassUtils.getShortClassName(getEnumClass());
186             iToString = shortName + "[" + getName() + "=" + getValue() + "]";
187         }
188         return iToString;
189     }
190 }
191
Popular Tags