Interface OptionalService<T>

Type Parameters:
T - the tracked service type
All Known Subinterfaces:
OptionalService.OptionalFilterableService<T>
All Known Implementing Classes:
FallbackOptionalService, StaticOptionalService

public interface OptionalService<T>
API for an "optional" service.

This API is like a simplified OSGi ServiceTracker. Calling the service() method will return the first available matching service, or null if none available.

Version:
2.0
Author:
matt
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    A convenient configurable optional service.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    Resolve a required optional service, throwing an exception if not available.
    static <T> T
    requiredService(OptionalService<T> optional, String description)
    Resolve a required optional service, throwing an exception if not available.
    Get the configured service, or null if none available.
    static <T> T
    service(OptionalService<T> optional)
    Resolve an optional service.
    static <T> T
    service(OptionalService<T> optional, T fallback)
    Resolve an optional service with a fallback.
  • Method Details

    • service

      T service()
      Get the configured service, or null if none available.
      Returns:
      the service, or null
    • service

      static <T> T service(OptionalService<T> optional)
      Resolve an optional service.

      This method is a convenient way to deal with a possibly null OptionalService.

      Type Parameters:
      T - the service type
      Parameters:
      optional - the optional service, or null
      Returns:
      the resolved service, or null
      Since:
      1.1
    • service

      static <T> T service(OptionalService<T> optional, T fallback)
      Resolve an optional service with a fallback.

      This method is a convenient way to deal with a possibly null OptionalService.

      Type Parameters:
      T - the service type
      Parameters:
      optional - the optional service, or null
      fallback - the result to return if optional is null or its resolved service is null
      Returns:
      the resolved service, or fallback
      Since:
      1.1
    • requiredService

      static <T> T requiredService(OptionalService<T> optional)
      Resolve a required optional service, throwing an exception if not available.
      Type Parameters:
      T - the service type
      Parameters:
      optional - the optional service, or null
      Returns:
      the resolved service, never null
      Throws:
      OptionalServiceNotAvailableException - if the service can not be resolved
      Since:
      2.0
    • requiredService

      static <T> T requiredService(OptionalService<T> optional, String description)
      Resolve a required optional service, throwing an exception if not available.
      Type Parameters:
      T - the service type
      Parameters:
      optional - the optional service, or null
      description - a description to use if the service can not be resolved, or null
      Returns:
      the resolved service, never null
      Throws:
      OptionalServiceNotAvailableException - if the service can not be resolved
      Since:
      2.0