public final class ServiceHelper extends Object
IServiceProvider
.Modifier and Type | Method and Description |
---|---|
static <T> T |
getOptionalService(TypeLiteral<T> typeLiteralT,
IServiceProvider serviceProvider)
Gets a service from a given service provider, or
null if the service does not exist. |
static <T> T |
getService(TypeLiteral<T> typeLiteralT,
IServiceProvider serviceProvider)
Gets a service from a given service provider, throwing a
ServiceNotAvailableException
if the service is not available. |
static <T> T |
getService(TypeLiteral<T> typeLiteralT,
IServiceProvider serviceProvider,
boolean throwIfNotAvailable)
Gets a service from a given service provider.
|
static <T> T |
getService(TypeLiteral<T> typeLiteralT,
IServiceProvider serviceProvider,
String exceptionMessage)
Gets a service from a given service provider, throwing a
ServiceNotAvailableException
if the service is not available. |
static boolean |
hasService(IServiceProvider serviceProvider,
Class<?> serviceType)
Checks whether a service provider has a given service.
|
static Object |
locateService(Class<?> serviceType,
Object candidate)
Convenience method for implementing
IServiceProvider objects. |
static <T extends IServiceProvider> |
locateService(Class<?> serviceType,
Object candidate,
Iterable<T> additionalServiceProviders)
Convenience method for implementing
IServiceProvider objects. |
static <T extends IServiceProvider> |
locateService(Class<?> serviceType,
Object candidate,
T... additionalServiceProviders)
Convenience method for implementing
IServiceProvider objects. |
@Nonnull public static <T> T getService(TypeLiteral<T> typeLiteralT, @Nonnull IServiceProvider serviceProvider)
ServiceNotAvailableException
if the service is not available.T
- The type of service to obtain.typeLiteralT
- A TypeLiteral object representing the generic type T
.serviceProvider
- The service provider from which to obtain the service.ArgumentNullException
- Thrown when the serviceProvider
is null
.ServiceNotAvailableException
- Thrown when the requested service is not available from the service provider.@Nonnull public static <T> T getService(TypeLiteral<T> typeLiteralT, @Nonnull IServiceProvider serviceProvider, String exceptionMessage)
ServiceNotAvailableException
if the service is not available.T
- The type of service to obtain.typeLiteralT
- A TypeLiteral object representing the generic type T
.serviceProvider
- The service provider from which to obtain the service.exceptionMessage
- An error message to include in the exception if the service is unavailable.ArgumentNullException
- Thrown when the serviceProvider
is null
.ServiceNotAvailableException
- Thrown when the requested service is not available from the service provider.public static <T> T getService(TypeLiteral<T> typeLiteralT, @Nonnull IServiceProvider serviceProvider, boolean throwIfNotAvailable)
T
- The type of service to obtain.typeLiteralT
- A TypeLiteral object representing the generic type T
.serviceProvider
- The service provider from which to obtain the service.throwIfNotAvailable
- true
if this method should throw a ServiceNotAvailableException
if
the requested service is not available from the service provider, or false
if this
method should simply return null
if the service does not exist.ArgumentNullException
- Thrown when the serviceProvider
is null
.ServiceNotAvailableException
- Thrown when throwIfNotAvailable
is true
,
and the requested service is not available from the service provider.@Nullable public static <T> T getOptionalService(TypeLiteral<T> typeLiteralT, @Nonnull IServiceProvider serviceProvider)
null
if the service does not exist.T
- The type of service to obtain.typeLiteralT
- A TypeLiteral object representing the generic type T
.serviceProvider
- The service provider from which to obtain the service.null
if the service does not exist.ArgumentNullException
- Thrown when the serviceProvider
is null
.public static boolean hasService(@Nonnull IServiceProvider serviceProvider, @Nonnull Class<?> serviceType)
serviceProvider
- The service provider from which to obtain the service.serviceType
- The type of the service that is requested.true
if the service provider returns a non-null service of the given type.ArgumentNullException
- Thrown when serviceProvider
or serviceType
is null
.@Nullable public static Object locateService(@Nonnull Class<?> serviceType, @Nonnull Object candidate)
IServiceProvider
objects.
If candidate
is an instance of serviceType
,
returns candidate
, otherwise returns null
.candidate
- A candidate object that could be of the requested serviceType
.serviceType
- The type of the service that is requested.candidate
if it is of type serviceType
; otherwise null
.ArgumentNullException
- Thrown when serviceType
or candidate
is null
.@Nullable public static <T extends IServiceProvider> Object locateService(@Nonnull Class<?> serviceType, @Nonnull Object candidate, @Nonnull T... additionalServiceProviders)
IServiceProvider
objects.
If candidate
is an instance of serviceType
, returns candidate
.
Otherwise, checks additional IServiceProvider
objects for any that return a non-null result for serviceType
.
Otherwise, returns null
.T
- The type of additional service providers to search.candidate
- A candidate object that could be of the requested serviceType
.serviceType
- The type of the service that is requested.additionalServiceProviders
- Additional service providers that will be checked, in order, for the requested service. The first non-null
result will be returned.null
if not found.ArgumentNullException
- Thrown when serviceType
, candidate
, or additionalServiceProviders
is null
.@Nullable public static <T extends IServiceProvider> Object locateService(@Nonnull Class<?> serviceType, @Nonnull Object candidate, @Nonnull Iterable<T> additionalServiceProviders)
IServiceProvider
objects.
If candidate
is an instance of serviceType
, returns candidate
.
Otherwise, checks additional IServiceProvider
objects for any that return a non-null result for serviceType
.
Otherwise, returns null
.T
- The type of additional service providers to search.candidate
- A candidate object that could be of the requested serviceType
.serviceType
- The type of the service that is requested.additionalServiceProviders
- Additional service providers that will be checked, in order, for the requested service. The first non-null
result will be returned.null
if not found.ArgumentNullException
- Thrown when serviceType
, candidate
, or additionalServiceProviders
is null
.