Package com.guinetik.hexafun.hexa
Class UseCaseHandler<I,O>
java.lang.Object
com.guinetik.hexafun.hexa.UseCaseHandler<I,O>
- Type Parameters:
I- The input type for this use caseO- The output type for this use case
- All Implemented Interfaces:
UseCase<I,O>
- Direct Known Subclasses:
SysmonHandlers.GetAllMetricsHandler,SysmonHandlers.GetCpuHandler,SysmonHandlers.GetDiskHandler,SysmonHandlers.GetMemoryHandler
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 -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedUseCaseHandler(HexaApp app) Creates a new handler with access to the given HexaApp. -
Method Summary
-
Field Details
-
app
Reference to the HexaApp for port access.
-
-
Constructor Details
-
UseCaseHandler
Creates a new handler with access to the given HexaApp.- Parameters:
app- The HexaApp instance for port access
-
-
Method Details
-
port
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
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
-