Top Description Fields Constructors Methods
org.apache.commons.lang3

public Class EnumUtils

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Arrays, .Collections, .EnumSet, .List, .Map, .Objects, java.util.function.Function, .ToIntFunction, java.util.stream.Collectors, .Stream, org.apache.commons.lang3.stream.Streams

Provides methods for Java enums.

#ThreadSafe#

Since
3.0

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private static final String
private static final String

Constructor Summary

AccessConstructor and Description
public
EnumUtils()
Deprecated TODO Make private in 4.0.

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

Method Summary

Modifier and TypeMethod and Description
private static <
the type of the enumeration.
E extends Enum<E>
>
Class<E>

Returns:

enumClass.
asEnum
(final Class<E>
to check.
enumClass
)

Validate enumClass.

private static <
the type of the enumeration.
E extends Enum<E>
>
Class<E>

Returns:

enumClass.
checkBitVectorable
(final Class<E>
to check.
enumClass
)

Validate that enumClass is compatible with representation in a long.

public static <
the type of the enumeration.
E extends Enum<E>
>
long

Returns:

a long whose value provides a binary representation of the given set of enum values.
generateBitVector
(final Class<E>
the class of the enum we are working with, not null.
enumClass
,
final E...
the values we want to convert, not null.
values
)

Creates a long bit vector representation of the given array of Enum values.

public static <
the type of the enumeration.
E extends Enum<E>
>
long

Returns:

a long whose value provides a binary representation of the given set of enum values.
generateBitVector
(final Class<E>
the class of the enum we are working with, not null.
enumClass
,
final Iterable<? extends E>
the values we want to convert, not null, neither containing null.
values
)

Creates a long bit vector representation of the given subset of an Enum.

public static <
the type of the enumeration.
E extends Enum<E>
>
long[]

Returns:

a long[] whose values provide a binary representation of the given set of enum values with the least significant digits rightmost.
generateBitVectors
(final Class<E>
the class of the enum we are working with, not null.
enumClass
,
final E...
the values we want to convert, not null, neither containing null.
values
)

Creates a bit vector representation of the given subset of an Enum using as many longs as needed.

public static <
the type of the enumeration.
E extends Enum<E>
>
long[]

Returns:

a long[] whose values provide a binary representation of the given set of enum values with the least significant digits rightmost.
generateBitVectors
(final Class<E>
the class of the enum we are working with, not null.
enumClass
,
final Iterable<? extends E>
the values we want to convert, not null, neither containing null.
values
)

Creates a bit vector representation of the given subset of an Enum using as many longs as needed.

public static <
the type of the enumeration.
E extends Enum<E>
>
E

Returns:

the enum, null if not found.
getEnum
(final Class<E>
the class of the enum to query, not null.
enumClass
,
final String
the enum name, null returns null.
enumName
)

Gets the enum for the class, returning null if not found.

public static <
the type of the enumeration.
E extends Enum<E>
>
E

Returns:

the enum, default enum if not found.
getEnum
(final Class<E>
the class of the enum to query, null returns default enum.
enumClass
,
final String
the enum name, null returns default enum.
enumName
,
final E
the default enum.
defaultEnum
)

Gets the enum for the class, returning defaultEnum if not found.

public static <
the type of the enumeration.
E extends Enum<E>
>
E

Returns:

the enum, null if not found.
getEnumIgnoreCase
(final Class<E>
the class of the enum to query, may be null.
enumClass
,
final String
the enum name, null returns null.
enumName
)

Gets the enum for the class, returning null if not found.

public static <
the type of the enumeration.
E extends Enum<E>
>
E

Returns:

the enum, default enum if not found.
getEnumIgnoreCase
(final Class<E>
the class of the enum to query, null returns default enum.
enumClass
,
final String
the enum name, null returns default enum.
enumName
,
final E
the default enum.
defaultEnum
)

Gets the enum for the class, returning defaultEnum if not found.

public static <
the type of the enumeration.
E extends Enum<E>
>
List<E>

Returns:

the modifiable list of enums, never null.
getEnumList
(final Class<E>
the class of the enum to query, not null.
enumClass
)

Gets the List of enums.

public static <
the type of the enumeration.
E extends Enum<E>
>
Map<String, E>

Returns:

the modifiable map of enum names to enums, never null.
getEnumMap
(final Class<E>
the class of the enum to query, not null.
enumClass
)

Gets the Map of enums by name.

public static <
the type of enumeration.
E extends Enum<E>
,
the type of the map key.
K
>
Map<K, E>

