Class EnumUtils
java.lang.Object
fr.inria.rules.integraal.util.enumerations.EnumUtils
Utility class providing various facilities for working with enumerations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Enum<?> findConstantInEnumeration(String value, Class<? extends Enum<?>> enumClass) Searches for an enum constant of the specified type that matches the given value.static Enum<?> findConstantInNestedEnumeration(Class<?> clazz, String name) Recursively searches through an enumeration and its nested enums to find a constant matching the given name.findEnumClassInNestedEnumeration(String value, Class<? extends Enum<?>> enumClass) Recursively searches for an enum class that matches the given value by exploring the nested enum classes from the given root enum class.static <E extends Enum<E>>
EfindEnumFromString(String value, Class<E> clazz) Searches for an enum constant of the specified type that matches the given value.findNestedEnumClassByName(Class<? extends Enum<?>> clazz, String name) Recursively searches through an enum class and its nested enums to find a nested enum class that matches the given name.getAllConstantsAsString(Class<E> enumClass) Returns an array of strings containing all the constants of the specified enum type.static <E extends Enum<E>>
EgetEnumerationFromString(String value, Class<E> clazz) Returns the enumeration constant matching the supplied string, ornullwhen the string is absent.
-
Constructor Details
-
EnumUtils
public EnumUtils()Creates the enumeration utility.
-
-
Method Details
-
findEnumFromString
public static <E extends Enum<E>> E findEnumFromString(String value, Class<E> clazz) throws IllegalArgumentException Searches for an enum constant of the specified type that matches the given value. The search is case-insensitive. If a matching enum constant is found, it is returned. If no match is found, anIllegalArgumentExceptionis thrown with a detailed error message. Additionally, the method performs checks to ensure that neither the enum class nor the value is null or empty.- Type Parameters:
E- The enum type whose constants are being searched.- Parameters:
value- The string representation of the enum constant to search for. This comparison is case-insensitive.clazz- TheClassobject of the enum type in which to search.- Returns:
- The enum constant that matches the given value.
- Throws:
IllegalArgumentException- If the enum class or value is null or empty, or if no matching enum constant is found.
-
getAllConstantsAsString
Returns an array of strings containing all the constants of the specified enum type. This can be useful for logging or displaying the available options for an enum.- Type Parameters:
E- The enum type.- Parameters:
enumClass- TheClassobject of the enum type whose constants are to be listed.- Returns:
- An array of strings representing the names of the constants of the enum type.
-
findNestedEnumClassByName
public static Class<? extends Enum<?>> findNestedEnumClassByName(Class<? extends Enum<?>> clazz, String name) Recursively searches through an enum class and its nested enums to find a nested enum class that matches the given name.- Parameters:
clazz- The enum class to start the search with.name- The name of the nested enum class to find.- Returns:
- The matching nested enum class if found, otherwise null.
-
findEnumClassInNestedEnumeration
public static Class<? extends Enum<?>> findEnumClassInNestedEnumeration(String value, Class<? extends Enum<?>> enumClass) Recursively searches for an enum class that matches the given value by exploring the nested enum classes from the given root enum class. The search is case-insensitive. If a matching enum class is found, it is returned. Else null is returned. AnIllegalArgumentExceptionis thrown if the given class is null or is not an enum class or if the value is null or empty.- Parameters:
value- The string representation of the enum class to search for. This comparison is case-insensitive.enumClass- TheClassobject of the enum type in which to search.- Returns:
- The enum class if found, otherwise null.
- Throws:
IllegalArgumentException- If the class is null or not an enum class or the value is null or empty.
-
findConstantInEnumeration
Searches for an enum constant of the specified type that matches the given value. The search is case-insensitive. If a matching enum constant is found, it is returned. If no match is found, anIllegalArgumentExceptionis thrown with a detailed error message. Additionally, the method performs checks to ensure that neither the enum class nor the value is null or empty.- Parameters:
value- The string representation of the enum constant to search for. This comparison is case-insensitive.enumClass- TheClassobject of the enum type in which to search..- Returns:
- The enum constant that matches the given value if found, otherwise null.
- Throws:
IllegalArgumentException- If the class is null or not an enum class or the value is null or empty.
-
findConstantInNestedEnumeration
Recursively searches through an enumeration and its nested enums to find a constant matching the given name.- Parameters:
clazz- The class to start the search with.name- The name of the enum constant to find.- Returns:
- The enum constant if found, otherwise null.
-
getEnumerationFromString
Returns the enumeration constant matching the supplied string, ornullwhen the string is absent.- Type Parameters:
E- the enumeration type- Parameters:
value- the string representation of the enumeration constantclazz- the enumeration class to inspect- Returns:
- null if the string is null or empty and the corresponding enumeration otherwise ; an error IllegalArgumentException is raised if the string does not correspond to any enumeration
-