Class EnumUtils
- java.lang.Object
-
- org.apache.commons.lang3.EnumUtils
-
public class EnumUtils extends java.lang.Object
Provides methods for Java enums.#ThreadSafe#
- Since:
- 3.0
-
-
Constructor Summary
Constructors Constructor Description EnumUtils()
Deprecated.TODO Make private in 4.0.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E extends java.lang.Enum<E>>
longgenerateBitVector(java.lang.Class<E> enumClass, E... values)
Creates a long bit vector representation of the given array of Enum values.static <E extends java.lang.Enum<E>>
longgenerateBitVector(java.lang.Class<E> enumClass, java.lang.Iterable<? extends E> values)
Creates a long bit vector representation of the given subset of an Enum.static <E extends java.lang.Enum<E>>
long[]generateBitVectors(java.lang.Class<E> enumClass, E... values)
Creates a bit vector representation of the given subset of an Enum using as manylong
s as needed.static <E extends java.lang.Enum<E>>
long[]generateBitVectors(java.lang.Class<E> enumClass, java.lang.Iterable<? extends E> values)
Creates a bit vector representation of the given subset of an Enum using as manylong
s as needed.static <E extends java.lang.Enum<E>>
EgetEnum(java.lang.Class<E> enumClass, java.lang.String enumName)
Gets the enum for the class, returningnull
if 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, returningdefaultEnum
if 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, returningnull
if 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, returningdefaultEnum
if not found.static <E extends java.lang.Enum<E>>
java.util.List<E>getEnumList(java.lang.Class<E> enumClass)
Gets theList
of enums.static <E extends java.lang.Enum<E>>
java.util.Map<java.lang.String,E>getEnumMap(java.lang.Class<E> enumClass)
Gets theMap
of enums by name.static <E extends java.lang.Enum<E>,K>
java.util.Map<K,E>getEnumMap(java.lang.Class<E> enumClass, java.util.function.Function<E,K> keyFunction)
Gets theMap
of enums by name.static <E extends java.lang.Enum<E>>
EgetEnumSystemProperty(java.lang.Class<E> enumClass, java.lang.String propName, E defaultEnum)
Gets the enum for the class in a system property, returningdefaultEnum
if not found.static <E extends java.lang.Enum<E>>
EgetFirstEnum(java.lang.Class<E> enumClass, int value, java.util.function.ToIntFunction<E> toIntFunction, E defaultEnum)
Gets the enum for the class and value, returningdefaultEnum
if not found.static <E extends java.lang.Enum<E>>
EgetFirstEnumIgnoreCase(java.lang.Class<E> enumClass, java.lang.String enumName, java.util.function.Function<E,java.lang.String> stringFunction, E defaultEnum)
Gets the enum for the class, returningdefaultEnum
if not found.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.static <E extends java.lang.Enum<E>>
java.util.EnumSet<E>processBitVector(java.lang.Class<E> enumClass, long value)
Convert a long value created bygenerateBitVector(java.lang.Class<E>, E...)
into the set of enum values that it represents.static <E extends java.lang.Enum<E>>
java.util.EnumSet<E>processBitVectors(java.lang.Class<E> enumClass, long... values)
Convert along[]
created bygenerateBitVectors(java.lang.Class<E>, E...)
into the set of enum values that it represents.static <T> java.util.stream.Stream<T>
stream(java.lang.Class<T> clazz)
Returns a sequential ordered stream whose elements are the given class' enum values.
-
-
-
Method Detail
-
generateBitVector
@SafeVarargs public static <E extends java.lang.Enum<E>> long generateBitVector(java.lang.Class<E> enumClass, E... values)
Creates a long bit vector representation of the given array of Enum values.This generates a value that is usable by
processBitVector(java.lang.Class<E>, long)
.Do not use this method if you have more than 64 values in your Enum, as this would create a value greater than a long can hold.
- Type Parameters:
E
- the type of the enumeration.- Parameters:
enumClass
- the class of the enum we are working with, notnull
.values
- the values we want to convert, notnull
.- Returns:
- a long whose value provides a binary representation of the given set of enum values.
- Throws:
java.lang.NullPointerException
- ifenumClass
orvalues
isnull
.java.lang.IllegalArgumentException
- ifenumClass
is not an enum class or has more than 64 values.- Since:
- 3.0.1
- See Also:
generateBitVectors(Class, Iterable)
-
generateBitVector
public static <E extends java.lang.Enum<E>> long generateBitVector(java.lang.Class<E> enumClass, java.lang.Iterable<? extends E> values)
Creates a long bit vector representation of the given subset of an Enum.This generates a value that is usable by
processBitVector(java.lang.Class<E>, long)
.Do not use this method if you have more than 64 values in your Enum, as this would create a value greater than a long can hold.
- Type Parameters:
E
- the type of the enumeration.- Parameters:
enumClass
- the class of the enum we are working with, notnull
.values
- the values we want to convert, notnull
, neither containingnull
.- Returns:
- a long whose value provides a binary representation of the given set of enum values.
- Throws:
java.lang.NullPointerException
- ifenumClass
orvalues
isnull
.java.lang.IllegalArgumentException
- ifenumClass
is not an enum class or has more than 64 values, or if anyvalues
null
.- Since:
- 3.0.1
- See Also:
generateBitVectors(Class, Iterable)
-
generateBitVectors
@SafeVarargs public static <E extends java.lang.Enum<E>> long[] generateBitVectors(java.lang.Class<E> enumClass, E... values)
Creates a bit vector representation of the given subset of an Enum using as manylong
s as needed.This generates a value that is usable by
processBitVectors(java.lang.Class<E>, long...)
.Use this method if you have more than 64 values in your Enum.
- Type Parameters:
E
- the type of the enumeration.- Parameters:
enumClass
- the class of the enum we are working with, notnull
.values
- the values we want to convert, notnull
, neither containingnull
.- Returns:
- a long[] whose values provide a binary representation of the given set of enum values with the least significant digits rightmost.
- Throws:
java.lang.NullPointerException
- ifenumClass
orvalues
isnull
.java.lang.IllegalArgumentException
- ifenumClass
is not an enum class, or if anyvalues
null
.- Since:
- 3.2
-
generateBitVectors
public static <E extends java.lang.Enum<E>> long[] generateBitVectors(java.lang.Class<E> enumClass, java.lang.Iterable<? extends E> values)
Creates a bit vector representation of the given subset of an Enum using as manylong
s as needed.This generates a value that is usable by
processBitVectors(java.lang.Class<E>, long...)
.Use this method if you have more than 64 values in your Enum.
- Type Parameters:
E
- the type of the enumeration.- Parameters:
enumClass
- the class of the enum we are working with, notnull
.values
- the values we want to convert, notnull
, neither containingnull
.- Returns:
- a long[] whose values provide a binary representation of the given set of enum values with the least significant digits rightmost.
- Throws:
java.lang.NullPointerException
- ifenumClass
orvalues
isnull
.java.lang.IllegalArgumentException
- ifenumClass
is not an enum class, or if anyvalues
null
.- Since:
- 3.2
-
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, returningnull
if 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 null.enumName
- 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, returningdefaultEnum
if 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, null returns default enum.enumName
- the enum name, null returns default enum.defaultEnum
- 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, returningnull
if 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, may be null.enumName
- 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, returningdefaultEnum
if 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, null returns default enum.enumName
- the enum name, null returns default enum.defaultEnum
- 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 theList
of 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 theMap
of 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.
-
getEnumMap
public static <E extends java.lang.Enum<E>,K> java.util.Map<K,E> getEnumMap(java.lang.Class<E> enumClass, java.util.function.Function<E,K> keyFunction)
Gets theMap
of enums by name.This method is useful when you need a map of enums by name.
- Type Parameters:
E
- the type of enumeration.K
- the type of the map key.- Parameters:
enumClass
- the class of the enum to query, not null.keyFunction
- the function to query for the key, not null.- Returns:
- the modifiable map of enums, never null.
- Since:
- 3.13.0
-
getEnumSystemProperty
public static <E extends java.lang.Enum<E>> E getEnumSystemProperty(java.lang.Class<E> enumClass, java.lang.String propName, E defaultEnum)
Gets the enum for the class in a system property, returningdefaultEnum
if 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.If a
SecurityException
is caught, the return value isnull
.- Type Parameters:
E
- the type of the enumeration.- Parameters:
enumClass
- the class of the enum to query, not null.propName
- the system property key for the enum name, null returns default enum.defaultEnum
- the default enum.- Returns:
- the enum, default enum if not found.
- Since:
- 3.13.0
-
getFirstEnum
public static <E extends java.lang.Enum<E>> E getFirstEnum(java.lang.Class<E> enumClass, int value, java.util.function.ToIntFunction<E> toIntFunction, E defaultEnum)
Gets the enum for the class and value, returningdefaultEnum
if 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 null.value
- the enum name, null returns default enum.toIntFunction
- the function that gets an int for an enum for comparison tovalue
.defaultEnum
- the default enum.- Returns:
- an enum, default enum if not found.
- Since:
- 3.18.0
-
getFirstEnumIgnoreCase
public static <E extends java.lang.Enum<E>> E getFirstEnumIgnoreCase(java.lang.Class<E> enumClass, java.lang.String enumName, java.util.function.Function<E,java.lang.String> stringFunction, E defaultEnum)
Gets the enum for the class, returningdefaultEnum
if 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, null returns default enum.enumName
- the enum name, null returns default enum.stringFunction
- the function that gets the string for an enum for comparison toenumName
.defaultEnum
- the default enum.- Returns:
- an enum, default enum if not found.
- Since:
- 3.13.0
-
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 it 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, null returns false.enumName
- 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 it 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, null returns false.enumName
- the enum name, null returns false.- Returns:
- true if the enum name is valid, otherwise false.
- Since:
- 3.8
-
processBitVector
public static <E extends java.lang.Enum<E>> java.util.EnumSet<E> processBitVector(java.lang.Class<E> enumClass, long value)
Convert a long value created bygenerateBitVector(java.lang.Class<E>, E...)
into the set of enum values that it represents.If you store this value, beware any changes to the enum that would affect ordinal values.
- Type Parameters:
E
- the type of the enumeration.- Parameters:
enumClass
- the class of the enum we are working with, notnull
.value
- the long value representation of a set of enum values.- Returns:
- a set of enum values.
- Throws:
java.lang.NullPointerException
- ifenumClass
isnull
.java.lang.IllegalArgumentException
- ifenumClass
is not an enum class or has more than 64 values.- Since:
- 3.0.1
-
processBitVectors
public static <E extends java.lang.Enum<E>> java.util.EnumSet<E> processBitVectors(java.lang.Class<E> enumClass, long... values)
Convert along[]
created bygenerateBitVectors(java.lang.Class<E>, E...)
into the set of enum values that it represents.If you store this value, beware any changes to the enum that would affect ordinal values.
- Type Parameters:
E
- the type of the enumeration.- Parameters:
enumClass
- the class of the enum we are working with, notnull
.values
- the long[] bearing the representation of a set of enum values, the least significant digits rightmost, notnull
.- Returns:
- a set of enum values.
- Throws:
java.lang.NullPointerException
- ifenumClass
isnull
.java.lang.IllegalArgumentException
- ifenumClass
is not an enum class.- Since:
- 3.2
-
stream
public static <T> java.util.stream.Stream<T> stream(java.lang.Class<T> clazz)
Returns a sequential ordered stream whose elements are the given class' enum values.- Type Parameters:
T
- the type of stream elements.- Parameters:
clazz
- the class containing the enum values, may be null.- Returns:
- the new stream, empty of
clazz
is null. - Since:
- 3.18.0
- See Also:
Class.getEnumConstants()
-
-