Returns:

the modifiable map of enums, never null.
getEnumMap
(final Class<E>
the class of the enum to query, not null.
enumClass
,
final Function<E, K>
the function to query for the key, not null.
keyFunction
)

Gets the Map of enums by name.

public static <
the type of the enumeration.
E extends Enum<E>
>
E

Returns:

the enum, default enum if not found.
getEnumSystemProperty
(final Class<E>
the class of the enum to query, not null.
enumClass
,
final String
the system property key for the enum name, null returns default enum.
propName
,
final E
the default enum.
defaultEnum
)

Gets the enum for the class in a system property, returning defaultEnum if not found.

public static <
the type of the enumeration.
E extends Enum<E>
>
E

Returns:

an enum, default enum if not found.
getFirstEnum
(final Class<E>
the class of the enum to query, not null.
enumClass
,
final int
the enum name, null returns default enum.
value
,
final ToIntFunction<E>
the function that gets an int for an enum for comparison to value.
toIntFunction
,
final E
the default enum.
defaultEnum
)

Gets the enum for the class and value, returning defaultEnum if not found.

public static <
the type of the enumeration.
E extends Enum<E>
>
E

Returns:

an enum, default enum if not found.
getFirstEnumIgnoreCase
(final Class<E>
the class of the enum to query, null returns default enum.
enumClass
,
final String
the enum name, null returns default enum.
enumName
,
final Function<E, String>
the function that gets the string for an enum for comparison to enumName.
stringFunction
,
final E
the default enum.
defaultEnum
)

Gets the enum for the class, returning defaultEnum if not found.

private static <E extends Enum<E>> boolean
isEnum(final Class<E> enumClass)

public static <
the type of the enumeration.
E extends Enum<E>
>
boolean

Returns:

true if the enum name is valid, otherwise false.
isValidEnum
(final Class<E>
the class of the enum to query, null returns false.
enumClass
,
final String
the enum name, null returns false.
enumName
)

Checks if the specified name is a valid enum for the class.

public static <
the type of the enumeration.
E extends Enum<E>
>
boolean

Returns:

true if the enum name is valid, otherwise false.
isValidEnumIgnoreCase
(final Class<E>
the class of the enum to query, null returns false.
enumClass
,
final String
the enum name, null returns false.
enumName
)

Checks if the specified name is a valid enum for the class.

public static <
the type of the enumeration.
E extends Enum<E>
>
EnumSet<E>

Returns:

a set of enum values.
processBitVector
(final Class<E>
the class of the enum we are working with, not null.
enumClass
,
final long
the long value representation of a set of enum values.
value
)

Convert a long value created by EnumUtils#generateBitVector into the set of enum values that it represents.

public static <
the type of the enumeration.
E extends Enum<E>
>
EnumSet<E>

Returns:

a set of enum values.
processBitVectors
(final Class<E>
the class of the enum we are working with, not null.
enumClass
,
final long...
the long[] bearing the representation of a set of enum values, the least significant digits rightmost, not null.
values
)

Convert a long[] created by EnumUtils#generateBitVectors into the set of enum values that it represents.

public static <
the type of stream elements.
T
>
Stream<T>

Returns:

the new stream, empty of clazz is null.
stream
(final Class<T>
the class containing the enum values, may be null.
clazz
)

Returns a sequential ordered stream whose elements are the given class' enum values.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CANNOT_STORE_S_S_VALUES_IN_S_BITSback to summary
private static final String CANNOT_STORE_S_S_VALUES_IN_S_BITS
ENUM_CLASS_MUST_BE_DEFINEDback to summary
private static final String ENUM_CLASS_MUST_BE_DEFINED
NULL_ELEMENTS_NOT_PERMITTEDback to summary
private static final String NULL_ELEMENTS_NOT_PERMITTED
S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPEback to summary
private static final String S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE

Constructor Detail

EnumUtilsback to summary
public EnumUtils()

Deprecated

TODO Make private in 4.0.

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

Annotations
@Deprecated

Method Detail

asEnumback to summary
private static <E extends Enum<E>> Class<E> asEnum(final Class<E> enumClass)

Validate enumClass.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

to check.

Returns:Class<E>

enumClass.

Exceptions
NullPointerException:
if enumClass is null.
IllegalArgumentException:
if enumClass is not an enum class.
Since
3.2
checkBitVectorableback to summary
private static <E extends Enum<E>> Class<E> checkBitVectorable(final Class<E> enumClass)

