Class AbstractServiceRegistry<S extends Identifiable<K>,K>

java.lang.Object
org.openhab.core.common.registry.AbstractServiceRegistry<S,K>
Type Parameters:
S - type of the identifiable service
K - type of the key
All Implemented Interfaces:
Registry<S,K>

@NonNullByDefault public abstract class AbstractServiceRegistry<@NonNull S extends Identifiable<@NonNull K>,@NonNull K> extends Object implements Registry<S,K>
The AbstractServiceRegistry is an abstract implementation of the Registry interface, that can be used as a base class for Registry implementations that track registered OSGi services and provide access to them.

This Registry implementation provides access to registered OSGi services by their Identifiable.getUID(). It does NOT support adding, updating, or removing elements manually.

In comparison to AbstractRegistry, this implementation does not track Providers and query them for elements. Instead, elements are registered directly as OSGi services. Also, there is no ManagedProvider for this Registry implementation to write to, so adding, updating, or removing elements through the Registry methods is not possible. Use this class to implement Registry-like access to OSGi services of a specific service type. Use AbstractRegistry when elements are to be provided by Providers and the Registry methods to add, update, and remove elements are required to work.

Author:
Florian Hotze - Initial contribution
Implementation note:
Implementations must define OSGi bind and unbind methods for the service type they track, and call addService(Identifiable) and removeService(Identifiable) when the service is bound or unbound.
  • Field Details

  • Constructor Details

    • AbstractServiceRegistry

      public AbstractServiceRegistry()
  • Method Details

    • addService

      protected void addService(@NonNull S service)
    • removeService

      protected void removeService(@NonNull S service)
    • get

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

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

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

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

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

      public S add(@NonNull S element)
      Description copied from interface: Registry
      Adds the given element to the according ManagedProvider.
      Specified by:
      add in interface Registry<S 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 S update(@NonNull S element)
      Description copied from interface: Registry
      Updates the given element at the according ManagedProvider.
      Specified by:
      update in interface Registry<S 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 S remove(@NonNull K key)
      Description copied from interface: Registry
      Removes the given element from the according ManagedProvider.
      Specified by:
      remove in interface Registry<S 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