Class AbstractRegistry<E extends Identifiable<K>,K,P extends Provider<E>>
- Type Parameters:
E
- type of the elementK
- type of the keyP
- type of the provider
- All Implemented Interfaces:
ProviderChangeListener<E>
,Registry<E,
K>
- Direct Known Subclasses:
AbstractLinkRegistry
AbstractRegistry
is an abstract implementation of the Registry
interface, that can be used as
base class for Registry
implementations.- Author:
- Dennis Nobel - Initial contribution, Stefan Bußweiler - Migration to new event mechanism, Victor Toni - provide elements as
Stream
, Kai Kreuzer - switched to parameterized logging, Hilbrand Bouwkamp - Made protected fields private and added new methods to give access., Markus Rathgeb - Use separate collections to improve performance, Chris Jackson - Ensure managed provider is only unset by current provider
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractRegistry
(@Nullable Class<@NonNull P> providerClazz) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
activate
(org.osgi.framework.BundleContext context) Adds the given element to the accordingManagedProvider
.void
Notifies the listener that a single element has been added.protected void
addProvider
(Provider<@NonNull E> provider) void
addRegistryChangeListener
(RegistryChangeListener<@NonNull E> listener) Adds aRegistryChangeListener
to the registry.protected void
beforeUpdateElement
(@NonNull E existingElement) This method is called before an element is updated.protected void
protected void
forEach
(BiConsumer<Provider<@NonNull E>, @NonNull E> consumer) This method traverses over all elements in the registry and calls the consumer with the provider of the element as the first parameter and the element as the second argument.protected void
This method traverses over all elements in the registry and calls the consumer with each element.protected void
This method traverses over all elements of a provider in the registry and calls the consumer with each element.@Nullable E
This method retrieves a single element from the registry.getAll()
Returns a collection of all elements in the registry.protected @Nullable EventPublisher
protected Optional<ManagedProvider<E,
K>> getProvider
(@NonNull E element) This method retrieves the provider of an element from the registry.getProvider
(@NonNull K key) This method retrieves the provider of an element from the registry.getValueAndProvider
(@NonNull K key) This method retrieves an Entry with the provider and the element for the key from the registry.protected void
notifyListeners
(@NonNull E oldElement, @NonNull E element, org.openhab.core.common.registry.AbstractRegistry.EventType eventType) protected void
notifyListeners
(@NonNull E element, org.openhab.core.common.registry.AbstractRegistry.EventType eventType) protected void
notifyListenersAboutAddedElement
(@NonNull E element) protected void
notifyListenersAboutRemovedElement
(@NonNull E element) protected void
notifyListenersAboutUpdatedElement
(@NonNull E oldElement, @NonNull E element) protected void
onAddElement
(@NonNull E element) This method is called before an element is added.protected void
onRemoveElement
(@NonNull E element) This method is called before an element is removed.protected void
onUpdateElement
(@NonNull E oldElement, @NonNull E element) This method is called before an element is updated.protected void
This method can be used in a subclass in order to post events through the openHAB events bus.@Nullable E
Removes the given element from the accordingManagedProvider
.void
Notifies the listener that a single element has been removed.protected void
removeProvider
(Provider<@NonNull E> provider) void
removeRegistryChangeListener
(RegistryChangeListener<@NonNull E> listener) Removes aRegistryChangeListener
from the registry.protected void
setEventPublisher
(EventPublisher eventPublisher) protected void
setManagedProvider
(ManagedProvider<@NonNull E, @NonNull K> provider) protected void
setReadyService
(ReadyService readyService) stream()
Returns a stream of all elements in the registry.protected void
unsetEventPublisher
(EventPublisher eventPublisher) protected void
unsetManagedProvider
(ManagedProvider<@NonNull E, @NonNull K> provider) protected void
unsetReadyService
(ReadyService readyService) @Nullable E
Updates the given element at the accordingManagedProvider
.void
Notifies the listener that a single element has been updated.void
-
Constructor Details
-
AbstractRegistry
Constructor.- Parameters:
providerClazz
- the class of the providers (see e.g.addProvider(Provider)
), null if no providers should be tracked automatically after activation
-
-
Method Details
-
activate
protected void activate(org.osgi.framework.BundleContext context) -
deactivate
protected void deactivate() -
waitForCompletedAsyncActivationTasks
public void waitForCompletedAsyncActivationTasks() -
added
Description copied from interface:ProviderChangeListener
Notifies the listener that a single element has been added.- Specified by:
added
in interfaceProviderChangeListener<E extends Identifiable<K>>
- Parameters:
provider
- the provider that provides the elementelement
- the element that has been added
-
addRegistryChangeListener
Description copied from interface:Registry
Adds aRegistryChangeListener
to the registry.- Specified by:
addRegistryChangeListener
in interfaceRegistry<E extends Identifiable<K>,
K> - Parameters:
listener
- registry change listener
-
getAll
Description copied from interface:Registry
Returns a collection of all elements in the registry. -
stream
Description copied from interface:Registry
Returns a stream of all elements in the registry. -
removed
Description copied from interface:ProviderChangeListener
Notifies the listener that a single element has been removed.- Specified by:
removed
in interfaceProviderChangeListener<E extends Identifiable<K>>
- Parameters:
provider
- the provider that provides the elementelement
- the element that has been removed
-
removeRegistryChangeListener
Description copied from interface:Registry
Removes aRegistryChangeListener
from the registry.- Specified by:
removeRegistryChangeListener
in interfaceRegistry<E extends Identifiable<K>,
K> - Parameters:
listener
- registry change listener
-
updated
Description copied from interface:ProviderChangeListener
Notifies the listener that a single element has been updated.- Specified by:
updated
in interfaceProviderChangeListener<E extends Identifiable<K>>
- Parameters:
provider
- the provider that provides the elementelement
- the element that has been updated
-
get
Description copied from interface:Registry
This method retrieves a single element from the registry. -
getValueAndProvider
This method retrieves an Entry with the provider and the element for the key from the registry.- Parameters:
key
- key of the element- Returns:
- provider and element entry or null if no element was found
-
add
Description copied from interface:Registry
Adds the given element to the accordingManagedProvider
. -
update
Description copied from interface:Registry
Updates the given element at the accordingManagedProvider
. -
remove
Description copied from interface:Registry
Removes the given element from the accordingManagedProvider
. -
notifyListeners
protected void notifyListeners(@NonNull E element, org.openhab.core.common.registry.AbstractRegistry.EventType eventType) -
notifyListeners
-
notifyListenersAboutAddedElement
-
notifyListenersAboutRemovedElement
-
notifyListenersAboutUpdatedElement
-
addProvider
-
getProvider
This method retrieves the provider of an element from the registry.- Parameters:
key
- key of the element- Returns:
- provider or null if no provider was found
-
getProvider
This method retrieves the provider of an element from the registry.- Parameters:
element
- the element- Returns:
- provider or null if no provider was found
-
forEach
This method traverses over all elements of a provider in the registry and calls the consumer with each element.The traversal over the elements is done while holding a lock for the respective internal collections. If you use this method, please ensure not execution time consuming stuff as it will block any other usage of that collections. You should also not call third party code that could e.g. access the registry itself again. This could lead to a dead lock and hard finding bugs. The
getAll()
andstream()
method will operate on a copy and so no lock is hold.- Parameters:
provider
- provider to traverse elements ofconsumer
- function to call with element
-
forEach
This method traverses over all elements in the registry and calls the consumer with each element.The traversal over the elements is done while holding a lock for the respective internal collections. If you use this method, please ensure not execution time consuming stuff as it will block any other usage of that collections. You should also not call third party code that could e.g. access the registry itself again. This could lead to a dead lock and hard finding bugs. The
getAll()
andstream()
method will operate on a copy and so no lock is hold.- Parameters:
consumer
- function to call with element
-
forEach
This method traverses over all elements in the registry and calls the consumer with the provider of the element as the first parameter and the element as the second argument.The traversal over the elements is done while holding a lock for the respective internal collections. If you use this method, please ensure not execution time consuming stuff as it will block any other usage of that collections. You should also not call third party code that could e.g. access the registry itself again. This could lead to a dead lock and hard finding bugs. The
getAll()
andstream()
method will operate on a copy and so no lock is hold.- Parameters:
consumer
- function to call with the provider and element
-
getManagedProvider
-
setManagedProvider
-
unsetManagedProvider
-
onAddElement
This method is called before an element is added. The implementing class can override this method to perform initialization logic or check the validity of the element.To keep custom logic on the inheritance chain, you must call always the super implementation first.
If the method throws an
IllegalArgumentException
the element will not be added.- Parameters:
element
- element to be added- Throws:
IllegalArgumentException
- if the element is invalid and should not be added
-
onRemoveElement
This method is called before an element is removed. The implementing class can override this method to perform specific logic.To keep custom logic on the inheritance chain, you must call always the super implementation first.
- Parameters:
element
- element to be removed
-
beforeUpdateElement
This method is called before an element is updated. The implementing class can override this method to perform specific logic.- Parameters:
existingElement
- the previously existing element (as held in the element cache)
-
onUpdateElement
protected void onUpdateElement(@NonNull E oldElement, @NonNull E element) throws IllegalArgumentException This method is called before an element is updated. The implementing class can override this method to perform specific logic or check the validity of the updated element.To keep custom logic on the inheritance chain, you must call always the super implementation first.
- Parameters:
oldElement
- old element (before update, as given by the provider)element
- updated element (after update)If the method throws an
IllegalArgumentException
the element will not be updated.- Throws:
IllegalArgumentException
- if the updated element is invalid and should not be updated
-
removeProvider
-
getEventPublisher
-
setEventPublisher
-
unsetEventPublisher
-
setReadyService
-
unsetReadyService
-
postEvent
This method can be used in a subclass in order to post events through the openHAB events bus. A common use case is to notify event subscribers about an element which has been added/removed/updated to the registry.- Parameters:
event
- the event
-