Validate that enumClass is compatible with representation in a long.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

to check.

Returns:Class<E>

enumClass.

Exceptions
NullPointerException:
if enumClass is null.
IllegalArgumentException:
if enumClass is not an enum class or has more than 64 values.
Since
3.0.1
generateBitVectorback to summary
public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values)

Creates a long bit vector representation of the given array of Enum values.

This generates a value that is usable by EnumUtils#processBitVector.

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.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum we are working with, not null.

values:E[]

the values we want to convert, not null.

Returns:long

a long whose value provides a binary representation of the given set of enum values.

Annotations
@SafeVarargs
Exceptions
NullPointerException:
if enumClass or values is null.
IllegalArgumentException:
if enumClass is not an enum class or has more than 64 values.
Since
3.0.1
See Also
generateBitVectors(Class, Iterable)
generateBitVectorback to summary
public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final 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 EnumUtils#processBitVector.

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.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum we are working with, not null.

values:Iterable<? extends E>

the values we want to convert, not null, neither containing null.

Returns:long

a long whose value provides a binary representation of the given set of enum values.

Exceptions
NullPointerException:
if enumClass or values is null.
IllegalArgumentException:
if enumClass is not an enum class or has more than 64 values, or if any values null.
Since
3.0.1
See Also
generateBitVectors(Class, Iterable)
generateBitVectorsback to summary
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values)

Creates a bit vector representation of the given subset of an Enum using as many longs as needed.

This generates a value that is usable by EnumUtils#processBitVectors.

Use this method if you have more than 64 values in your Enum.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum we are working with, not null.

values:E[]

the values we want to convert, not null, neither containing null.

Returns:long[]

a long[] whose values provide a binary representation of the given set of enum values with the least significant digits rightmost.

Annotations
@SafeVarargs
Exceptions
NullPointerException:
if enumClass or values is null.
IllegalArgumentException:
if enumClass is not an enum class, or if any values null.
Since
3.2
generateBitVectorsback to summary
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final Iterable<? extends E> values)

Creates a bit vector representation of the given subset of an Enum using as many longs as needed.

This generates a value that is usable by EnumUtils#processBitVectors.

Use this method if you have more than 64 values in your Enum.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum we are working with, not null.

values:Iterable<? extends E>

the values we want to convert, not null, neither containing null.

Returns:long[]

a long[] whose values provide a binary representation of the given set of enum values with the least significant digits rightmost.

Exceptions
NullPointerException:
if enumClass or values is null.
IllegalArgumentException:
if enumClass is not an enum class, or if any values null.
Since
3.2
getEnumback to summary
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName)

Gets the enum for the class, returning null if not found.

This method differs from Enum#valueOf in that it does not throw an exception for an invalid enum name.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, not null.

enumName:String

the enum name, null returns null.

Returns:E

the enum, null if not found.

getEnumback to summary
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName, final E defaultEnum)

Gets the enum for the class, returning defaultEnum if not found.

This method differs from Enum#valueOf in that it does not throw an exception for an invalid enum name.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, null returns default enum.

enumName:String

the enum name, null returns default enum.

defaultEnum:E

the default enum.

Returns:E

the enum, default enum if not found.

Since
3.10
getEnumIgnoreCaseback to summary
public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass, final String enumName)

Gets the enum for the class, returning null if not found.

This method differs from Enum#valueOf in that it does not throw an exception for an invalid enum name and performs case insensitive matching of the name.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, may be null.

enumName:String

the enum name, null returns null.

Returns:E

the enum, null if not found.

Since
3.8
getEnumIgnoreCaseback to summary
public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass, final String enumName, final E defaultEnum)

Gets the enum for the class, returning defaultEnum if not found.

This method differs from Enum#valueOf in that it does not throw an exception for an invalid enum name and performs case insensitive matching of the name.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, null returns default enum.

enumName:String

the enum name, null returns default enum.

defaultEnum:E

the default enum.

Returns:E

the enum, default enum if not found.

Since
3.10
getEnumListback to summary
public static <E extends Enum<E>> List<E> getEnumList(final Class<E> enumClass)

Gets the List of enums.

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

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, not null.

Returns:List<E>

the modifiable list of enums, never null.

getEnumMapback to summary
public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass)

Gets the Map of enums by name.

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

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, not null.

Returns:Map<String, E>

the modifiable map of enum names to enums, never null.

getEnumMapback to summary
public static <E extends Enum<E>, K> Map<K, E> getEnumMap(final Class<E> enumClass, final Function<E, K> keyFunction)

