Class EnumUtils
- java.lang.Object
-
- yakworks.commons.lang.EnumUtils
-
public class EnumUtils extends java.lang.ObjectUtility library to provide helper methods for Java enums.
#ThreadSafe#
- Since:
- 3.0
-
-
Constructor Summary
Constructors Constructor Description EnumUtils()This constructor is public to permit tools that require a JavaBean instance to operate.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E extends java.lang.Enum<E>>
EgetEnum(java.lang.Class<E> enumClass, java.lang.String enumName)Gets the enum for the class, returningnullif not found.static <E extends java.lang.Enum<E>>
EgetEnum(java.lang.Class<E> enumClass, java.lang.String enumName, E defaultEnum)Gets the enum for the class, returningdefaultEnumif not found.static <E extends java.lang.Enum<E>>
EgetEnumIgnoreCase(java.lang.Class<E> enumClass, java.lang.String enumName)Gets the enum for the class, returningnullif not found.static <E extends java.lang.Enum<E>>
EgetEnumIgnoreCase(java.lang.Class<E> enumClass, java.lang.String enumName, E defaultEnum)Gets the enum for the class, returningdefaultEnumif not found.static <E extends java.lang.Enum<E>>
java.util.List<E>getEnumList(java.lang.Class<E> enumClass)Gets theListof enums.static <E extends java.lang.Enum<E>>
java.util.Map<java.lang.String,E>getEnumMap(java.lang.Class<E> enumClass)Gets theMapof enums by name.static <E extends java.lang.Enum<E>>
booleanisValidEnum(java.lang.Class<E> enumClass, java.lang.String enumName)Checks if the specified name is a valid enum for the class.static <E extends java.lang.Enum<E>>
booleanisValidEnumIgnoreCase(java.lang.Class<E> enumClass, java.lang.String enumName)Checks if the specified name is a valid enum for the class.
-
-
-
Method Detail
-
getEnum
public static <E extends java.lang.Enum<E>> E getEnum(java.lang.Class<E> enumClass, java.lang.String enumName)Gets the enum for the class, returning
nullif not found.This method differs from
Enum.valueOf(java.lang.Class<T>, java.lang.String)in that it does not throw an exception for an invalid enum name.- Type Parameters:
E- the type of the enumeration- Parameters:
enumClass- the class of the enum to query, not nullenumName- the enum name, null returns null- Returns:
- the enum, null if not found
-
getEnum
public static <E extends java.lang.Enum<E>> E getEnum(java.lang.Class<E> enumClass, java.lang.String enumName, E defaultEnum)Gets the enum for the class, returning
defaultEnumif not found.This method differs from
Enum.valueOf(java.lang.Class<T>, java.lang.String)in that it does not throw an exception for an invalid enum name.- Type Parameters:
E- the type of the enumeration- Parameters:
enumClass- the class of the enum to query, not nullenumName- the enum name, null returns default enumdefaultEnum- the default enum- Returns:
- the enum, default enum if not found
- Since:
- 3.10
-
getEnumIgnoreCase
public static <E extends java.lang.Enum<E>> E getEnumIgnoreCase(java.lang.Class<E> enumClass, java.lang.String enumName)Gets the enum for the class, returning
nullif not found.This method differs from
Enum.valueOf(java.lang.Class<T>, java.lang.String)in that it does not throw an exception for an invalid enum name and performs case insensitive matching of the name.- Type Parameters:
E- the type of the enumeration- Parameters:
enumClass- the class of the enum to query, not nullenumName- the enum name, null returns null- Returns:
- the enum, null if not found
- Since:
- 3.8
-
getEnumIgnoreCase
public static <E extends java.lang.Enum<E>> E getEnumIgnoreCase(java.lang.Class<E> enumClass, java.lang.String enumName, E defaultEnum)Gets the enum for the class, returning
defaultEnumif not found.This method differs from
Enum.valueOf(java.lang.Class<T>, java.lang.String)in that it does not throw an exception for an invalid enum name and performs case insensitive matching of the name.- Type Parameters:
E- the type of the enumeration- Parameters:
enumClass- the class of the enum to query, not nullenumName- the enum name, null returns default enumdefaultEnum- the default enum- Returns:
- the enum, default enum if not found
- Since:
- 3.10
-
getEnumList
public static <E extends java.lang.Enum<E>> java.util.List<E> getEnumList(java.lang.Class<E> enumClass)
Gets the
Listof enums.This method is useful when you need a list of enums rather than an array.
- Type Parameters:
E- the type of the enumeration- Parameters:
enumClass- the class of the enum to query, not null- Returns:
- the modifiable list of enums, never null
-
getEnumMap
public static <E extends java.lang.Enum<E>> java.util.Map<java.lang.String,E> getEnumMap(java.lang.Class<E> enumClass)
Gets the
Mapof enums by name.This method is useful when you need a map of enums by name.
- Type Parameters:
E- the type of the enumeration- Parameters:
enumClass- the class of the enum to query, not null- Returns:
- the modifiable map of enum names to enums, never null
-
isValidEnum
public static <E extends java.lang.Enum<E>> boolean isValidEnum(java.lang.Class<E> enumClass, java.lang.String enumName)Checks if the specified name is a valid enum for the class.
This method differs from
Enum.valueOf(java.lang.Class<T>, java.lang.String)in that checks if the name is a valid enum without needing to catch the exception.- Type Parameters:
E- the type of the enumeration- Parameters:
enumClass- the class of the enum to query, not nullenumName- the enum name, null returns false- Returns:
- true if the enum name is valid, otherwise false
-
isValidEnumIgnoreCase
public static <E extends java.lang.Enum<E>> boolean isValidEnumIgnoreCase(java.lang.Class<E> enumClass, java.lang.String enumName)Checks if the specified name is a valid enum for the class.
This method differs from
Enum.valueOf(java.lang.Class<T>, java.lang.String)in that checks if the name is a valid enum without needing to catch the exception and performs case insensitive matching of the name.- Type Parameters:
E- the type of the enumeration- Parameters:
enumClass- the class of the enum to query, not nullenumName- the enum name, null returns false- Returns:
- true if the enum name is valid, otherwise false
-
-