Class KiwiEnums

java.lang.Object
org.kiwiproject.base.KiwiEnums

public final class KiwiEnums extends Object
Static utilities for working with Enum.
  • 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 use getIfPresentIgnoreCase(Class, String).
      Type Parameters:
      E - the enum type
      Parameters:
      enumClass - the enum class
      value - the string value to compare against, may be blank
      Returns:
      an Optional that may contain an Enum constant 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 class
      value - the string value to compare against, may be blank
      Returns:
      an Optional that may contain an Enum constant 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

      public static <E extends Enum<E>> boolean equals(Enum<E> enumValue, @Nullable CharSequence value)
      Compares the given enum's name with the given value for equality.
      Type Parameters:
      E - the enum type
      Parameters:
      enumValue - the enum to use for the comparison
      value - 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's name with the given value, ignoring case, for equality.
      Type Parameters:
      E - the enum type
      Parameters:
      enumValue - the enum to use for the comparison
      value - 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's name with 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 comparison
      value - 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's name with 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 comparison
      value - 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 the name of the given enums.
      Type Parameters:
      E - the enum type
      Parameters:
      value - the value to use for the comparison, may be null
      enumValues - 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 the name of the given enums, ignoring case.
      Type Parameters:
      E - the enum type
      Parameters:
      value - the value to use for the comparison, may be null
      enumValues - the enums to use for the comparison
      Returns:
      true if the value equals the name of the enums in case-insensitive manner, false otherwise