Gets the Map of enums by name.

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

Parameters
<E>
the type of enumeration.
<K>
the type of the map key.
enumClass:Class<E>

the class of the enum to query, not null.

keyFunction:Function<E, K>

the function to query for the key, not null.

Returns:Map<K, E>

the modifiable map of enums, never null.

Since
3.13.0
getEnumSystemPropertyback to summary
public static <E extends Enum<E>> E getEnumSystemProperty(final Class<E> enumClass, final String propName, final E defaultEnum)

Gets the enum for the class in a system property, returning defaultEnum if not found.

This method differs from Enum#valueOf in that it does not throw an exception for an invalid enum name.

If a SecurityException is caught, the return value is null.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, not null.

propName:String

the system property key for the enum name, null returns default enum.

defaultEnum:E

the default enum.

Returns:E

the enum, default enum if not found.

Since
3.13.0
getFirstEnumback to summary
public static <E extends Enum<E>> E getFirstEnum(final Class<E> enumClass, final int value, final ToIntFunction<E> toIntFunction, final E defaultEnum)

Gets the enum for the class and value, returning defaultEnum if not found.

This method differs from Enum#valueOf in that it does not throw an exception for an invalid enum name and performs case insensitive matching of the name.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, not null.

value:int

the enum name, null returns default enum.

toIntFunction:ToIntFunction<E>

the function that gets an int for an enum for comparison to value.

defaultEnum:E

the default enum.

Returns:E

an enum, default enum if not found.

Since
3.18.0
getFirstEnumIgnoreCaseback to summary
public static <E extends Enum<E>> E getFirstEnumIgnoreCase(final Class<E> enumClass, final String enumName, final Function<E, String> stringFunction, final E defaultEnum)

Gets the enum for the class, returning defaultEnum if not found.

This method differs from Enum#valueOf in that it does not throw an exception for an invalid enum name and performs case insensitive matching of the name.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, null returns default enum.

enumName:String

the enum name, null returns default enum.

stringFunction:Function<E, String>

the function that gets the string for an enum for comparison to enumName.

defaultEnum:E

the default enum.

Returns:E

an enum, default enum if not found.

Since
3.13.0
isEnumback to summary
private static <E extends Enum<E>> boolean isEnum(final Class<E> enumClass)
isValidEnumback to summary
public static <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass, final String enumName)

Checks if the specified name is a valid enum for the class.

This method differs from Enum#valueOf in that it checks if the name is a valid enum without needing to catch the exception.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, null returns false.

enumName:String

the enum name, null returns false.

Returns:boolean

true if the enum name is valid, otherwise false.

isValidEnumIgnoreCaseback to summary
public static <E extends Enum<E>> boolean isValidEnumIgnoreCase(final Class<E> enumClass, final String enumName)

Checks if the specified name is a valid enum for the class.

This method differs from Enum#valueOf 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.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum to query, null returns false.

enumName:String

the enum name, null returns false.

Returns:boolean

true if the enum name is valid, otherwise false.

Since
3.8
processBitVectorback to summary
public static <E extends Enum<E>> EnumSet<E> processBitVector(final Class<E> enumClass, final long value)

Convert a long value created by EnumUtils#generateBitVector 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.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum we are working with, not null.

value:long

the long value representation of a set of enum values.

Returns:EnumSet<E>

a set of enum values.

Exceptions
NullPointerException:
if enumClass is null.
IllegalArgumentException:
if enumClass is not an enum class or has more than 64 values.
Since
3.0.1
processBitVectorsback to summary
public static <E extends Enum<E>> EnumSet<E> processBitVectors(final Class<E> enumClass, final long... values)

Convert a long[] created by EnumUtils#generateBitVectors 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.

Parameters
<E>
the type of the enumeration.
enumClass:Class<E>

the class of the enum we are working with, not null.

values:long[]

the long[] bearing the representation of a set of enum values, the least significant digits rightmost, not null.

Returns:EnumSet<E>

a set of enum values.

Exceptions
NullPointerException:
if enumClass is null.
IllegalArgumentException:
if enumClass is not an enum class.
Since
3.2
streamback to summary
public static <T> Stream<T> stream(final Class<T> clazz)

Returns a sequential ordered stream whose elements are the given class' enum values.

Parameters
<T>
the type of stream elements.
clazz:Class<T>

the class containing the enum values, may be null.

Returns:Stream<T>

the new stream, empty of clazz is null.

Since
3.18.0
See Also
Class#getEnumConstants()