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