Class DynamicEnum<E extends DynamicEnum<E>>

java.lang.Object
com.pnfsoftware.jeb.util.base.DynamicEnum<E>
Direct Known Subclasses:
CallingConventionName, CompilerType, ProcessorFamily, ProcessorType, SubsystemType

@Ser public abstract class DynamicEnum<E extends DynamicEnum<E>> extends Object
Base class for dynamic enumerations. Dynamic enumerations are custom-built enumerations supporting the addition and removal of entries.

A dynamic enum must adhere to a defined contract. It is recommended to use the template below:

 @Ser
 static public class Something extends DynamicEnum {
     // --- start of static fields ---
     protected static LinkedHashMap<String, Something> map = new LinkedHashMap<>();
     // built-in constants must be created here (or in the a static initializer block)
     public static final Something UNKNOWN = register(0, "UNKNOWN");
     public static final Something BLAH = register(1, "BLAH");
     public static final Something FOOBAR = register(2, "FOOBAR");
     // count of built-in constants; those cannot be unregistered!
     public static final int builtinCount = map.size();
     // --- end of static fields ---

     // non-static fields are allowed, but naturally restricted to simple types (primitives, strings, etc.)
 
     // id and name are mandatory
     protected Something(int id, String name) {
         super(id, name);
     }
 
     @Override
     public int ordinal() {
         return ordinal(map, this);
     }
 
     public static int count() {
         return map.size();
     }
 
     public static Collection values() {
         return values(map);
     }
 
     public static Something valueOf(String name) {
         return valueOf(map, name, UNKNOWN);
     }
 
     public static Something valueOf(int id) {
         return valueOf(map, id, UNKNOWN);
     }
 
     public static Something register(int id, String name) {
         return register(map, new Something(id, name));
     }
 
     public static boolean unregister(String name) {
         return unregister(map, builtinCount, name);
     }
 }
 
  • Field Details

    • id

      protected final int id
    • name

      protected final String name
  • Constructor Details

    • DynamicEnum

      protected DynamicEnum(int id, String name)
  • Method Details

    • id

      public int id()
    • name

      public String name()
    • ordinal

      public abstract int ordinal()
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • ordinal

      protected static final <E extends DynamicEnum<E>> int ordinal(Map<String,E> map, E e)
    • valueOf

      protected static final <E extends DynamicEnum<E>> E valueOf(Map<String,E> map, int id, E def)
      Type Parameters:
      E -
      Parameters:
      map -
      id -
      def - optional; useful when deserializing unknown entries / entries that have not been re-registered
      Returns:
      an entry or the provided default entry (which may be null)
    • valueOf

      protected static final <E extends DynamicEnum<E>> E valueOf(Map<String,E> map, String name, E def)
      Type Parameters:
      E -
      Parameters:
      map -
      name -
      def - optional; useful when deserializing unknown entries / entries that have not been re-registered
      Returns:
      an entry or the provided default entry (which may be null)
    • values

      protected static final <E extends DynamicEnum<E>> Collection<E> values(Map<String,E> map)
    • verifyAvailability

      protected static final <E extends DynamicEnum<E>> void verifyAvailability(Map<String,E> map, int id, String name)
    • isBuiltin

      protected static final <E extends DynamicEnum<E>> boolean isBuiltin(Map<String,E> map, int builtinCount, E e)
    • register

      protected static final <E extends DynamicEnum<E>> E register(Map<String,E> map, E e)
    • unregister

      protected static final <E extends DynamicEnum<E>> boolean unregister(Map<String,E> map, int builtinCount, String name)
    • isCompatibleWith

      public boolean isCompatibleWith(E other)
      Determine whether two dyn.enums are compatible. This default implementation performs a name-based compatibility test, using _ as a hierarchical separator. An enum named SOME_THING is compatible with SOME_THING_ELSE, SOME, but not with SO, SOM, SOMETHING, SOME_FOO or SOME_THINGELSE.

      This method may be overridden.

      Parameters:
      other - another enum
      Returns:
      true if this enumerated constant is compatible with the provided enumerated constant