EnumUtils¶
- public class org.apache.commons.lang3.EnumUtils[source]¶
Provides methods for Java enums.
#ThreadSafe#
- Since:
3.0
Constructors¶
Deprecated. This constructor is public to permit tools that require a JavaBean instance to operate. |
- @Deprecated public EnumUtils()[source]¶
Deprecated: TODO Make private in 4.0.
This constructor is public to permit tools that require a JavaBean instance to operate.
Methods¶
Creates a long bit vector representation of the given array of Enum values. |
|
Creates a long bit vector representation of the given subset of an Enum. |
|
Creates a bit vector representation of the given subset of an Enum using as many |
|
Creates a bit vector representation of the given subset of an Enum using as many |
|
Gets the enum for the class, returning |
|
Gets the enum for the class, returning |
|
Gets the enum for the class, returning |
|
Gets the enum for the class, returning |
|
Gets the |
|
Gets the |
|
Gets the |
|
Gets the enum for the class in a system property, returning |
|
Gets the enum for the class and value, returning |
|
Gets the enum for the class, returning |
|
Tests whether the specified name is a valid enum for the class. |
|
Tests whether the specified name is a valid enum for the class. |
|
Convert a long value created by |
|
Convert a |
|
Returns a sequential ordered stream whose elements are the given class’ enum values. |
- @SafeVarargs public static <E extends Enum<E>> long generateBitVector(Class<E> enumClass, E... values)[source]¶
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:
enumClass – The class of the enum we are working with, not
null.values – The values we want to convert, not
null.
- Type parameters:
<E> – the type of the enumeration.
- Returns:
A long whose value provides a binary representation of the given set of enum values.
- Throws:
java.lang.NullPointerException – Thrown if
enumClassorvaluesisnull.java.lang.IllegalArgumentException – Thrown if
enumClassis not an enum class or has more than 64 values.
- Since:
3.0.1
- See also:
- public static <E extends Enum<E>> long generateBitVector(Class<E> enumClass, Iterable<? extends E> values)[source]¶
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:
enumClass – The class of the enum we are working with, not
null.values – The values we want to convert, not
null, neither containingnull.
- Type parameters:
<E> – the type of the enumeration.
- Returns:
A long whose value provides a binary representation of the given set of enum values.
- Throws:
java.lang.NullPointerException – Thrown if
enumClassorvaluesisnull.java.lang.IllegalArgumentException – Thrown if
enumClassis not an enum class or has more than 64 values, or if anyvaluesnull.
- Since:
3.0.1
- See also:
- @SafeVarargs public static <E extends Enum<E>> long[] generateBitVectors(Class<E> enumClass, E... values)[source]¶
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:
enumClass – The class of the enum we are working with, not
null.values – The values we want to convert, not
null, neither containingnull.
- Type parameters:
<E> – the type of the enumeration.
- 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 – Thrown if
enumClassorvaluesisnull.java.lang.IllegalArgumentException – Thrown if
enumClassis not an enum class, or if anyvaluesnull.
- Since:
3.2
- public static <E extends Enum<E>> long[] generateBitVectors(Class<E> enumClass, Iterable<? extends E> values)[source]¶
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:
enumClass – The class of the enum we are working with, not
null.values – The values we want to convert, not
null, neither containingnull.
- Type parameters:
<E> – the type of the enumeration.
- 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 – Thrown if
enumClassorvaluesisnull.java.lang.IllegalArgumentException – Thrown if
enumClassis not an enum class, or if anyvaluesnull.
- Since:
3.2
- public static <E extends Enum<E>> E getEnum(Class<E> enumClass, String enumName)[source]¶
Gets the enum for the class, returning
nullif not found.This method differs from
Enum#valueOfin 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.
- public static <E extends Enum<E>> E getEnum(Class<E> enumClass, String enumName, E defaultEnum)[source]¶
Gets the enum for the class, returning
defaultEnumif not found.This method differs from
Enum#valueOfin 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
- public static <E extends Enum<E>> E getEnumIgnoreCase(Class<E> enumClass, String enumName)[source]¶
Gets the enum for the class, returning
nullif not found.This method differs from
Enum#valueOfin 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
- public static <E extends Enum<E>> E getEnumIgnoreCase(Class<E> enumClass, String enumName, E defaultEnum)[source]¶
Gets the enum for the class, returning
defaultEnumif not found.This method differs from
Enum#valueOfin 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
- public static <E extends Enum<E>> List<E> getEnumList(Class<E> enumClass)[source]¶
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.
- public static <E extends Enum<E>> Map<String, E> getEnumMap(Class<E> enumClass)[source]¶
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.
- public static <E extends Enum<E>, K> Map<K, E> getEnumMap(Class<E> enumClass, Function<E, K> keyFunction)[source]¶
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 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
- public static <E extends Enum<E>> E getEnumSystemProperty(Class<E> enumClass, String propName, E defaultEnum)[source]¶
Gets the enum for the class in a system property, returning
defaultEnumif not found.This method differs from
Enum#valueOfin that it does not throw an exception for an invalid enum name.If a
SecurityExceptionis 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
- public static <E extends Enum<E>> E getFirstEnum(Class<E> enumClass, int value, ToIntFunction<E> toIntFunction, E defaultEnum)[source]¶
Gets the enum for the class and value, returning
defaultEnumif not found.This method differs from
Enum#valueOfin 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 to
value.defaultEnum – The default enum.
- Returns:
An enum, default enum if not found.
- Since:
3.18.0
- public static <E extends Enum<E>> E getFirstEnumIgnoreCase(Class<E> enumClass, String enumName, Function<E, String> stringFunction, E defaultEnum)[source]¶
Gets the enum for the class, returning
defaultEnumif not found.This method differs from
Enum#valueOfin 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 to
enumName.defaultEnum – The default enum.
- Returns:
An enum, default enum if not found.
- Since:
3.13.0
- public static <E extends Enum<E>> boolean isValidEnum(Class<E> enumClass, String enumName)[source]¶
Tests whether the specified name is a valid enum for the class.
This method differs from
Enum#valueOfin 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.
- public static <E extends Enum<E>> boolean isValidEnumIgnoreCase(Class<E> enumClass, String enumName)[source]¶
Tests whether the specified name is a valid enum for the class.
This method differs from
Enum#valueOfin 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
- public static <E extends Enum<E>> EnumSet<E> processBitVector(Class<E> enumClass, long value)[source]¶
Convert a long value created by
EnumUtils#generateBitVectorinto 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:
enumClass – The class of the enum we are working with, not
null.value – The long value representation of a set of enum values.
- Type parameters:
<E> – the type of the enumeration.
- Returns:
A set of enum values.
- Throws:
java.lang.NullPointerException – Thrown if
enumClassisnull.java.lang.IllegalArgumentException – Thrown if
enumClassis not an enum class or has more than 64 values.
- Since:
3.0.1
- public static <E extends Enum<E>> EnumSet<E> processBitVectors(Class<E> enumClass, long... values)[source]¶
Convert a
long[]created byEnumUtils#generateBitVectorsinto 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:
enumClass – The class of the enum we are working with, not
null.values – The long[] bearing the representation of a set of enum values, the least significant digits rightmost, not
null.
- Type parameters:
<E> – the type of the enumeration.
- Returns:
A set of enum values.
- Throws:
java.lang.NullPointerException – Thrown if
enumClassisnull.java.lang.IllegalArgumentException – Thrown if
enumClassis not an enum class.
- Since:
3.2
- public static <T> Stream<T> stream(Class<T> clazz)[source]¶
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
clazzis null.- Since:
3.18.0
- See also: