Interface ServiceRegistry


public interface ServiceRegistry
Holds the implementations of the services through which the server delegates its behaviour.

A service is identified by an interface, for instance CombatService. Several implementations may be registered for the same service; the one with the highest ServicePriority is used, the first registered one winning a tie.

Since:
0.1.0
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> Optional<T>
    find(Class<T> service)
    Gets the implementation in use for a service, if any.
    <T> List<T>
    findAll(Class<T> service)
    Gets every implementation registered for a service, for instance to delegate to the one a plugin replaced.
    <T> T
    get(Class<T> service)
    Gets the implementation in use for a service.
    default <T> void
    register(Class<T> service, T implementation, Object owner)
    Registers an implementation of a service at ServicePriority.NORMAL priority.
    <T> void
    register(Class<T> service, T implementation, Object owner, ServicePriority priority)
    Registers an implementation of a service.
    <T> boolean
    unregister(Class<T> service, T implementation)
    Removes one implementation of a service.
    void
    Removes every implementation registered by owner, whatever the service.
  • Method Details

    • register

      <T> void register(Class<T> service, T implementation, Object owner, ServicePriority priority)
      Registers an implementation of a service.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service interface
      implementation - the implementation
      owner - the owner of the registration, usually the Plugin instance; it is released by unregisterAll(Object)
      priority - the rank of this implementation
      Throws:
      IllegalArgumentException - if implementation does not implement service
      Since:
      0.1.0
    • register

      default <T> void register(Class<T> service, T implementation, Object owner)
      Registers an implementation of a service at ServicePriority.NORMAL priority.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service interface
      implementation - the implementation
      owner - the owner of the registration, usually the Plugin instance
      Throws:
      IllegalArgumentException - if implementation does not implement service
      Since:
      0.1.0
    • get

      <T> T get(Class<T> service)
      Gets the implementation in use for a service.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service interface
      Returns:
      the implementation with the highest priority
      Throws:
      IllegalStateException - if no implementation is registered
      Since:
      0.1.0
    • find

      <T> Optional<T> find(Class<T> service)
      Gets the implementation in use for a service, if any.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service interface
      Returns:
      the implementation with the highest priority, or empty if none is registered
      Since:
      0.1.0
    • findAll

      <T> List<T> findAll(Class<T> service)
      Gets every implementation registered for a service, for instance to delegate to the one a plugin replaced.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service interface
      Returns:
      an immutable snapshot of the implementations, the one in use first
      Since:
      0.1.0
    • unregister

      <T> boolean unregister(Class<T> service, T implementation)
      Removes one implementation of a service.
      Type Parameters:
      T - the service type
      Parameters:
      service - the service interface
      implementation - the implementation to remove
      Returns:
      true if the implementation was registered
      Since:
      0.1.0
    • unregisterAll

      void unregisterAll(Object owner)
      Removes every implementation registered by owner, whatever the service.
      Parameters:
      owner - the owner given at registration
      Since:
      0.1.0