Package com.pnfsoftware.jeb.util.base
Class DynamicEnum<E extends DynamicEnum<E>>
java.lang.Object
com.pnfsoftware.jeb.util.base.DynamicEnum<E>
- Direct Known Subclasses:
CallingConventionName,CompilerType,ProcessorFamily,ProcessorType,SubsystemType
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleaninthashCode()intid()protected static final <E extends DynamicEnum<E>>
booleanbooleanisCompatibleWith(E other) Determine whether two dyn.enums are compatible.name()abstract intordinal()protected static final <E extends DynamicEnum<E>>
intprotected static final <E extends DynamicEnum<E>>
EtoString()protected static final <E extends DynamicEnum<E>>
booleanunregister(Map<String, E> map, int builtinCount, String name) protected static final <E extends DynamicEnum<E>>
Eprotected static final <E extends DynamicEnum<E>>
Eprotected static final <E extends DynamicEnum<E>>
Collection<E>protected static final <E extends DynamicEnum<E>>
voidverifyAvailability(Map<String, E> map, int id, String name)
-
Field Details
-
id
protected final int id -
name
-
-
Constructor Details
-
DynamicEnum
-
-
Method Details
-
id
public int id() -
name
-
ordinal
public abstract int ordinal() -
hashCode
public int hashCode() -
equals
-
toString
-
ordinal
-
valueOf
- 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
- 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
-
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
-
unregister
protected static final <E extends DynamicEnum<E>> boolean unregister(Map<String, E> map, int builtinCount, String name) -
isCompatibleWith
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
-