Class EnumUtil

java.lang.Object
org.conical.common.bbl.util.EnumUtil

public class EnumUtil extends Object
Wrapper for static utility methods related to Java enums.
Author:
rdoherty
  • Constructor Details

    • EnumUtil

      public EnumUtil()
  • Method Details

    • getNullSafeName

      public static <T extends Enum<?>> String getNullSafeName(T enumValue)
      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

      public static <T extends Enum<?>> T getNullSafeEnum(Class<T> clazz, String value)
      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 returned
      value - value to be converted
      Returns:
      enum value
      Throws:
      IllegalArgumentException - if value is non-empty, but can still not be converted
    • getValueByCode

      public static <T extends Coded> T getValueByCode(Class<T> clazz, int codedValue)
      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 enum
      codedValue - code
      Returns:
      enum value
      Throws:
      NoSuchElementException - if no value of the given class is coded to the given value
    • getGenericDescription

      public static String getGenericDescription(String name)
      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

      public static <T extends Enum<?>> T getDefaultIfNullOrBad(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. 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 value
      value - 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