All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fluxcapacitor.javaclient.eventsourcing.AnnotatedEventSourcingHandler Maven / Gradle / Ivy

There is a newer version: 0.1015.0
Show newest version
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
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy