Class AbstractRegistry<E extends Identifiable<K>,K,P extends Provider<E>>

java.lang.Object
org.openhab.core.common.registry.AbstractRegistry<E,K,P>
Type Parameters:
E - type of the element
K - type of the key
P - type of the provider
All Implemented Interfaces:
ProviderChangeListener<E>, Registry<E,K>
Direct Known Subclasses:
AbstractLinkRegistry

@NonNullByDefault public abstract class AbstractRegistry<@NonNull E extends Identifiable<@NonNull K>,@NonNull K,@NonNull P extends Provider<@NonNull E>> extends Object implements ProviderChangeListener<E>, Registry<E,K>
The 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 Details

    • AbstractRegistry

      protected AbstractRegistry(@Nullable Class<@NonNull P> providerClazz)
      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

      public void added(Provider<@NonNull E> provider, @NonNull E element)
      Description copied from interface: ProviderChangeListener
      Notifies the listener that a single element has been added.
      Specified by:
      added in interface ProviderChangeListener<E extends Identifiable<K>>
      Parameters:
      provider - the provider that provides the element
      element - the element that has been added
    • addRegistryChangeListener

      public void addRegistryChangeListener(RegistryChangeListener<@NonNull E> listener)
      Description copied from interface: Registry
      Adds a RegistryChangeListener to the registry.
      Specified by:
      addRegistryChangeListener in interface Registry<E extends Identifiable<K>,K>
      Parameters:
      listener - registry change listener
    • getAll

      public Collection<E> getAll()
      Description copied from interface: Registry
      Returns a collection of all elements in the registry.
      Specified by:
      getAll in interface Registry<E extends Identifiable<K>,K>
      Returns:
      collection of all elements in the registry
    • stream

      public Stream<E> stream()
      Description copied from interface: Registry
      Returns a stream of all elements in the registry.
      Specified by:
      stream in interface Registry<E extends Identifiable<K>,K>
      Returns:
      stream of all elements in the registry
    • removed

      public void removed(Provider<@NonNull E> provider, @NonNull E element)
      Description copied from interface: ProviderChangeListener
      Notifies the listener that a single element has been removed.
      Specified by:
      removed in interface ProviderChangeListener<E extends Identifiable<K>>
      Parameters:
      provider - the provider that provides the element
      element - the element that has been removed
    • removeRegistryChangeListener

      public void removeRegistryChangeListener(RegistryChangeListener<@NonNull E> listener)
      Description copied from interface: Registry
      Removes a RegistryChangeListener from the registry.
      Specified by:
      removeRegistryChangeListener in interface Registry<E extends Identifiable<K>,K>
      Parameters:
      listener - registry change listener
    • updated

      public void updated(Provider<@NonNull E> provider, @NonNull E oldElement, @NonNull E element)
      Description copied from interface: ProviderChangeListener
      Notifies the listener that a single element has been updated.
      Specified by:
      updated in interface ProviderChangeListener<E extends Identifiable<K>>
      Parameters:
      provider - the provider that provides the element
      element - the element that has been updated
    • get

      public @Nullable E get(@NonNull K key)
      Description copied from interface: Registry
      This method retrieves a single element from the registry.
      Specified by:
      get in interface Registry<E extends Identifiable<K>,K>
      Parameters:
      key - key of the element
      Returns:
      element or null if no element was found
    • getValueAndProvider

      protected @Nullable Map.Entry<Provider<E>,E> getValueAndProvider(@NonNull K key)
      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

      public E add(@NonNull E element)
      Description copied from interface: Registry
      Adds the given element to the according ManagedProvider.
      Specified by:
      add in interface Registry<E extends Identifiable<K>,K>
      Parameters:
      element - element to be added (must not be null)
      Returns:
      the added element or newly created object of the same type
    • update

      public @Nullable E update(@NonNull E element)
      Description copied from interface: Registry
      Updates the given element at the according ManagedProvider.
      Specified by:
      update in interface Registry<E extends Identifiable<K>,K>
      Parameters:
      element - element to be updated (must not be null)
      Returns:
      returns the old element or null if no element with the same key exists
    • remove

      public @Nullable E remove(@NonNull K key)
      Description copied from interface: Registry
      Removes the given element from the according ManagedProvider.
      Specified by:
      remove in interface Registry<E extends Identifiable<K>,K>
      Parameters:
      key - key of the element (must not be null)
      Returns:
      element that was removed, or null if no element with the given key exists
    • notifyListeners

      protected void notifyListeners(@NonNull E element, org.openhab.core.common.registry.AbstractRegistry.EventType eventType)
    • notifyListeners

      protected void notifyListeners(@NonNull E oldElement, @NonNull E element, org.openhab.core.common.registry.AbstractRegistry.EventType eventType)
    • notifyListenersAboutAddedElement

      protected void notifyListenersAboutAddedElement(@NonNull E element)
    • notifyListenersAboutRemovedElement

      protected void notifyListenersAboutRemovedElement(@NonNull E element)
    • notifyListenersAboutUpdatedElement

      protected void notifyListenersAboutUpdatedElement(@NonNull E oldElement, @NonNull E element)
    • addProvider

      protected void addProvider(Provider<@NonNull E> provider)
    • getProvider

      protected @Nullable Provider<E> getProvider(@NonNull K key)
      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

      public @Nullable Provider<E> getProvider(@NonNull E element)
      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

      protected void forEach(Provider<@NonNull E> provider, Consumer<@NonNull E> consumer)
      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() and stream() method will operate on a copy and so no lock is hold.

      Parameters:
      provider - provider to traverse elements of
      consumer - function to call with element
    • forEach

      protected void forEach(Consumer<@NonNull E> consumer)
      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() and stream() method will operate on a copy and so no lock is hold.

      Parameters:
      consumer - function to call with element
    • forEach

      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.

      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() and stream() method will operate on a copy and so no lock is hold.

      Parameters:
      consumer - function to call with the provider and element
    • getManagedProvider

      protected Optional<ManagedProvider<E,K>> getManagedProvider()
    • setManagedProvider

      protected void setManagedProvider(ManagedProvider<@NonNull E,@NonNull K> provider)
    • unsetManagedProvider

      protected void unsetManagedProvider(ManagedProvider<@NonNull E,@NonNull K> provider)
    • onAddElement

      protected void onAddElement(@NonNull E element) throws IllegalArgumentException
      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

      protected void onRemoveElement(@NonNull E element)
      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

      protected void beforeUpdateElement(@NonNull E existingElement)
      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

      protected void removeProvider(Provider<@NonNull E> provider)
    • getEventPublisher

      protected @Nullable EventPublisher getEventPublisher()
    • setEventPublisher

      protected void setEventPublisher(EventPublisher eventPublisher)
    • unsetEventPublisher

      protected void unsetEventPublisher(EventPublisher eventPublisher)
    • setReadyService

      protected void setReadyService(ReadyService readyService)
    • unsetReadyService

      protected void unsetReadyService(ReadyService readyService)
    • postEvent

      protected void postEvent(Event event)
      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