KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Map JavaDoc;
21
22 /**
23  * <p>Utility class for accessing and manipulating {@link Enum}s.</p>
24  *
25  * @see Enum
26  * @see ValuedEnum
27  * @author Stephen Colebourne
28  * @author Gary Gregory
29  * @since 2.1 (class existed in enum package from v1.0)
30  * @version $Id: EnumUtils.java 161243 2005-04-14 04:30:28Z ggregory $
31  */

32 public class EnumUtils {
33
34     /**
35      * Public constructor. This class should not normally be instantiated.
36      * @since 2.0
37      */

38     public EnumUtils() {
39     }
40
41     /**
42      * <p>Gets an <code>Enum</code> object by class and name.</p>
43      *
44      * @param enumClass the class of the <code>Enum</code> to get
45      * @param name the name of the Enum to get, may be <code>null</code>
46      * @return the enum object
47      * @throws IllegalArgumentException if the enum class is <code>null</code>
48      */

49     public static Enum JavaDoc getEnum(Class JavaDoc enumClass, String JavaDoc name) {
50         return Enum.getEnum(enumClass, name);
51     }
52
53     /**
54      * <p>Gets a <code>ValuedEnum</code> object by class and value.</p>
55      *
56      * @param enumClass the class of the <code>Enum</code> to get
57      * @param value the value of the <code>Enum</code> to get
58      * @return the enum object, or null if the enum does not exist
59      * @throws IllegalArgumentException if the enum class is <code>null</code>
60      */

61     public static ValuedEnum getEnum(Class JavaDoc enumClass, int value) {
62         return (ValuedEnum) ValuedEnum.getEnum(enumClass, value);
63     }
64
65     /**
66      * <p>Gets the <code>Map</code> of <code>Enum</code> objects by
67      * name using the <code>Enum</code> class.</p>
68      *
69      * <p>If the requested class has no enum objects an empty
70      * <code>Map</code> is returned. The <code>Map</code> is unmodifiable.</p>
71      *
72      * @param enumClass the class of the <code>Enum</code> to get
73      * @return the enum object Map
74      * @throws IllegalArgumentException if the enum class is <code>null</code>
75      * @throws IllegalArgumentException if the enum class is not a subclass
76      * of <code>Enum</code>
77      */

78     public static Map JavaDoc getEnumMap(Class JavaDoc enumClass) {
79         return Enum.getEnumMap(enumClass);
80     }
81
82     /**
83      * <p>Gets the <code>List</code> of <code>Enum</code> objects using
84      * the <code>Enum</code> class.</p>
85      *
86      * <p>The list is in the order that the objects were created
87      * (source code order).</p>
88      *
89      * <p>If the requested class has no enum objects an empty
90      * <code>List</code> is returned. The <code>List</code> is unmodifiable.</p>
91      *
92      * @param enumClass the class of the Enum to get
93      * @return the enum object Map
94      * @throws IllegalArgumentException if the enum class is <code>null</code>
95      * @throws IllegalArgumentException if the enum class is not a subclass
96      * of <code>Enum</code>
97      */

98     public static List JavaDoc getEnumList(Class JavaDoc enumClass) {
99         return Enum.getEnumList(enumClass);
100     }
101
102     /**
103      * <p>Gets an <code>Iterator</code> over the <code>Enum</code> objects
104      * in an <code>Enum</code> class.</p>
105      *
106      * <p>The iterator is in the order that the objects were created
107      * (source code order).</p>
108      *
109      * <p>If the requested class has no enum objects an empty
110      * <code>Iterator</code> is returned. The <code>Iterator</code>
111      * is unmodifiable.</p>
112      *
113      * @param enumClass the class of the <code>Enum</code> to get
114      * @return an <code>Iterator</code> of the <code>Enum</code> objects
115      * @throws IllegalArgumentException if the enum class is <code>null</code>
116      * @throws IllegalArgumentException if the enum class is not a subclass of <code>Enum</code>
117      */

118     public static Iterator JavaDoc iterator(Class JavaDoc enumClass) {
119         return Enum.getEnumList(enumClass).iterator();
120     }
121     
122 }
123
Popular Tags