Class UseCaseHandler<I,O>

java.lang.Object
com.guinetik.hexafun.hexa.UseCaseHandler<I,O>
Type Parameters:
I - The input type for this use case
O - The output type for this use case
All Implemented Interfaces:
UseCase<I,O>
Direct Known Subclasses:
SysmonHandlers.GetAllMetricsHandler, SysmonHandlers.GetCpuHandler, SysmonHandlers.GetDiskHandler, SysmonHandlers.GetMemoryHandler

public abstract class UseCaseHandler<I,O> extends Object implements UseCase<I,O>
Abstract base class for use case handlers that need access to ports.

This class provides a convenient way to implement use cases that require infrastructure dependencies (ports) while maintaining the functional interface contract of UseCase.

When to use UseCaseHandler

  • When your use case needs to access repositories, external services, or other ports
  • When you want to externalize handler logic into dedicated classes
  • When handlers need to be tested with different port implementations

When to use lambdas instead

  • For simple, pure transformations with no port dependencies
  • For one-liner handlers that don't need testing in isolation

Example


 public class CreateTaskHandler extends UseCaseHandler<CreateTask, Result<Task>> {
     public CreateTaskHandler(HexaApp app) {
         super(app);
     }

     @Override
     public Result<Task> apply(CreateTask input) {
         TaskRepository repo = port(TaskRepository.class);
         Task task = Task.create(input.title());
         return Result.ok(repo.save(task));
     }
 }

 // Registration
 HexaApp app = HexaFun.dsl()
     .port(TaskRepository.class, new InMemoryTaskRepo())
     .useCase(CREATE_TASK)
         .handle(new CreateTaskHandler(app))
     .build();
 
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final HexaApp
    Reference to the HexaApp for port access.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a new handler with access to the given HexaApp.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    hasPort(Class<?> type)
    Check if a port is registered for the given type.
    protected <T> T
    port(Class<T> type)
    Convenience method to retrieve a port by its type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.guinetik.hexafun.hexa.UseCase

    apply
  • Field Details

    • app

      protected final HexaApp app
      Reference to the HexaApp for port access.
  • Constructor Details

    • UseCaseHandler

      protected UseCaseHandler(HexaApp app)
      Creates a new handler with access to the given HexaApp.
      Parameters:
      app - The HexaApp instance for port access
  • Method Details

    • port

      protected <T> T port(Class<T> type)
      Convenience method to retrieve a port by its type.

      This is a shorthand for app.port(type).

      Type Parameters:
      T - The port type
      Parameters:
      type - The interface/class type to retrieve
      Returns:
      The registered implementation
      Throws:
      IllegalArgumentException - if no port is registered for the given type
    • hasPort

      protected boolean hasPort(Class<?> type)
      Check if a port is registered for the given type.
      Parameters:
      type - The interface/class type to check
      Returns:
      true if a port is registered, false otherwise