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 case.static <E extends Enum<E>>
booleanequalsIgnoreCase(Enum<E> enumValue, @Nullable CharSequence value) Compares the given enum'snamewith the given value, ignoring 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.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 case, for inverse equality, i.e. they are not equal.
-
Method Details
-
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 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 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 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 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 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 case-insensitive manner, false otherwise
-