View Javadoc

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   */
17  package org.apache.commons.lang.enums;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * <p>Utility class for accessing and manipulating {@link Enum}s.</p>
25   *
26   * @see Enum
27   * @see ValuedEnum
28   * @author Stephen Colebourne
29   * @author Gary Gregory
30   * @since 2.1 (class existed in enum package from v1.0)
31   * @version $Id: EnumUtils.java 437554 2006-08-28 06:21:41Z bayard $
32   */
33  public class EnumUtils {
34  
35      /**
36       * Public constructor. This class should not normally be instantiated.
37       * @since 2.0
38       */
39      public EnumUtils() {
40        super();
41      }
42  
43      /**
44       * <p>Gets an <code>Enum</code> object by class and name.</p>
45       * 
46       * @param enumClass  the class of the <code>Enum</code> to get
47       * @param name  the name of the Enum to get, may be <code>null</code>
48       * @return the enum object
49       * @throws IllegalArgumentException if the enum class is <code>null</code>
50       */
51      public static Enum getEnum(Class enumClass, String name) {
52          return Enum.getEnum(enumClass, name);
53      }
54  
55      /**
56       * <p>Gets a <code>ValuedEnum</code> object by class and value.</p>
57       * 
58       * @param enumClass  the class of the <code>Enum</code> to get
59       * @param value  the value of the <code>Enum</code> to get
60       * @return the enum object, or null if the enum does not exist
61       * @throws IllegalArgumentException if the enum class is <code>null</code>
62       */
63      public static ValuedEnum getEnum(Class enumClass, int value) {
64          return (ValuedEnum) ValuedEnum.getEnum(enumClass, value);
65      }
66  
67      /**
68       * <p>Gets the <code>Map</code> of <code>Enum</code> objects by
69       * name using the <code>Enum</code> class.</p>
70       *
71       * <p>If the requested class has no enum objects an empty
72       * <code>Map</code> is returned. The <code>Map</code> is unmodifiable.</p>
73       * 
74       * @param enumClass  the class of the <code>Enum</code> to get
75       * @return the enum object Map
76       * @throws IllegalArgumentException if the enum class is <code>null</code>
77       * @throws IllegalArgumentException if the enum class is not a subclass
78       *  of <code>Enum</code>
79       */
80      public static Map getEnumMap(Class enumClass) {
81          return Enum.getEnumMap(enumClass);
82      }
83  
84      /**
85       * <p>Gets the <code>List</code> of <code>Enum</code> objects using
86       * the <code>Enum</code> class.</p>
87       *
88       * <p>The list is in the order that the objects were created
89       * (source code order).</p>
90       *
91       * <p>If the requested class has no enum objects an empty
92       * <code>List</code> is returned. The <code>List</code> is unmodifiable.</p>
93       * 
94       * @param enumClass  the class of the Enum to get
95       * @return the enum object Map
96       * @throws IllegalArgumentException if the enum class is <code>null</code>
97       * @throws IllegalArgumentException if the enum class is not a subclass
98       *  of <code>Enum</code>
99       */
100     public static List getEnumList(Class enumClass) {
101         return Enum.getEnumList(enumClass);
102     }
103 
104     /**
105      * <p>Gets an <code>Iterator</code> over the <code>Enum</code> objects
106      * in an <code>Enum</code> class.</p>
107      *
108      * <p>The iterator is in the order that the objects were created
109      * (source code order).</p>
110      *
111      * <p>If the requested class has no enum objects an empty
112      * <code>Iterator</code> is returned. The <code>Iterator</code>
113      * is unmodifiable.</p>
114      * 
115      * @param enumClass  the class of the <code>Enum</code> to get
116      * @return an <code>Iterator</code> of the <code>Enum</code> objects
117      * @throws IllegalArgumentException if the enum class is <code>null</code>
118      * @throws IllegalArgumentException if the enum class is not a subclass of <code>Enum</code>
119      */
120     public static Iterator iterator(Class enumClass) {
121         return Enum.getEnumList(enumClass).iterator();
122     }
123     
124 }