Interface Registry


public interface Registry
A framework for registering and loading services.

Used to register providers of services which will be utilised by dependents according to the priority of registration.

A service is a type, usually an interface or abstract class, which multiple programs may decide to implement. An implementation of a service is considered a provider.

Providers may be registered with a priority and an optional user friendly name. These details comprise a registration (which is really just a wrapper class for the provider, priority, and name). See Registration.

The service class is the class corresponding to the service type.
Author:
A248
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets all registrations for a specific service, as an immutable copy.
    <T> Optional<T>
    getProvider(Class<T> service)
    Finds the highest priority registration for a service and returns its provider.
    getRegistration(Class<T> service)
    Retrieves the highest priority registration for a service.
    <T> boolean
    isProvidedFor(Class<T> service)
    Checks whether a service has any accompanying provider

    This method should only be used in the rare case where one needs to check whether a service is registered but does not need to retrieve the registration itself.
    <T> Registration<T>
    register(Class<T> service, byte priority, T provider, String name)
    Registers a resource as a specific service and generates a Registration to represent the registration which was just added.
    <T> Registration<T>
    registerAndGet(Class<T> service, byte priority, T provider, String name)
    Registers a resource as a specific service and returns the highest priority registration for the service.
    unregister(Class<T> service, Registration<T> registration)
    Unregisters the specified Registration, and returns the updated highest priority registration after the specified Registration is unregistered.
  • Method Details

    • register

      <T> Registration<T> register(Class<T> service, byte priority, T provider, String name)
      Registers a resource as a specific service and generates a Registration to represent the registration which was just added.

      The provider and name must be nonnull.
      Higher priority registrations will be preferred for getProvider(Class).

      Services may be registered under multiple intended service types.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      priority - the registration priority
      provider - the resource to register, must not be null
      name - a user friendly name for the implementation, must not be null
      Returns:
      the registration which was added to the registry, formed from the parameters
      Throws:
      DuplicateRegistrationException - if the provider is already registered for the service type
    • registerAndGet

      <T> Registration<T> registerAndGet(Class<T> service, byte priority, T provider, String name)
      Registers a resource as a specific service and returns the highest priority registration for the service.
      This is similar to register(Class, byte, Object, String), but instead of returning a Registration based on the resource registered, this returns the highest priority registration after computations.

      The provider and name must be nonnull.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      priority - the registration priority
      provider - the resource to register, must not be null
      name - a user friendly name for the implementation, must not be null
      Returns:
      the highest priority registration after the resource is registered
      Throws:
      DuplicateRegistrationException - if the provider is already registered for the service type
    • getProvider

      <T> Optional<T> getProvider(Class<T> service)
      Finds the highest priority registration for a service and returns its provider.
      If no registration for the service is found, an empty optional is returned.

      The proper way to retrieve registrations is to call this method once, and check if the value is present. If the optional is empty, there is no registration for the service.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      Returns:
      the highest priority provider or an empty optional if not found
    • getRegistration

      <T> Optional<Registration<T>> getRegistration(Class<T> service)
      Retrieves the highest priority registration for a service.

      If no registration for the service is found, an empty optional is returned.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      Returns:
      the highest priority registration or an empty optional if not found
    • getAllRegistrations

      <T> List<Registration<T>> getAllRegistrations(Class<T> service)
      Gets all registrations for a specific service, as an immutable copy.
      The list is sorted in ascending priority. The last element has the highest priority.

      If no registrations for the service are found, an empty list is returned.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      Returns:
      an unmodifiable copy of all registrations for the service, never null
    • isProvidedFor

      <T> boolean isProvidedFor(Class<T> service)
      Checks whether a service has any accompanying provider

      This method should only be used in the rare case where one needs to check whether a service is registered but does not need to retrieve the registration itself.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      Returns:
      true if the service is provided for, false if not
    • unregister

      <T> Optional<Registration<T>> unregister(Class<T> service, Registration<T> registration)
      Unregisters the specified Registration, and returns the updated highest priority registration after the specified Registration is unregistered.

      If the specified Registration was not registered, this is a no-op.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service class
      registration - the registration to unregister
      Returns:
      the updated highest priority registration for the service, or an empty optional if there is none