Interface DecoratorDefinition<C,D extends C>

Type Parameters:
C - The declared type of the component.
D - The instance type of the decorated component.
All Known Subinterfaces:
DecoratorDefinition.CompletedDecoratorDefinition<C,D>

public sealed interface DecoratorDefinition<C,D extends C> permits DecoratorDefinition.CompletedDecoratorDefinition<C,D>
Defines the structure of a decorator for components in the Configuration of the application or one of its Modules.

Decorators can wrap or replace the implementation of defined components based on their type and optionally their name. Decorators must return an instance that is an implementation of the declared type. Typically, they wrap another component to provide additional behavior.

To create a decorator for a component of type MyComponentInterface with an implementation that has a dependency, it would look as follows:


 DecoratorDefinition.forType(MyComponentInterface.class)
                    .with((config, name, delegate) -> new MyComponentWrapper(delegate, config.getComponent(MyDependency.class)))
                    .onStart(0, MyComponentWrapper::start)
                    .onShutdown(0, MyComponentWrapper::shutdown)
 

Alternatively, you can use:


     DecoratorDefinition.forTypeAndName(MyComponentInterface.class, "MyName")
                        .with(config -> ...)
 
In this case, you need to only wrap a component with a given name.
Since:
5.0.0
Author:
Allard Buijze
  • Method Details

    • forType

      static <C> DecoratorDefinition.PartialDecoratorDefinition<C> forType(@Nonnull Class<C> type)
      Initiates the configuration of a decorator for components with the given type, which must correspond with the declared type of these components.

      If multiple components have been defined for this type, they all are subject to decoration under this definition. To decorate only a specific component, consider using forTypeAndName(Class, String).

      Type Parameters:
      C - The declared type of the components to decorate.
      Parameters:
      type - The class of the declared type of the components to decorate.
      Returns:
      A builder for further configuration of this decorator definition.
    • forTypeAndName

      static <C> DecoratorDefinition.PartialDecoratorDefinition<C> forTypeAndName(@Nonnull Class<C> type, @Nonnull String name)
      Initiates the configuration of a decorator for a component with the given type and name, which must correspond with the declared type of these components.

      The decorator is only invoked if such component with such name is defined in the component registry where this decorator is registered. If multiple components for this type have been defined, and all are subject to decoration, consider using forType(Class).

      Type Parameters:
      C - The declared type of the components to decorate.
      Parameters:
      type - The class of the declared type of the components to decorate.
      name - The name of the component to decorate.
      Returns:
      A builder for further configuration of this decorator definition.
    • order

      DecoratorDefinition<C,D> order(int order)
      Sets the order in which this decorator will be invoked on a component relative to other decorators.

      The relative order of decorators with the same order is undefined.

      In absence of configuration, the order is 0.

      Parameters:
      order - The relative order in which to invoke this decorator.
      Returns:
      This DecoratorDefinition fur fluent API.
    • onStart

      DecoratorDefinition<C,D> onStart(int phase, @Nonnull ComponentLifecycleHandler<D> handler)
      Registers the given handler to be registered with the application's lifecycle during startup for this decorator.

      The handler will be invoked in the given startup phase for each component that has been decorated.

      Parameters:
      phase - The phase in which the handler must be invoked.
      handler - The handler to invoke.
      Returns:
      This DecoratorDefinition fur fluent API.
    • onStart

      default DecoratorDefinition<C,D> onStart(int phase, @Nonnull Consumer<D> handler)
      Registers the given handler to be registered with the application's lifecycle during startup for this decorator.

      The handler will be invoked in the given startup phase for each component that has been decorated.

      Parameters:
      phase - The phase in which the start handler must be invoked.
      handler - The handler to invoke.
      Returns:
      This DecoratorDefinition fur fluent API.
    • onShutdown

      DecoratorDefinition<C,D> onShutdown(int phase, @Nonnull ComponentLifecycleHandler<D> handler)
      Registers the given handler to be registered with the application's lifecycle during shutdown for this decorator.

      The handler will be invoked in the given shutdown phase for each component that has been decorated.

      Parameters:
      phase - The phase in which the shutdown handler must be invoked.
      handler - The handler to invoke.
      Returns:
      This DecoratorDefinition fur fluent API.
    • onShutdown

      default DecoratorDefinition<C,D> onShutdown(int phase, @Nonnull Consumer<D> handler)
      Registers the given handler to be registered with the application's lifecycle during shutdown for this decorator.

      The handler will be invoked in the given shutdown phase for each component that has been decorated.

      Parameters:
      phase - The phase in which the shutdown handler must be invoked.
      handler - The handler to invoke.
      Returns:
      This DecoratorDefinition fur fluent API.