Class DynamicEnum<E extends DynamicEnum<E>>

java.lang.Object
io.github.venkateshamurthy.enums.DynamicEnum<E>
Type Parameters:
E - a type of self-referential generic.
Direct Known Subclasses:
Faults

public abstract class DynamicEnum<E extends DynamicEnum<E>> extends Object
DynamicEnum is a convenience over static Enum to add dynamically more instances. Please keep the child class final and keep the child class constructor as private with @Builder from lombok on private constructor.
  • Field Details

    • instances

      protected static final Map<Class<?>,Map<String,DynamicEnum<?>>> instances
      Only child classes can access this to make sure avoiding duplicate instances.
    • mappers

      protected static final Map<Class<?>,com.fasterxml.jackson.databind.ObjectMapper> mappers
      A map of dynamic enum type to its ObjectMapper.
  • Constructor Details

    • DynamicEnum

      public DynamicEnum(@NonNull @NonNull String name)
      Constructor
      Parameters:
      name - should be unique for a given child class type.
  • Method Details

    • allOf

      public static <T extends DynamicEnum<T>> Collection<? super T> allOf(@NonNull @NonNull Class<T> clazz)
      allOf DynamicEnum instances for a given class
      Type Parameters:
      T - type of the dynamic enum
      Parameters:
      clazz - is the dynamic enum class
      Returns:
      collection of dynamic enum instances
    • values

      public static <T extends DynamicEnum<T>> T[] values(@NonNull @NonNull Class<T> clazz, @NonNull @NonNull IntFunction<T[]> newArrayMaker)
      values provides DynamicEnum instances array for a given type
      Type Parameters:
      T - type of the dynamic enum
      Parameters:
      clazz - is the dynamic enum class
      newArrayMaker - a function to create an array
      Returns:
      array of the type
    • valueOf

      public static <T extends DynamicEnum<T>> T valueOf(@NonNull @NonNull Class<T> clazz, @NonNull @NonNull String name)
      valueOf provides the dynamic enum corresponding to the name.
      Type Parameters:
      T - is basically the <T extends DynamicEnum<T>>
      Parameters:
      clazz - is the dynamic enum class
      name - of the dynamic enum for which the corresponding subcclass of DynamicEnum is to fetched
      Returns:
      T
      Throws:
      RuntimeException - when name does not match what is cached with in
    • getDefaultMapper

      public static <T extends DynamicEnum<T>> com.fasterxml.jackson.databind.ObjectMapper getDefaultMapper(Class<T> clazz)
      Gets the ObjectMapper
      Type Parameters:
      T - type of <T extends DynamicEnum<T>>
      Parameters:
      clazz - of the dynamic enum for which the mapper is configured
      Returns:
      ObjectMapper
    • valueOf

      public static <T extends DynamicEnum<T>> T valueOf(@NonNull @NonNull Class<T> clazz, @NonNull @NonNull String name, boolean createIfAbsent, Supplier<T> supplier)
      returns an existing DynamicEnum if not create one using the given supplier.
      Type Parameters:
      T - a type of <T extends DynamicEnum<T>
      Parameters:
      clazz - DynamicEnum child
      name - of the dynamic enum
      createIfAbsent - flag whether to create o new one if absent
      supplier - to make use when to create a new one
      Returns:
      instance of T
    • name

      public String name()
      Gets the name
      Returns:
      name
    • ordinal

      public int ordinal()
      Gets the ordinal which might be useful to store instance in an array.
      Returns:
      ordinal.