Class DefaultServiceRegistry

java.lang.Object
eu.cloudnetservice.driver.registry.DefaultServiceRegistry
All Implemented Interfaces:
ServiceRegistry

@Singleton @Provides(ServiceRegistry.class) public class DefaultServiceRegistry extends Object implements ServiceRegistry
Represents the default service registry implementation.
Since:
4.0
  • Field Details

  • Constructor Details

    • DefaultServiceRegistry

      public DefaultServiceRegistry()
  • Method Details

    • registerProvider

      @NonNull public <T, E extends T> @NonNull ServiceRegistry registerProvider(@NonNull @NonNull Class<T> service, @NonNull @NonNull String name, @NonNull E provider)
      Registers the given provider for the given service to this service registry. After registration the given provider will be the first provider of the given service and retrievable with the given name.

      If multiple services with the same name are registered the first one will be returned which was registered to this registry.

      Specified by:
      registerProvider in interface ServiceRegistry
      Type Parameters:
      T - the service class type to register the service to.
      E - the provider (or service implementation) class type.
      Parameters:
      service - the service class to register the implementation to.
      name - the name of the provider to register.
      provider - the provider (and service implementation) to register.
      Returns:
      the same instance as used to call the method, for chaining.
    • unregisterProvider

      @NonNull public <T, E extends T> @NonNull ServiceRegistry unregisterProvider(@NonNull @NonNull Class<T> service, @NonNull @NonNull Class<E> provider)
      Unregisters all providers for the given service which are an instance of the given class.
      Specified by:
      unregisterProvider in interface ServiceRegistry
      Type Parameters:
      T - the type of the service.
      E - the type of the providers.
      Parameters:
      service - the service to unregister the providers from.
      provider - the class of the providers to unregister.
      Returns:
      the same instance as used to call the method, for chaining.
    • unregisterProvider

      @NonNull public <T, E extends T> @NonNull ServiceRegistry unregisterProvider(@NonNull @NonNull Class<T> service, @NonNull E provider)
      Unregisters the given provider instance from the registered services of the given service class.
      Specified by:
      unregisterProvider in interface ServiceRegistry
      Type Parameters:
      T - the type of the service.
      E - the type of the providers.
      Parameters:
      service - the service to unregister the provider from.
      provider - the provider instance to unregister.
      Returns:
      the same instance as used to call the method, for chaining.
    • hasProvider

      public <T> boolean hasProvider(@NonNull @NonNull Class<T> clazz, @NonNull @NonNull String name)
      Checks if a provider with the given name is registered for the given service.
      Specified by:
      hasProvider in interface ServiceRegistry
      Type Parameters:
      T - the type of the service.
      Parameters:
      clazz - the service to check the providers of.
      name - the name of the service to check for.
      Returns:
      true if this registry has a provider with the given name for the service, false otherwise.
    • unregisterProvider

      @NonNull public <T> @NonNull ServiceRegistry unregisterProvider(@NonNull @NonNull Class<T> service, @NonNull @NonNull String name)
      Unregisters the provider with the given name from the given service if registered previously.
      Specified by:
      unregisterProvider in interface ServiceRegistry
      Type Parameters:
      T - the type of the service.
      Parameters:
      service - the service to unregister the provider from.
      name - the name of the provider to unregister.
      Returns:
      the same instance as used to call the method, for chaining.
    • unregisterProviders

      @NonNull public <T> @NonNull ServiceRegistry unregisterProviders(@NonNull @NonNull Class<T> service)
      Removes all previously registered providers for the given service.
      Specified by:
      unregisterProviders in interface ServiceRegistry
      Type Parameters:
      T - the type of the service.
      Parameters:
      service - the service to unregisters the providers of.
      Returns:
      the same instance as used to call the method, for chaining.
    • unregisterAll

      @NonNull public @NonNull ServiceRegistry unregisterAll()
      Unregisters all previously registered providers for all services from this registry.
      Specified by:
      unregisterAll in interface ServiceRegistry
      Returns:
      the same instance as used to call the method, for chaining.
    • unregisterAll

      @NonNull public @NonNull ServiceRegistry unregisterAll(@NonNull @NonNull ClassLoader classLoader)
      Unregisters all providers for any service from this registry whose classes were loaded by the given class loader.
      Specified by:
      unregisterAll in interface ServiceRegistry
      Parameters:
      classLoader - the class loader of the providers to unregister.
      Returns:
      the same instance as used to call the method, for chaining.
    • providedServices

      @NonNull public @NonNull Collection<Class<?>> providedServices()
      Get all services for which a provider was registered previously.
      Specified by:
      providedServices in interface ServiceRegistry
      Returns:
      all services for which a provider was registered previously.
    • provider

      public <T> T provider(@NonNull @NonNull Class<T> service, @NonNull @NonNull String name)
      Get the provider for the given service with the given name from this registry. This method returns null if no such provider was registered previously.

      This method will return the first provider registered with the given name if multiple providers with the same name for the given service were registered.

      Specified by:
      provider in interface ServiceRegistry
      Type Parameters:
      T - the type of the service.
      Parameters:
      service - the service of the provider.
      name - the name of the provider to get.
      Returns:
      the provider for the given service with the given name, null if no such provider was registered.
    • providers

      @NonNull public <T> @NonNull Collection<T> providers(@NonNull @NonNull Class<T> service)
      Get all registered providers for the given service. This method returns an empty collection if no providers are registered for the given service but never null.
      Specified by:
      providers in interface ServiceRegistry
      Type Parameters:
      T - the type of the service.
      Parameters:
      service - the service to get all registered providers of.
      Returns:
      all registered providers for the service or an empty collection if no providers were registered for it.