Class BasicRegistry<T>
- Type Parameters:
T- The type of values stored in the registry.
- Direct Known Subclasses:
IterableLabeledRegistry, LabeledRegistry
T with String keys.
The registry supports explicit key-value insertion and also provides
automatic key generation using NamesGenerator.
Duplicate keys are not allowed.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionBasicRegistry(String prefix) Constructs a BasicRegistry with a prefix used for autogenerated keys. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from the registry.booleancontainsKey(String key) Returnstrueif the given key exists in the registry.booleancontainsValue(T value) Returnstrueif the given value is present in the registry.entrySet()Returns a set view of the entries contained in this registry.protected StringgenerateKey(String key, T value) Generates the final key to use for storing the given value.Retrieves the unique value associated with the given key.protected StringPerforms the actual insertion of the key-value pair into the map.booleanisEmpty()Returnstrueif the registry contains no entries.keySet()Returns a set view of the keys contained in this registry.Inserts a value into the registry under the given key, or generates a key if none is provided.Convenience method that inserts the given value using an auto-generated key.voidputAll(BasicRegistry<? extends T> other) Adds all entries from anotherBasicRegistryinto this registry.voidAdds all entries from the given map to this registry, enforcing non-null keys/values and disallowing any duplicate keys.Removes the entry associated with the given key.protected Stringrollback(String key, T value, RuntimeException e) Performs rollback actions when an exception occurs during the validation phase.intsize()Returns the number of entries in the registry.protected voidvalidateParams(String key, T value) Validates that the key-value pair can be safely inserted into the map.values()Returns a collection view of the values contained in this registry.
-
Field Details
-
UNDEFINED
Special marker used to denote the absence of a user-defined key.This value must be consistent with
Labeled.NOLABELto ensure thatBasicRegistryandLabeledRegistryinterpret "undefined" in the same way. Divergence would break key-label consistency logic.- See Also:
-
-
Constructor Details
-
BasicRegistry
Constructs a BasicRegistry with a prefix used for autogenerated keys.- Parameters:
prefix- the prefix to use for generated keys- Throws:
NullPointerException- when the prefix is null
-
-
Method Details
-
size
public int size()Returns the number of entries in the registry.- Returns:
- the number of key-value pairs stored
-
isEmpty
public boolean isEmpty()Returnstrueif the registry contains no entries.- Returns:
trueif empty,falseotherwise
-
get
Retrieves the unique value associated with the given key.- Parameters:
key- the key to search for- Returns:
- the unique value associated with the key
- Throws:
NoSuchElementException- when no value has been found
-
containsKey
Returnstrueif the given key exists in the registry.- Parameters:
key- the key to check- Returns:
trueif the key exists,falseotherwise
-
put
public String put(String key, T value) throws IllegalStateException, NullPointerException, RegistryInsertionException Inserts a value into the registry under the given key, or generates a key if none is provided.If the specified key is
UNDEFINED, a unique key will be generated using aNamesGenerator. Otherwise, the provided key is used. In both cases, the method ensures the key is not already associated with a different value.Unlike
Map.put(Object, Object), this method returns the actual key used, not the value. This is intentional to allow retrieval of auto-generated keys.This method delegates to four other methods to fulfill its responsibilities:
generateKey(String, Object): Resolves the final key to usevalidateParams(String, Object): Ensures the key-value pair can be safely addedinsert(String, Object): Actually inserts the entry into the maprollback(String, Object, RuntimeException): Handles insertion failure (optional override)
- Parameters:
key- the desired key to associate with the value. IfUNDEFINED, a new key is auto-generated.value- the non-null value to insert into the registry- Returns:
- the key used to store the value (either provided or generated)
- Throws:
IllegalStateException- if:- the key already exists and is associated with a different value
- a unique key could not be generated within the allowed attempts
NullPointerException- if either key or value isnullRegistryInsertionException- if the insertion fails despite passing validation. This should never occur under normal conditions.
-
generateKey
Generates the final key to use for storing the given value.If the provided key is not equal to
UNDEFINED, it is returned unchanged. Otherwise, this method attempts to generate a unique key using theNamesGenerator, appending a timestamp and a configured prefix.The method ensures that the generated key does not collide with existing entries in the map. If a unique key cannot be found within
MAX_RETRYattempts, an exception is thrown.- Parameters:
key- the initial key, possiblyUNDEFINEDvalue- the value to be stored- Returns:
- a key guaranteed to be non-null and not already present in the map
- Throws:
IllegalStateException- if a unique key could not be generated within allowed attempts
-
validateParams
Validates that the key-value pair can be safely inserted into the map.By default, this method checks that if the key already exists, it must be associated with the same object instance (reference equality).
Subclasses can override this method to implement custom validation logic.
- Parameters:
key- the key to validatevalue- the value to validate- Throws:
IllegalStateException- if the key already exists and is mapped to a different value
-
insert
Performs the actual insertion of the key-value pair into the map.This method should not perform validation, but rather assume the key and value have been verified already. The default implementation inserts the pair into the map and returns the key.
Subclasses may override to perform custom insertion strategies.
- Parameters:
key- the key to insertvalue- the value to associate with the key- Returns:
- the key used for insertion
- Throws:
RegistryInsertionException- if the insertion fails unexpectedly
-
rollback
Performs rollback actions when an exception occurs during the validation phase.This method is intended to undo any side effects or preparatory steps taken during
validateParams(String, Object). For example, if validation reserves a resource or updates shared state, rollback should revert those changes.It is not called when
insert(String, Object)fails; in that case, theRegistryInsertionExceptionis propagated directly as insertion failures are considered unrecoverable and indicative of a deeper issue.The default implementation simply rethrows the exception with no rollback behavior. Subclasses can override this method to implement rollback semantics as needed.
- Parameters:
key- the key involved in the operationvalue- the value that was being validatede- the exception that occurred during validation- Returns:
- never returns normally; always rethrows an exception
- Throws:
RuntimeException- the same or a new exception to propagate
-
put
Convenience method that inserts the given value using an auto-generated key.This is a shorthand for calling
put(String, Object)withUNDEFINEDas the key, triggering automatic key generation.- Parameters:
value- the value to insert; must not benull- Returns:
- the generated key used for insertion
- Throws:
NullPointerException- if the value isnullIllegalStateException- if a unique key could not be generated or validation failsRegistryInsertionException- if insertion fails unexpectedly- See Also:
-
putAll
public void putAll(Map<? extends String, ? extends T> other) throws IllegalStateException, NullPointerException Adds all entries from the given map to this registry, enforcing non-null keys/values and disallowing any duplicate keys. This method performs a full validation before inserting anything, ensuring that: The input map is notnullAll keys and values in the map are non-null No key in the input map is already present in this registry If any of these checks fail, an appropriate exception is thrown and no changes are made to the registry.- Parameters:
other- a map of entries to add to the registry- Throws:
NullPointerException- if the map, any key, or any value isnullIllegalStateException- if any key already exists in this registry for a distinct value
-
putAll
public void putAll(BasicRegistry<? extends T> other) throws IllegalStateException, NullPointerException Adds all entries from anotherBasicRegistryinto this registry.This is a convenience method that delegates to
putAll(Map)while ensuring the same validations and rules apply.- Parameters:
other- anotherBasicRegistrycontaining entries to copy- Throws:
NullPointerException- if the registry or any of its keys/values isnullIllegalStateException- if any key inotheralready exists in this registry with a distinct value
-
remove
Removes the entry associated with the given key.- Parameters:
key- the key to remove- Returns:
- the removed value
- Throws:
NoSuchElementException- if the key is not present
-
clear
public void clear()Removes all entries from the registry. -
containsValue
Returnstrueif the given value is present in the registry.- Parameters:
value- the value to check- Returns:
trueif the value exists,falseotherwise
-
keySet
-
values
Returns a collection view of the values contained in this registry.- Returns:
- a collection of values
-
entrySet
-