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
ConstructorsModifierConstructorDescriptionprotectedAbstractRegistry(@Nullable Class<@NonNull P> providerClazz) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidactivate(org.osgi.framework.BundleContext context) Adds the given element to the accordingManagedProvider.voidNotifies the listener that a single element has been added.protected voidaddProvider(Provider<@NonNull E> provider) voidaddRegistryChangeListener(RegistryChangeListener<@NonNull E> listener) Adds aRegistryChangeListenerto the registry.protected voidbeforeUpdateElement(@NonNull E existingElement) This method is called before an element is updated.protected voidprotected voidforEach(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 voidThis method traverses over all elements in the registry and calls the consumer with each element.protected voidThis method traverses over all elements of a provider in the registry and calls the consumer with each element.@Nullable EThis method retrieves a single element from the registry.getAll()Returns a collection of all elements in the registry.protected @Nullable EventPublisherprotected 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 voidnotifyListeners(@NonNull E oldElement, @NonNull E element, org.openhab.core.common.registry.AbstractRegistry.EventType eventType) protected voidnotifyListeners(@NonNull E element, org.openhab.core.common.registry.AbstractRegistry.EventType eventType) protected voidnotifyListenersAboutAddedElement(@NonNull E element) protected voidnotifyListenersAboutRemovedElement(@NonNull E element) protected voidnotifyListenersAboutUpdatedElement(@NonNull E oldElement, @NonNull E element) protected voidonAddElement(@NonNull E element) This method is called before an element is added.protected voidonRemoveElement(@NonNull E element) This method is called before an element is removed.protected voidonUpdateElement(@NonNull E oldElement, @NonNull E element) This method is called before an element is updated.protected voidThis method can be used in a subclass in order to post events through the openHAB events bus.@Nullable ERemoves the given element from the accordingManagedProvider.voidNotifies the listener that a single element has been removed.protected voidremoveProvider(Provider<@NonNull E> provider) voidremoveRegistryChangeListener(RegistryChangeListener<@NonNull E> listener) Removes aRegistryChangeListenerfrom the registry.protected voidsetEventPublisher(EventPublisher eventPublisher) protected voidsetManagedProvider(ManagedProvider<@NonNull E, @NonNull K> provider) protected voidsetReadyService(ReadyService readyService) stream()Returns a stream of all elements in the registry.protected voidunsetEventPublisher(EventPublisher eventPublisher) protected voidunsetManagedProvider(ManagedProvider<@NonNull E, @NonNull K> provider) protected voidunsetReadyService(ReadyService readyService) @Nullable EUpdates the given element at the accordingManagedProvider.voidNotifies 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:ProviderChangeListenerNotifies the listener that a single element has been added.- Specified by:
addedin interfaceProviderChangeListener<E extends Identifiable<K>>- Parameters:
provider- the provider that provides the elementelement- the element that has been added
-
addRegistryChangeListener
Description copied from interface:RegistryAdds aRegistryChangeListenerto the registry.- Specified by:
addRegistryChangeListenerin interfaceRegistry<E extends Identifiable<K>,K> - Parameters:
listener- registry change listener
-
getAll
Description copied from interface:RegistryReturns a collection of all elements in the registry. -
stream
Description copied from interface:RegistryReturns a stream of all elements in the registry. -
removed
Description copied from interface:ProviderChangeListenerNotifies the listener that a single element has been removed.- Specified by:
removedin interfaceProviderChangeListener<E extends Identifiable<K>>- Parameters:
provider- the provider that provides the elementelement- the element that has been removed
-
removeRegistryChangeListener
Description copied from interface:RegistryRemoves aRegistryChangeListenerfrom the registry.- Specified by:
removeRegistryChangeListenerin interfaceRegistry<E extends Identifiable<K>,K> - Parameters:
listener- registry change listener
-
updated
Description copied from interface:ProviderChangeListenerNotifies the listener that a single element has been updated.- Specified by:
updatedin interfaceProviderChangeListener<E extends Identifiable<K>>- Parameters:
provider- the provider that provides the elementelement- the element that has been updated
-
get
Description copied from interface:RegistryThis 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:RegistryAdds the given element to the accordingManagedProvider. -
update
Description copied from interface:RegistryUpdates the given element at the accordingManagedProvider. -
remove
Description copied from interface:RegistryRemoves 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
IllegalArgumentExceptionthe 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
IllegalArgumentExceptionthe 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
-