source: src/main/java/agents/org/apache/commons/lang/enum1/ValuedEnum.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 7.0 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package agents.org.apache.commons.lang.enum1;
18
19import java.util.Iterator;
20import java.util.List;
21
22import agents.org.apache.commons.lang.ClassUtils;
23
24/**
25 * <p>
26 * Abstract superclass for type-safe enums with integer values suitable for use
27 * in <code>switch</code> statements.
28 * </p>
29 *
30 * <p>
31 * <em>NOTE:</em>Due to the way in which Java ClassLoaders work, comparing
32 * <code>Enum</code> objects should always be done using the equals() method,
33 * not <code>==</code>. The equals() method will try <code>==</code> first so in
34 * most cases the effect is the same.
35 * </p>
36 *
37 * <p>
38 * To use this class, it must be subclassed. For example:
39 * </p>
40 *
41 * <pre>
42 * public final class JavaVersionEnum extends ValuedEnum {
43 * // standard enums for version of JVM
44 * public static final int JAVA1_0_VALUE = 100;
45 * public static final int JAVA1_1_VALUE = 110;
46 * public static final int JAVA1_2_VALUE = 120;
47 * public static final int JAVA1_3_VALUE = 130;
48 * public static final JavaVersionEnum JAVA1_0 = new JavaVersionEnum("Java 1.0", JAVA1_0_VALUE);
49 * public static final JavaVersionEnum JAVA1_1 = new JavaVersionEnum("Java 1.1", JAVA1_1_VALUE);
50 * public static final JavaVersionEnum JAVA1_2 = new JavaVersionEnum("Java 1.2", JAVA1_2_VALUE);
51 * public static final JavaVersionEnum JAVA1_3 = new JavaVersionEnum("Java 1.3", JAVA1_3_VALUE);
52 *
53 * private JavaVersionEnum(String name, int value) {
54 * super(name, value);
55 * }
56 *
57 * public static JavaVersionEnum getEnum(String javaVersion) {
58 * return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
59 * }
60 *
61 * public static JavaVersionEnum getEnum(int javaVersion) {
62 * return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
63 * }
64 *
65 * public static Map getEnumMap() {
66 * return getEnumMap(JavaVersionEnum.class);
67 * }
68 *
69 * public static List getEnumList() {
70 * return getEnumList(JavaVersionEnum.class);
71 * }
72 *
73 * public static Iterator iterator() {
74 * return iterator(JavaVersionEnum.class);
75 * }
76 * }
77 * </pre>
78 *
79 * <p>
80 * The above class could then be used as follows:
81 * </p>
82 *
83 * <pre>
84 * public void doSomething(JavaVersionEnum ver) {
85 * switch (ver.getValue()) {
86 * case JAVA1_0_VALUE:
87 * // ...
88 * break;
89 * case JAVA1_1_VALUE:
90 * // ...
91 * break;
92 * // ...
93 * }
94 * }
95 * </pre>
96 *
97 * <p>
98 * As shown, each enum has a name and a value. These can be accessed using
99 * <code>getName</code> and <code>getValue</code>.
100 * </p>
101 *
102 * <p>
103 * The <code>getEnum</code> and <code>iterator</code> methods are recommended.
104 * Unfortunately, Java restrictions require these to be coded as shown in each
105 * subclass. An alternative choice is to use the {@link EnumUtils} class.
106 * </p>
107 *
108 * @deprecated Replaced by {@link agents.org.apache.commons.lang.enums.ValuedEnum
109 * org.apache.commons.lang.enums.ValuedEnum} and will be removed in
110 * version 3.0. All classes in this package are deprecated and
111 * repackaged to {@link agents.org.apache.commons.lang.enums} since
112 * <code>enum</code> is a Java 1.5 keyword.
113 * @see agents.org.apache.commons.lang.enums.ValuedEnum
114 * @author Apache Avalon project
115 * @author Apache Software Foundation
116 * @since 1.0
117 * @version $Id: ValuedEnum.java 905636 2010-02-02 14:03:32Z niallp $
118 */
119public abstract class ValuedEnum extends Enum {
120
121 /**
122 * Required for serialization support. Lang version 1.0.1 serial
123 * compatibility.
124 *
125 * @see java.io.Serializable
126 */
127 private static final long serialVersionUID = -7129650521543789085L;
128
129 /**
130 * The value contained in enum.
131 */
132 private final int iValue;
133
134 /**
135 * Constructor for enum item.
136 *
137 * @param name
138 * the name of enum item
139 * @param value
140 * the value of enum item
141 */
142 protected ValuedEnum(String name, int value) {
143 super(name);
144 iValue = value;
145 }
146
147 /**
148 * <p>
149 * Gets an <code>Enum</code> object by class and value.
150 * </p>
151 *
152 * <p>
153 * This method loops through the list of <code>Enum</code>, thus if there
154 * are many <code>Enum</code>s this will be slow.
155 * </p>
156 *
157 * @param enumClass
158 * the class of the <code>Enum</code> to get
159 * @param value
160 * the value of the <code>Enum</code> to get
161 * @return the enum object, or null if the enum does not exist
162 * @throws IllegalArgumentException
163 * if the enum class is <code>null</code>
164 */
165 protected static Enum getEnum(Class enumClass, int value) {
166 if (enumClass == null) {
167 throw new IllegalArgumentException("The Enum Class must not be null");
168 }
169 List list = Enum.getEnumList(enumClass);
170 for (Iterator it = list.iterator(); it.hasNext();) {
171 ValuedEnum enumeration = (ValuedEnum) it.next();
172 if (enumeration.getValue() == value) {
173 return enumeration;
174 }
175 }
176 return null;
177 }
178
179 /**
180 * <p>
181 * Get value of enum item.
182 * </p>
183 *
184 * @return the enum item's value.
185 */
186 public final int getValue() {
187 return iValue;
188 }
189
190 /**
191 * <p>
192 * Tests for order.
193 * </p>
194 *
195 * <p>
196 * The default ordering is numeric by value, but this can be overridden by
197 * subclasses.
198 * </p>
199 *
200 * @see java.lang.Comparable#compareTo(Object)
201 * @param other
202 * the other object to compare to
203 * @return -ve if this is less than the other object, +ve if greater than,
204 * <code>0</code> of equal
205 * @throws ClassCastException
206 * if other is not an <code>Enum</code>
207 * @throws NullPointerException
208 * if other is <code>null</code>
209 */
210 public int compareTo(Object other) {
211 return iValue - ((ValuedEnum) other).iValue;
212 }
213
214 /**
215 * <p>
216 * Human readable description of this <code>Enum</code> item.
217 * </p>
218 *
219 * @return String in the form <code>type[name=value]</code>, for example:
220 * <code>JavaVersion[Java 1.0=100]</code>. Note that the package
221 * name is stripped from the type name.
222 */
223 public String toString() {
224 if (iToString == null) {
225 String shortName = ClassUtils.getShortClassName(getEnumClass());
226 iToString = shortName + "[" + getName() + "=" + getValue() + "]";
227 }
228 return iToString;
229 }
230}
Note: See TracBrowser for help on using the repository browser.