E
- the type of enum instances this class will manage. It must extend CoreDynamicEnum
.
Example usage:
EnumCache<YesNo> yesNoEnumCache = EnumCache.of(YesNo.class);
YesNo maybe = yesNoEnumCache.valueOf("Maybe"); // Dynamically creates a new enum instance with name "Maybe"
public class DynamicEnumClass<E extends CoreDynamicEnum<E>> extends EnumCache<E>
EnumCache
.
The class is capable of dynamically creating and managing instances which resemble enumerations
(enums) in behavior. Unlike traditional enums, which have a fixed set of instances,
DynamicEnumClass
can create new instances on-the-fly.
This class is thread-safe and ensures that each unique name maps to a single instance.
Instances of DynamicEnumClass
have properties similar to traditional enums,
like name
and ordinal
. The name
is the string identifier of an enum instance,
while the ordinal
represents the position of the enum instance in the declaration order.
Modifier and Type | Method and Description |
---|---|
E[] |
asArray()
Returns an array containing all the enum instances managed by this cache.
|
<T> Map<E,T> |
createMap()
Creates a map where the keys are enum instances.
|
Set<E> |
createSet()
Creates a set for holding enum instances.
|
E |
forIndex(int index)
Returns an array containing the enum instances managed by this class in
the order they were created.
|
E |
get(String name)
Returns the enum instance with the specified name.
|
void |
reset()
Resets the internal state of this class by clearing the stored enum instances
and resetting them to the initial state.
|
int |
size()
Returns the number of enum instances currently managed by this class.
|
E |
valueOf(String name)
Returns the enum instance with the specified name.
|
public E get(String name)
null
if the instance does not exist.get
in class EnumCache<E extends CoreDynamicEnum<E>>
name
- the name of the enum instance to be retrieved.null
if not present.public E valueOf(String name)
valueOf
in class EnumCache<E extends CoreDynamicEnum<E>>
name
- the name of the enum instance to be retrieved or created.public int size()
size
in class EnumCache<E extends CoreDynamicEnum<E>>
public E forIndex(int index)
forIndex
in class EnumCache<E extends CoreDynamicEnum<E>>
index
- the ordinal index of the enum instance to retrieve.public E[] asArray()
EnumCache
asArray
in class EnumCache<E extends CoreDynamicEnum<E>>
public <T> Map<E,T> createMap()
EnumCache
createMap
in class EnumCache<E extends CoreDynamicEnum<E>>
T
- the type of the map values.public Set<E> createSet()
EnumCache
createSet
in class EnumCache<E extends CoreDynamicEnum<E>>
@TestOnly public void reset()
This method is intended to be used for testing purposes.
Copyright © 2023. All rights reserved.