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
The service class is the class corresponding to the service type.
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 TypeMethodDescription<T> List<Registration<T>> getAllRegistrations(Class<T> service) 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.<T> Optional<Registration<T>> getRegistration(Class<T> service) Retrieves the highest priority registration for a service.<T> booleanisProvidedFor(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> Registers a resource as a specific service and generates aRegistrationto 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.<T> Optional<Registration<T>> unregister(Class<T> service, Registration<T> registration) Unregisters the specifiedRegistration, and returns the updated highest priority registration after the specifiedRegistrationis unregistered.
-
Method Details
-
register
Registers a resource as a specific service and generates aRegistrationto represent the registration which was just added.
The provider and name must be nonnull.
Higher priority registrations will be preferred forgetProvider(Class).
Services may be registered under multiple intended service types.- Type Parameters:
T- the service type- Parameters:
service- the service classpriority- the registration priorityprovider- the resource to register, must not be nullname- 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
Registers a resource as a specific service and returns the highest priority registration for the service.
This is similar toregister(Class, byte, Object, String), but instead of returning aRegistrationbased 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 classpriority- the registration priorityprovider- the resource to register, must not be nullname- 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
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
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
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
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
Unregisters the specifiedRegistration, and returns the updated highest priority registration after the specifiedRegistrationis unregistered.
If the specifiedRegistrationwas not registered, this is a no-op.- Type Parameters:
T- the service type- Parameters:
service- the service classregistration- the registration to unregister- Returns:
- the updated highest priority registration for the service, or an empty optional if there is none
-