1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang.enum;
  55. import java.util.Iterator;
  56. import java.util.List;
  57. import java.util.Map;
  58. /**
  59. * <p>Utility class for accessing and manipulating {@link Enum}s.</p>
  60. *
  61. * @see Enum
  62. * @see ValuedEnum
  63. * @author Stephen Colebourne
  64. * @author Gary Gregory
  65. * @since 1.0
  66. * @version $Id: EnumUtils.java,v 1.10 2003/08/21 05:29:08 ggregory Exp $
  67. */
  68. public class EnumUtils {
  69. /**
  70. * Public constructor. This class should not normally be instantiated.
  71. * @since 2.0
  72. */
  73. public EnumUtils() {
  74. }
  75. /**
  76. * <p>Gets an <code>Enum</code> object by class and name.</p>
  77. *
  78. * @param enumClass the class of the <code>Enum</code> to get
  79. * @param name the name of the Enum to get, may be <code>null</code>
  80. * @return the enum object
  81. * @throws IllegalArgumentException if the enum class is <code>null</code>
  82. */
  83. public static Enum getEnum(Class enumClass, String name) {
  84. return Enum.getEnum(enumClass, name);
  85. }
  86. /**
  87. * <p>Gets a <code>ValuedEnum</code> object by class and value.</p>
  88. *
  89. * @param enumClass the class of the <code>Enum</code> to get
  90. * @param value the value of the <code>Enum</code> to get
  91. * @return the enum object, or null if the enum does not exist
  92. * @throws IllegalArgumentException if the enum class is <code>null</code>
  93. */
  94. public static ValuedEnum getEnum(Class enumClass, int value) {
  95. return (ValuedEnum) ValuedEnum.getEnum(enumClass, value);
  96. }
  97. /**
  98. * <p>Gets the <code>Map</code> of <code>Enum</code> objects by
  99. * name using the <code>Enum</code> class.</p>
  100. *
  101. * <p>If the requested class has no enum objects an empty
  102. * <code>Map</code> is returned. The <code>Map</code> is unmodifiable.</p>
  103. *
  104. * @param enumClass the class of the <code>Enum</code> to get
  105. * @return the enum object Map
  106. * @throws IllegalArgumentException if the enum class is <code>null</code>
  107. * @throws IllegalArgumentException if the enum class is not a subclass
  108. * of <code>Enum</code>
  109. */
  110. public static Map getEnumMap(Class enumClass) {
  111. return Enum.getEnumMap(enumClass);
  112. }
  113. /**
  114. * <p>Gets the <code>List</code> of <code>Enum</code> objects using
  115. * the <code>Enum</code> class.</p>
  116. *
  117. * <p>The list is in the order that the objects were created
  118. * (source code order).</p>
  119. *
  120. * <p>If the requested class has no enum objects an empty
  121. * <code>List</code> is returned. The <code>List</code> is unmodifiable.</p>
  122. *
  123. * @param enumClass the class of the Enum to get
  124. * @return the enum object Map
  125. * @throws IllegalArgumentException if the enum class is <code>null</code>
  126. * @throws IllegalArgumentException if the enum class is not a subclass
  127. * of <code>Enum</code>
  128. */
  129. public static List getEnumList(Class enumClass) {
  130. return Enum.getEnumList(enumClass);
  131. }
  132. /**
  133. * <p>Gets an <code>Iterator</code> over the <code>Enum</code> objects
  134. * in an <code>Enum</code> class.</p>
  135. *
  136. * <p>The iterator is in the order that the objects were created
  137. * (source code order).</p>
  138. *
  139. * <p>If the requested class has no enum objects an empty
  140. * <code>Iterator</code> is returned. The <code>Iterator</code>
  141. * is unmodifiable.</p>
  142. *
  143. * @param enumClass the class of the <code>Enum</code> to get
  144. * @return an <code>Iterator</code> of the <code>Enum</code> objects
  145. * @throws IllegalArgumentException if the enum class is <code>null</code>
  146. * @throws IllegalArgumentException if the enum class is not a subclass of <code>Enum</code>
  147. */
  148. public static Iterator iterator(Class enumClass) {
  149. return Enum.getEnumList(enumClass).iterator();
  150. }
  151. }