org.apache.commons.lang3
Class EnumUtils

java.lang.Object
  extended by org.apache.commons.lang3.EnumUtils

public class EnumUtils
extends Object

Utility library to provide helper methods for Java enums.

#ThreadSafe#

Version:
$Id: EnumUtils.java 967237 2010-07-23 20:08:57Z mbenson $
Author:
Apache Software Foundation

Constructor Summary
EnumUtils()
          This constructor is public to permit tools that require a JavaBean instance to operate.
 
Method Summary
static
<E extends Enum<E>>
E
getEnum(Class<E> enumClass, String enumName)
          Gets the enum for the class, returning null if not found.
static
<E extends Enum<E>>
List<E>
getEnumList(Class<E> enumClass)
          Gets the List of enums.
static
<E extends Enum<E>>
Map<String,E>
getEnumMap(Class<E> enumClass)
          Gets the Map of enums by name.
static
<E extends Enum<E>>
boolean
isValidEnum(Class<E> enumClass, String enumName)
          Checks if the specified name is a valid enum for the class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EnumUtils

public EnumUtils()
This constructor is public to permit tools that require a JavaBean instance to operate.

Method Detail

getEnumMap

public static <E extends Enum<E>> Map<String,E> getEnumMap(Class<E> enumClass)
Gets the Map of enums by name.

This method is useful when you need a map of enums by name.

Parameters:
enumClass - the class of the enum to get, not null
Returns:
the modifiable map of enum names to enums, never null

getEnumList

public static <E extends Enum<E>> List<E> getEnumList(Class<E> enumClass)
Gets the List of enums.

This method is useful when you need a list of enums rather than an array.

Parameters:
enumClass - the class of the enum to get, not null
Returns:
the modifiable list of enums, never null

isValidEnum

public static <E extends Enum<E>> boolean isValidEnum(Class<E> enumClass,
                                                      String enumName)
Checks if the specified name is a valid enum for the class.

This method differs from Enum.valueOf(java.lang.Class, java.lang.String) in that checks if the name is a valid enum without needing to catch the exception.

Parameters:
enumClass - the class of the enum to get, not null
enumName - the enum name
Returns:
true if the enum name is valid, otherwise false

getEnum

public static <E extends Enum<E>> E getEnum(Class<E> enumClass,
                                            String enumName)
Gets the enum for the class, returning null if not found.

This method differs from Enum.valueOf(java.lang.Class, java.lang.String) in that it does not throw an exception for an invalid enum name.

Parameters:
enumClass - the class of the enum to get, not null
enumName - the enum name
Returns:
the enum or null if not found


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.