public abstract class

DynamicEnum

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.util.base.DynamicEnum<E extends com.pnfsoftware.jeb.util.base.DynamicEnum<E>>
Known Direct Subclasses

Class Overview

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 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);
     }
 }
 

Summary

Fields
protected final int id
protected final String name
Protected Constructors
DynamicEnum(int id, String name)
Public Methods
boolean equals(Object obj)
int hashCode()
int id()
boolean isCompatibleWith(E other)
Determine whether two dyn.enums are compatible.
String name()
abstract int ordinal()
String toString()
Protected Methods
final static <E extends DynamicEnum<E>> boolean isBuiltin(Map<String, E> map, int builtinCount, E e)
final static <E extends DynamicEnum<E>> int ordinal(Map<String, E> map, E e)
synchronized final static <E extends DynamicEnum<E>> E register(Map<String, E> map, E e)
synchronized final static <E extends DynamicEnum<E>> boolean unregister(Map<String, E> map, int builtinCount, String name)
final static <E extends DynamicEnum<E>> E valueOf(Map<String, E> map, String name, E def)
final static <E extends DynamicEnum<E>> E valueOf(Map<String, E> map, int id, E def)
final static <E extends DynamicEnum<E>> Collection<E> values(Map<String, E> map)
final static <E extends DynamicEnum<E>> void verifyAvailability(Map<String, E> map, int id, String name)
[Expand]
Inherited Methods
From class java.lang.Object

Fields

protected final int id

protected final String name

Protected Constructors

protected DynamicEnum (int id, String name)

Public Methods

public boolean equals (Object obj)

public int hashCode ()

public int id ()

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

public String name ()

public abstract int ordinal ()

public String toString ()

Protected Methods

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

protected static final int ordinal (Map<String, E> map, E e)

protected static final synchronized E register (Map<String, E> map, E e)

protected static final synchronized boolean unregister (Map<String, E> map, int builtinCount, String name)

protected static final E valueOf (Map<String, E> map, String name, E def)

Parameters
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)

protected static final E valueOf (Map<String, E> map, int id, E def)

Parameters
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)

protected static final Collection<E> values (Map<String, E> map)

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