Package org.kiwiproject.base
Class KiwiEnums
java.lang.Object
org.kiwiproject.base.KiwiEnums
Static utilities for working with
Enum.-
Method Summary
Modifier and TypeMethodDescriptionstatic <E extends Enum<E>>
booleanequals(Enum<E> enumValue, @Nullable CharSequence value) Compares the given enum'snamewith the given value for equality.static <E extends Enum<E>>
booleanequalsAny(@Nullable CharSequence value, Enum<E>... enumValues) Checks whether the given value matches thenameof the given enums.static <E extends Enum<E>>
booleanequalsAnyIgnoreCase(@Nullable CharSequence value, Enum<E>... enumValues) Checks whether the given value matches thenameof the given enums, ignoring the case.static <E extends Enum<E>>
booleanequalsIgnoreCase(Enum<E> enumValue, @Nullable CharSequence value) Compares the given enum'snamewith the given value, ignoring the case, for equality.getIfPresent(Class<E> enumClass, @Nullable String value) Return an optional enum constant for the given enum type and value.getIfPresentIgnoreCase(Class<E> enumClass, @Nullable String value) Return an optional enum constant for the given enum type and value, compared against the enum constants in a case-insensitive manner, and ignoring any leading or trailing whitespace.Returns a list of the constants in the specified enum type.lowercaseName(Enum<E> enumValue) Return the lowercase name of the enum value, usingLocale.ENGLISHas the locale for the conversion.lowercaseName(Enum<E> enumValue, Locale locale) Return the lowercase name of the enum value.static <E extends Enum<E>>
booleannotEquals(Enum<E> enumValue, @Nullable CharSequence value) Compares the given enum'snamewith the given value for inverse equality, i.e., they are not equal.static <E extends Enum<E>>
booleannotEqualsIgnoreCase(Enum<E> enumValue, @Nullable CharSequence value) Compares the given enum'snamewith the given value, ignoring the case, for inverse equality, i.e., they are not equal.Returns a stream of constants in the specified enum type.
-
Method Details
-
listOf
Returns a list of the constants in the specified enum type.- Type Parameters:
E- the type of the enum- Parameters:
enumClass- the enum class; must not be null- Returns:
- a list containing all the constants of the specified enum type
- Throws:
IllegalArgumentException- if the provided class is null or not an enum type
-
streamOf
Returns a stream of constants in the specified enum type.- Type Parameters:
E- the type of the enum- Parameters:
enumClass- the enum class; must not be null- Returns:
- a stream containing all the constants of the specified enum type
- Throws:
IllegalArgumentException- if the provided class is null or not an enum type
-
getIfPresent
public static <E extends Enum<E>> Optional<E> getIfPresent(Class<E> enumClass, @Nullable String value) Return an optional enum constant for the given enum type and value. This performs an exact match of the enum constant names. For a case-insensitive comparison, you can usegetIfPresentIgnoreCase(Class, String).- Type Parameters:
E- the enum type- Parameters:
enumClass- the enum classvalue- the string value to compare against; may be blank- Returns:
- an Optional that may contain an
Enumconstant or be empty - Implementation Note:
- This wraps Guava's
Enums.getIfPresent(Class, String)and adapts the Guava Optional return type to a Java Optional. Unlike Guava, it permits blank values, in which case an empty Optional is returned.
-
getIfPresentIgnoreCase
public static <E extends Enum<E>> Optional<E> getIfPresentIgnoreCase(Class<E> enumClass, @Nullable String value) Return an optional enum constant for the given enum type and value, compared against the enum constants in a case-insensitive manner, and ignoring any leading or trailing whitespace.- Type Parameters:
E- the enum type- Parameters:
enumClass- the enum classvalue- the string value to compare against; may be blank- Returns:
- an Optional that may contain an
Enumconstant or be empty - Implementation Note:
- This wraps Apache Commons'
EnumUtils.getEnumIgnoreCase(Class, String)but returns am Optional instead of null if no enum constant is found. In addition, it ignores leading and trailing whitespace.
-
equals
Compares the given enum'snamewith the given value for equality.- Type Parameters:
E- the enum type- Parameters:
enumValue- the enum to use for the comparisonvalue- the value to use for the comparison; may be null- Returns:
- true if the enum name equals the value, false otherwise
-
equalsIgnoreCase
public static <E extends Enum<E>> boolean equalsIgnoreCase(Enum<E> enumValue, @Nullable CharSequence value) Compares the given enum'snamewith the given value, ignoring the case, for equality.- Type Parameters:
E- the enum type- Parameters:
enumValue- the enum to use for the comparisonvalue- the value to use for the comparison; may be null- Returns:
- if the enum name equals the value in a case-insensitive manner, false otherwise
-
notEquals
public static <E extends Enum<E>> boolean notEquals(Enum<E> enumValue, @Nullable CharSequence value) Compares the given enum'snamewith the given value for inverse equality, i.e., they are not equal.- Type Parameters:
E- the enum type- Parameters:
enumValue- the enum to use for the comparisonvalue- the value to use for the comparison; may be null- Returns:
- true if the enum name does not equal the value, false otherwise
-
notEqualsIgnoreCase
public static <E extends Enum<E>> boolean notEqualsIgnoreCase(Enum<E> enumValue, @Nullable CharSequence value) Compares the given enum'snamewith the given value, ignoring the case, for inverse equality, i.e., they are not equal.- Type Parameters:
E- the enum type- Parameters:
enumValue- the enum to use for the comparisonvalue- the value to use for the comparison; may be null- Returns:
- true if the enum name does not equal the value in a case-insensitive manner, false otherwise
-
equalsAny
@SafeVarargs public static <E extends Enum<E>> boolean equalsAny(@Nullable CharSequence value, Enum<E>... enumValues) Checks whether the given value matches thenameof the given enums.- Type Parameters:
E- the enum type- Parameters:
value- the value to use for the comparison; may be nullenumValues- the enums to use for the comparison- Returns:
- true if the value equals the name of the enums, false otherwise
-
equalsAnyIgnoreCase
@SafeVarargs public static <E extends Enum<E>> boolean equalsAnyIgnoreCase(@Nullable CharSequence value, Enum<E>... enumValues) Checks whether the given value matches thenameof the given enums, ignoring the case.- Type Parameters:
E- the enum type- Parameters:
value- the value to use for the comparison; may be nullenumValues- the enums to use for the comparison- Returns:
- true if the value equals the name of the enums in a case-insensitive manner, false otherwise
-
lowercaseName
Return the lowercase name of the enum value, usingLocale.ENGLISHas the locale for the conversion.- Type Parameters:
E- the enum type- Parameters:
enumValue- the enum value to lowercase- Returns:
- the lowercase value of the enum value
- See Also:
-
lowercaseName
Return the lowercase name of the enum value.- Type Parameters:
E- the enum type- Parameters:
enumValue- the enum value to lowercaselocale- the Locale to use for the lowercase conversion- Returns:
- the lowercase value of the enum value
- Throws:
IllegalArgumentException- if enumValue or locale is null
-