Package org.conical.common.bbl.util
Class EnumUtil
java.lang.Object
org.conical.common.bbl.util.EnumUtil
Wrapper for static utility methods related to Java enums.
- Author:
- rdoherty
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Enum<?>>
TgetDefaultIfNullOrBad
(T defaultValue, String value) Examines the passed value and determines whether it can be converted into an enum value of the same type as the defaultValue.static String
getGenericDescription
(String name) Converts an enum name to a more human-readable description.static <T extends Enum<?>>
TgetNullSafeEnum
(Class<T> clazz, String value) Returns the value of the enum class passed in whose name is the same as the String passed in.getNullSafeName
(T enumValue) Returns the name (String) of an enum value passed in.static <T extends Coded>
TgetValueByCode
(Class<T> clazz, int codedValue) Returns the value of a Coded enum based on its code.
-
Constructor Details
-
EnumUtil
public EnumUtil()
-
-
Method Details
-
getNullSafeName
Returns the name (String) of an enum value passed in. Since simply calling name() can cause NullPointerExceptions, this method provides a "null-safe" way to get the name. If null is passed in, null is returned.- Type Parameters:
T
- the enum class- Parameters:
enumValue
- an enum value- Returns:
- the name of the enum value, or null if null was passed
-
getNullSafeEnum
Returns the value of the enum class passed in whose name is the same as the String passed in. If an empty or null String is passed in, returns null.- Type Parameters:
T
- type to be returned- Parameters:
clazz
- class of type to be returnedvalue
- value to be converted- Returns:
- enum value
- Throws:
IllegalArgumentException
- if value is non-empty, but can still not be converted
-
getValueByCode
Returns the value of a Coded enum based on its code.- Type Parameters:
T
- class of the Coded enum- Parameters:
clazz
- Class of the Coded enumcodedValue
- code- Returns:
- enum value
- Throws:
NoSuchElementException
- if no value of the given class is coded to the given value
-
getGenericDescription
Converts an enum name to a more human-readable description. The result can be handy for drop-down selects, etc. Typically enum values are all caps with an underscore separator (e.g. "MY_ENUM"). The result of this method is a mixed case value with a space separator (e.g. "My Enum").- Parameters:
name
- enum name- Returns:
- human readable description
-
getDefaultIfNullOrBad
Examines the passed value and determines whether it can be converted into an enum value of the same type as the defaultValue. If it can, converts it and returns. If not, or if the value is null, returns defaultValue.- Type Parameters:
T
- type of value to be returned- Parameters:
defaultValue
- returned if unable to convert valuevalue
- string value to be converted- Returns:
- Enum represented by value of the same type as defaultValue, or defaultValue if value is null or cannot be converted
-