Class InstanceManager
The structural goal is to have the jmri package not depend on lower packages, with the implementations still available at run-time through the InstanceManager.
To retrieve the default object of a specific type, do
getDefault(java.lang.Class<T>) where the argument is e.g.
"SensorManager.class". In other words, you ask for the default object of a
particular type. Note that this call is intended to be used in the usual case
of requiring the object to function; it will log a message if there isn't
such an object. If that's routine, then use the
getNullableDefault(java.lang.Class<T>) method instead.
Multiple items can be held, and are retrieved as a list with
getList(java.lang.Class<T>).
If a specific item is needed, e.g. one that has been constructed via a
complex process during startup, it should be installed with
store(T, java.lang.Class<T>).
If it is desirable for the InstanceManager to create an object on first
request, have that object's class implement the
InstanceManagerAutoDefault flag interface. The InstanceManager will
then construct a default object via the no-argument constructor when one is
first requested.
For initialization of more complex default objects, see the
InstanceInitializer mechanism and its default implementation in
DefaultInstanceInitializer.
Implement the InstanceManagerAutoInitialize interface when default
objects need to be initialized after the default instance has been
constructed and registered with the InstanceManager. This will allow
references to the default instance during initialization to work as expected.
This file is part of JMRI.
JMRI is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. See the "COPYING" file for a copy of this license.
JMRI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidRegister for notification on changes to specific types.static voidaddPropertyChangeListener(String propertyName, PropertyChangeListener l) Register for notification on changes to specific types<T> voidClear all managed instances of a particular type from this InstanceManager.voidclearAll()Clear all managed instances from the common instance manager, effectively installing a new one.static <T> booleancontainsDefault(Class<T> type) Check if a default has been set for the given type.static StringDump generic content of InstanceManager by type.static <T> voidderegister(T item, Class<T> type) Remove an object of a particular type that had earlier been registered withstore(T, java.lang.Class<T>).static InstanceManagerGet the default instance of the InstanceManager.static <T> TgetDefault(Class<T> type) Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class).static ObjectgetDefault(String className) Retrieve the last object of specific type that was registered withstore(java.lang.Object, java.lang.Class).static StringgetDefaultsPropertyName(Class<?> clazz) Get the property name included in thePropertyChangeEventthrown when the default for a specific class is changed.<T> TgetInstance(Class<T> type) Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class).Get a list of stored types<T> List<T>getInstances(Class<T> type) Get a list of all registered objects of type T.<T> List<T>getInstances(String className) Get a list of all registered objects of a specific type.static <T> List<T>Retrieve a list of all objects of type T that were registered withstore(T, java.lang.Class<T>).Retrieve a list of all objects of a specific type that were registered withstore(T, java.lang.Class<T>).static StringgetListPropertyName(Class<?> clazz) Get the property name included in thePropertyChangeEventthrown when the list for a specific class is changed.static <T> TgetNullableDefault(Class<T> type) Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class).static ObjectgetNullableDefault(String className) Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class).static <T> Optional<T>getOptionalDefault(Class<T> type) Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class)wrapped in anOptional.static <T> booleanisInitialized(Class<T> type) Check if a particular type has been initialized without triggering an automatic initialization.static LightManagerMay eventually be deprecated, use @{link #getDefault} directly.static MemoryManagerMay eventually be deprecated, use @{link #getDefault} directly.<T> voidRemove an object of a particular type that had earlier been registered withstore(T, java.lang.Class<T>).static voidRemove notification on changes to specific types.static voidremovePropertyChangeListener(String propertyName, PropertyChangeListener l) Remove notification on changes to specific types.static <T> voidDeregister all objects of a particular type.static SensorManagerMay eventually be deprecated, use @{link #getDefault} directly.static voidstatic <T> TsetDefault(Class<T> type, T item) Set an object of type T as the default for that type.static voidstatic voidstatic voidstatic voidstatic voidstatic voidstatic voidstatic voidstatic <T> voidStore an object of a particular type for later retrieval viagetDefault(java.lang.Class<T>)orgetList(java.lang.Class<T>).static <T> voidstoreUnchecked(Object item, Class<T> type) Store an object of a particular type for later retrieval viagetDefault(java.lang.Class<T>)orgetList(java.lang.Class<T>).static ThrottleManagerMay eventually be deprecated, use @{link #getDefault} directly.static TurnoutManagerMay eventually be deprecated, use @{link #getDefault} directly.
-
Constructor Details
-
InstanceManager
public InstanceManager()Default constructor for the InstanceManager.
-
-
Method Details
-
store
Store an object of a particular type for later retrieval viagetDefault(java.lang.Class<T>)orgetList(java.lang.Class<T>).- Type Parameters:
T- The type of the class- Parameters:
item- The object of type T to be storedtype- The class Object for the item's type. This will be used as the key to retrieve the object later.
-
storeUnchecked
Store an object of a particular type for later retrieval viagetDefault(java.lang.Class<T>)orgetList(java.lang.Class<T>).store(T, java.lang.Class<T>)is preferred to this method because it does type checking at compile time. In (rare) cases that's not possible, and run-time checking is required.- Type Parameters:
T- The type of the class- Parameters:
item- The object of type T to be storedtype- The class Object for the item's type. This will be used as the key to retrieve the object later.
-
getList
Retrieve a list of all objects of type T that were registered withstore(T, java.lang.Class<T>).- Type Parameters:
T- The type of the class- Parameters:
type- The class Object for the items' type.- Returns:
- A list of type Objects registered with the manager or an empty list.
-
getList
Retrieve a list of all objects of a specific type that were registered withstore(T, java.lang.Class<T>). Intended for use with i.e. scripts where access to the class type is inconvenient. In Java code where typing is enforced, usegetList(Class).- Parameters:
className- Fully qualified class name- Returns:
- A list of type Objects registered with the manager or an empty list.
- Throws:
IllegalArgumentException- if the named class doesn't exist
-
reset
Deregister all objects of a particular type.- Type Parameters:
T- The type of the class- Parameters:
type- The class Object for the items to be removed.
-
deregister
Remove an object of a particular type that had earlier been registered withstore(T, java.lang.Class<T>). If item was previously registered, this will remove item and fire an indexed property change event for the property matching the output ofgetListPropertyName(java.lang.Class)for type.This is the static access to
remove(java.lang.Object, java.lang.Class).- Type Parameters:
T- The type of the class- Parameters:
item- The object of type T to be deregisteredtype- The class Object for the item's type
-
remove
Remove an object of a particular type that had earlier been registered withstore(T, java.lang.Class<T>). If item was previously registered, this will remove item and fire an indexed property change event for the property matching the output ofgetListPropertyName(java.lang.Class)for type.- Type Parameters:
T- The type of the class- Parameters:
item- The object of type T to be deregisteredtype- The class Object for the item's type
-
getDefault
Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class).Unless specifically set, the default is the last object stored, see the
setDefault(java.lang.Class, java.lang.Object)method.In some cases, InstanceManager can create the object the first time it's requested. For more on that, see the class comment.
In most cases, system configuration assures the existence of a default object, so this method will log and throw an exception if one doesn't exist. Use
getNullableDefault(java.lang.Class)orgetOptionalDefault(java.lang.Class)if the default is not guaranteed to exist.- Type Parameters:
T- The type of the class- Parameters:
type- The class Object for the item's type- Returns:
- The default object for type
- Throws:
NullPointerException- if no default object for type exists- See Also:
-
getDefault
Retrieve the last object of specific type that was registered withstore(java.lang.Object, java.lang.Class). Intended for use with i.e. scripts where access to the class type is inconvenient. In Java code where typing is enforced, usegetDefault(Class).Unless specifically set, the default is the last object stored, see the
setDefault(java.lang.Class, java.lang.Object)method.In some cases, InstanceManager can create the object the first time it's requested. For more on that, see the class comment.
In most cases, system configuration assures the existence of a default object, so this method will log and throw an exception if one doesn't exist. Use
getNullableDefault(java.lang.Class)orgetOptionalDefault(java.lang.Class)if the default is not guaranteed to exist.- Parameters:
className- Fully qualified class name- Returns:
- The default object for type
- Throws:
NullPointerException- if no default object for type existsIllegalArgumentException- if the named class doesn't exist- See Also:
-
getNullableDefault
Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class).Unless specifically set, the default is the last object stored, see the
setDefault(java.lang.Class, java.lang.Object)method.In some cases, InstanceManager can create the object the first time it's requested. For more on that, see the class comment.
In most cases, system configuration assures the existence of a default object, but this method also handles the case where one doesn't exist. Use
getDefault(java.lang.Class)when the object is guaranteed to exist.- Type Parameters:
T- The type of the class- Parameters:
type- The class Object for the item's type.- Returns:
- The default object for type.
- See Also:
-
getNullableDefault
Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class). Intended for use with i.e. scripts where access to the class type is inconvenient. In Java code where typing is enforced, usegetNullableDefault(Class).Unless specifically set, the default is the last object stored, see the
setDefault(java.lang.Class, java.lang.Object)method.In some cases, InstanceManager can create the object the first time it's requested. For more on that, see the class comment.
In most cases, system configuration assures the existence of a default object, but this method also handles the case where one doesn't exist. Use
getDefault(java.lang.Class)when the object is guaranteed to exist.- Parameters:
className- Fully qualified class name- Returns:
- The default object for type.
- Throws:
IllegalArgumentException- if the named class doesn't exist- See Also:
-
getInstance
Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class).Unless specifically set, the default is the last object stored, see the
setDefault(java.lang.Class, java.lang.Object)method.In some cases, InstanceManager can create the object the first time it's requested. For more on that, see the class comment.
In most cases, system configuration assures the existence of a default object, but this method also handles the case where one doesn't exist. Use
getDefault(java.lang.Class)when the object is guaranteed to exist.- Type Parameters:
T- The type of the class- Parameters:
type- The class Object for the item's type.- Returns:
- The default object for type.
- See Also:
-
getOptionalDefault
Retrieve the last object of type T that was registered withstore(java.lang.Object, java.lang.Class)wrapped in anOptional.Unless specifically set, the default is the last object stored, see the
setDefault(java.lang.Class, java.lang.Object)method.In some cases, InstanceManager can create the object the first time it's requested. For more on that, see the class comment.
In most cases, system configuration assures the existence of a default object, but this method also handles the case where one doesn't exist. Use
getDefault(java.lang.Class)when the object is guaranteed to exist.- Type Parameters:
T- the type of the default class- Parameters:
type- the class Object for the default type- Returns:
- the default wrapped in an Optional or an empty Optional if the default is null
- See Also:
-
setDefault
Set an object of type T as the default for that type.Also registers (stores) the object if not already present.
Now, we do that moving the item to the back of the list; see the
getDefault(java.lang.Class<T>)method- Type Parameters:
T- The type of the class- Parameters:
type- The Class object for valitem- The object to make default for type- Returns:
- The default for type (normally this is the item passed in)
-
containsDefault
Check if a default has been set for the given type.As a side-effect, then (a) ensures that the list for the given type exists, though it may be empty, and (b) if it had to create the list, a PropertyChangeEvent is fired to denote that.
- Type Parameters:
T- The type of the class- Parameters:
type- The class type- Returns:
- true if an item is available as a default for the given type; false otherwise
-
isInitialized
Check if a particular type has been initialized without triggering an automatic initialization. The existence or non-existence of the corresponding list is not changed, and no PropertyChangeEvent is fired.- Type Parameters:
T- The type of the class- Parameters:
type- The class type- Returns:
- true if an item is available as a default for the given type; false otherwise
-
contentsToString
Dump generic content of InstanceManager by type.- Returns:
- A formatted multiline list of managed objects
-
getInstanceClasses
Get a list of stored types- Returns:
- A unmodifiable list of the currently stored types
-
removePropertyChangeListener
Remove notification on changes to specific types.- Parameters:
l- The listener to remove
-
removePropertyChangeListener
Remove notification on changes to specific types.- Parameters:
propertyName- the property being listened forl- The listener to remove
-
addPropertyChangeListener
Register for notification on changes to specific types.- Parameters:
l- The listener to add
-
addPropertyChangeListener
Register for notification on changes to specific types- Parameters:
propertyName- the property being listened forl- The listener to add
-
getDefaultsPropertyName
Get the property name included in thePropertyChangeEventthrown when the default for a specific class is changed.- Parameters:
clazz- the class being listened for- Returns:
- the property name
-
getListPropertyName
Get the property name included in thePropertyChangeEventthrown when the list for a specific class is changed.- Parameters:
clazz- the class being listened for- Returns:
- the property name
-
lightManagerInstance
May eventually be deprecated, use @{link #getDefault} directly.- Returns:
- the default light manager. May not be the only instance.
-
memoryManagerInstance
May eventually be deprecated, use @{link #getDefault} directly.- Returns:
- the default memory manager. May not be the only instance.
-
sensorManagerInstance
May eventually be deprecated, use @{link #getDefault} directly.- Returns:
- the default sensor manager. May not be the only instance.
-
turnoutManagerInstance
May eventually be deprecated, use @{link #getDefault} directly.- Returns:
- the default turnout manager. May not be the only instance.
-
throttleManagerInstance
May eventually be deprecated, use @{link #getDefault} directly.- Returns:
- the default throttle manager. May not be the only instance.
-
setTurnoutManager
-
setThrottleManager
-
setLightManager
-
setReporterManager
-
setSensorManager
-
setIdTagManager
-
setMeterManager
-
setAnalogIOManager
-
setStringIOManager
-
getInstances
Get a list of all registered objects of type T.- Type Parameters:
T- type of the class- Parameters:
type- class Object for type T- Returns:
- a list of registered T instances with the manager or an empty list
-
getInstances
Get a list of all registered objects of a specific type. Intended for use with i.e. scripts where access to the class type is inconvenient.- Type Parameters:
T- type of the class- Parameters:
className- Fully qualified class name- Returns:
- a list of registered instances with the manager or an empty list
- Throws:
IllegalArgumentException- if the named class doesn't exist
-
clearAll
Clear all managed instances from the common instance manager, effectively installing a new one. -
clear
Clear all managed instances of a particular type from this InstanceManager.- Type Parameters:
T- the type of class to clear- Parameters:
type- the type to clear
-
getDefault
Get the default instance of the InstanceManager. This is used for verifying the source of events fired by the InstanceManager.- Returns:
- the default instance of the InstanceManager, creating it if needed
-