Class DefaultHandler<M>

java.lang.Object
io.fluxzero.common.handling.DefaultHandler<M>
All Implemented Interfaces:
Handler<M>

public class DefaultHandler<M> extends Object implements Handler<M>
Default implementation of the Handler interface.
  • Constructor Details

  • Method Details

    • forTarget

      public static <M> DefaultHandler<M> forTarget(Class<?> targetClass, Object target, HandlerMatcher<Object,M> handlerMatcher)
    • forPayloadTarget

      public static <M> DefaultHandler<M> forPayloadTarget(Class<?> targetClass, Function<M,?> fallbackTargetSupplier, HandlerMatcher<Object,M> handlerMatcher)
      Creates a handler for methods declared on the message payload itself.

      Prepared invocations obtain their target from HandlerInput.getPayload() each time, so a cached plan never retains an earlier payload instance. If an invocation cannot use the prepared path, the supplied fallback resolves the target from the complete message.

      Type Parameters:
      M - the message type used by the handling pipeline
      Parameters:
      targetClass - the payload type that declares the handler methods
      fallbackTargetSupplier - function that obtains the handler target from a complete message
      handlerMatcher - the matcher used to select a handler method
      Returns:
      a handler for methods whose invocation target is the current payload
    • getTargetClass

      public Class<?> getTargetClass()
      Description copied from interface: Handler
      Returns the class of the handler's target object. This may be used for reflective operations, logging, or framework-level behavior.
      Specified by:
      getTargetClass in interface Handler<M>
      Returns:
      the class of the handler's target
    • getInvoker

      public Optional<HandlerInvoker> getInvoker(M message)
      Description copied from interface: Handler
      Returns a HandlerInvoker capable of processing the given message, if available.
      Specified by:
      getInvoker in interface Handler<M>
      Parameters:
      message - the message to be handled
      Returns:
      an optional HandlerInvoker if this handler can handle the message; otherwise Optional.empty()
    • getInvokerOrNull

      public HandlerInvoker getInvokerOrNull(M message)
      Description copied from interface: Handler
      Returns a HandlerInvoker capable of processing the given message, or null when unavailable.

      This is a lower-allocation counterpart to Handler.getInvoker(Object) for internal hot paths. Implementations that can resolve an invoker without creating an Optional should override this method.

      Specified by:
      getInvokerOrNull in interface Handler<M>
      Parameters:
      message - the message to be handled
      Returns:
      an invoker if this handler can handle the message; null otherwise
    • getHandlerMethodOrNull

      public HandlerMethod<M> getHandlerMethodOrNull(M message)
      Description copied from interface: Handler
      Returns a reusable HandlerMethod capable of processing the given message, or null when unavailable.

      This is an optional lower-allocation path for handlers whose target and method plan can be reused across messages. Implementations that cannot expose a stable method should return null and rely on Handler.getInvokerOrNull(Object).

      Specified by:
      getHandlerMethodOrNull in interface Handler<M>
      Parameters:
      message - the message to be handled
      Returns:
      a handler method if this handler can handle the message through a reusable method; null otherwise
    • getHandlerMethodPlanOrNull

      public HandlerMethodPlan<M> getHandlerMethodPlanOrNull(M message)
      Description copied from interface: Handler
      Returns a reusable plan for the handler method selected for this message.

      The default asks Handler.getHandlerMethodPlanner() to prepare the plan. A null result means that this handler cannot safely prepare the invocation, so the caller should use Handler.getInvokerOrNull(Object) or Handler.getHandlerMethodOrNull(Object) instead.

      Specified by:
      getHandlerMethodPlanOrNull in interface Handler<M>
      Parameters:
      message - the message to match
      Returns:
      the prepared invocation plan, or null when preparation is unsupported or no method matches
    • getHandlerMethodPlanner

      public HandlerMethodPlanner<M> getHandlerMethodPlanner()
      Description copied from interface: Handler
      Returns the planner used to select and prepare this handler's methods.

      The returned planner may be cached and invoked concurrently. The default returns null, which keeps implementations that only support regular per-message matching fully compatible.

      Specified by:
      getHandlerMethodPlanner in interface Handler<M>
      Returns:
      a thread-safe handler method planner, or null when this handler does not support preparation
    • toString

      public String toString()
      Overrides:
      toString in class Object