io.fluxcapacitor.javaclient.eventsourcing.AnnotatedEventSourcingHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-client Show documentation
Show all versions of java-client Show documentation
Default Java client library for interfacing with Flux Capacitor.
package io.fluxcapacitor.javaclient.eventsourcing;
import io.fluxcapacitor.common.handling.HandlerInspector;
import io.fluxcapacitor.common.handling.HandlerInvoker;
import io.fluxcapacitor.common.handling.HandlerNotFoundException;
import io.fluxcapacitor.common.handling.ParameterResolver;
import io.fluxcapacitor.javaclient.common.Message;
import java.util.Arrays;
import java.util.List;
public class AnnotatedEventSourcingHandler implements EventSourcingHandler {
private final Class handlerType;
private final HandlerInvoker invoker;
public AnnotatedEventSourcingHandler(Class handlerType) {
this(handlerType, Arrays.asList(new PayloadParameterResolver(), new MetadataParameterResolver()));
}
public AnnotatedEventSourcingHandler(Class handlerType,
List> parameterResolvers) {
this.handlerType = handlerType;
this.invoker = HandlerInspector.inspect(handlerType, ApplyEvent.class, parameterResolvers);
}
@Override
public T apply(Message message, T model) {
Object result;
try {
result = invoker.invoke(model, message);
} catch (HandlerNotFoundException e) {
if (model == null) {
throw e;
}
return model;
}
if (model == null) {
return handlerType.cast(result);
}
if (handlerType.isInstance(result)) {
return handlerType.cast(result);
}
if (result == null && invoker.expectResult(message)) {
return null; //this handler has deleted the model on purpose
}
return model; //apparently the model is mutable
}
}