public abstract class AbstractInstanceManager<TYPE_COMMON_INST,TYPE_ACTUAL_INST,TYPE_SELECTOR> extends Object
We distinguish two types of instances :
- Single = only 1 instance of it is ever active at the same time)
- Multiple/Prototype = any number of instances active at the same time
In case all is well, you only use Single and getting the instance is as easy as providing the instance that it is a children of.
In case you have multiple instances for the same parent type, we cannot keep doing that. This is why we need to make
sure we can store them in an easy way that can allow distinction between those when needed afterwards. See the registerMultiple(Object, Object, Object) method for that.
| Constructor and Description |
|---|
AbstractInstanceManager() |
| Modifier and Type | Method and Description |
|---|---|
List<TYPE_ACTUAL_INST> |
getAll(TYPE_COMMON_INST parent) |
io.vavr.control.Option<TYPE_ACTUAL_INST> |
getMultiple(TYPE_COMMON_INST parent,
TYPE_SELECTOR selector)
Tries to find the instance you look for.
|
List<TYPE_ACTUAL_INST> |
getMultiples(TYPE_COMMON_INST parent) |
io.vavr.control.Option<TYPE_ACTUAL_INST> |
getSingle(TYPE_COMMON_INST parent) |
Map.Entry<TYPE_SELECTOR,TYPE_ACTUAL_INST> |
registerMultiple(TYPE_COMMON_INST parent,
TYPE_SELECTOR selector,
TYPE_ACTUAL_INST instance)
This method stores your instances in a
ConcurrentHashMap that looks like this :|-- CommonInst1 -- | |-- Selector1 -> Instance 1 of class TYPE_ACTUAL_INST| |-- Selector2 -> Instance 2 of class TYPE_ACTUAL_INST| |-- CommonInst2 -- | |-- Selector# -> Instance # of class TYPE_ACTUAL_INST| ... | |-- SelectorN -> Instance N of class TYPE_ACTUAL_INST... | |
TYPE_ACTUAL_INST |
registerSingle(TYPE_COMMON_INST parent,
TYPE_ACTUAL_INST instance)
Registers a single instance of type
TYPE_ACTUAL_INST under the TYPE_COMMON_INST category. |
public TYPE_ACTUAL_INST registerSingle(TYPE_COMMON_INST parent, TYPE_ACTUAL_INST instance)
TYPE_ACTUAL_INST under the TYPE_COMMON_INST category.parent - The parent of this instanceinstance - The instance to registerpublic Map.Entry<TYPE_SELECTOR,TYPE_ACTUAL_INST> registerMultiple(TYPE_COMMON_INST parent, TYPE_SELECTOR selector, TYPE_ACTUAL_INST instance)
ConcurrentHashMap that looks like this :TYPE_ACTUAL_INSTTYPE_ACTUAL_INSTTYPE_ACTUAL_INSTTYPE_ACTUAL_INSTThe point is that "selectors" are anything you want them to be, whether it be the hashcode of the instance (but you won't be able to access it easily later), or some kind of related Node. Whatever fits your model in the best way.
Be wary of collisions (i.e. Instance 1 and 2 having the same selector) as it replaces instances in case of collision.
You are responsible for providing correct and non-colliding selectors.
parent - An instance, typically an enum memberselector - The selector that you have to provide to recover this particular instance laterinstance - The instance to save.RuntimeException - in case there was an error in saving the instance.public io.vavr.control.Option<TYPE_ACTUAL_INST> getMultiple(TYPE_COMMON_INST parent, TYPE_SELECTOR selector)
Option (similar to Optional) that either
contains Option.Some (found) or Option.None (not found/exception was thrown).
Look at Option for information on how to use it.
parent - The instance who's children you look for.selector - The selector previously used in #registerMultiple(TYPE_COMMON_INST, TYPE_SELECTOR,
TYPE_ACTUAL_INST).Option that either contains it (Option.Some) or is empty. That is, Option.None, which means that it was not found or at some point in the hierarchy there has been an exception).public List<TYPE_ACTUAL_INST> getAll(TYPE_COMMON_INST parent)
parent - The parent instance of all the nodes soughtpublic List<TYPE_ACTUAL_INST> getMultiples(TYPE_COMMON_INST parent)
parent - The parent instance of all the nodes soughtpublic io.vavr.control.Option<TYPE_ACTUAL_INST> getSingle(TYPE_COMMON_INST parent)
parent - The parent instance of the node soughtOption.Some filled with the unique node of single-type under this instance, or Option.None.Copyright © 2018. All rights reserved.