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

com.fluxtion.runtime.dataflow.function.MapFlowFunction Maven / Gradle / Ivy

The newest version!
package com.fluxtion.runtime.dataflow.function;

import com.fluxtion.runtime.annotations.NoTriggerReference;
import com.fluxtion.runtime.annotations.OnTrigger;
import com.fluxtion.runtime.dataflow.DefaultValueSupplier;
import com.fluxtion.runtime.dataflow.DoubleFlowFunction;
import com.fluxtion.runtime.dataflow.FlowFunction;
import com.fluxtion.runtime.dataflow.IntFlowFunction;
import com.fluxtion.runtime.dataflow.LongFlowFunction;
import com.fluxtion.runtime.partition.LambdaReflection;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.lang.reflect.Method;

import static com.fluxtion.runtime.partition.LambdaReflection.*;

/**
 * Base class for all mapping operations
 *
 * @param  Incoming type
 * @param  Output type
 * @param  Previous {@link FlowFunction} type
 */
public abstract class MapFlowFunction> extends AbstractFlowFunction {

    protected transient String auditInfo;
    protected transient R result;

    @SuppressWarnings("unchecked")
    public MapFlowFunction(S inputEventStream, MethodReferenceReflection methodReferenceReflection) {
        super(inputEventStream, methodReferenceReflection);
        if (methodReferenceReflection != null) {
            Method method = methodReferenceReflection.method();
            auditInfo = method.getDeclaringClass().getSimpleName() + "->" + method.getName();
        }
    }

    @OnTrigger
    public final boolean map() {
        auditLog.info("mapFunction", auditInfo);
        if (executeUpdate()) {
            auditLog.info("invokeMapFunction", true);
            mapOperation();
        } else if (reset()) {
            auditLog.info("invokeMapFunction", false);
            auditLog.info("reset", true);
//            resetOperation();
        } else {
            auditLog.info("invokeMapFunction", false);
        }
        return fireEventUpdateNotification();
    }

    @Override
    protected void initialise() {
        Method method = getStreamFunction().method();
        if (DefaultValueSupplier.class.isAssignableFrom(method.getDeclaringClass())) {
            mapOperation();
        }
    }

    @Override
    public boolean hasDefaultValue() {
        return DefaultValueSupplier.class.isAssignableFrom(getStreamFunction().method().getDeclaringClass());
    }

    @Override
    public R get() {
        return result;
    }

    abstract protected void mapOperation();

    protected void resetOperation() {
        result = resetFunction.reset();
    }


    //***************** REFERENCE map producers START *****************//
    @EqualsAndHashCode(callSuper = true)
    public static class MapRef2RefFlowFunction> extends MapFlowFunction {

        private final SerializableFunction mapFunction;

        public MapRef2RefFlowFunction(S inputEventStream, SerializableFunction mapFunction) {
            super(inputEventStream, mapFunction);
            this.mapFunction = mapFunction;
        }

        protected void mapOperation() {
            result = mapFunction.apply(getInputEventStream().get());
        }

    }

    @EqualsAndHashCode(callSuper = true)
    public static class MapInt2RefFlowFunction extends MapFlowFunction {

        private final SerializableIntFunction mapFunction;

        public MapInt2RefFlowFunction(IntFlowFunction inputEventStream, SerializableIntFunction mapFunction) {
            super(inputEventStream, mapFunction);
            this.mapFunction = mapFunction;
        }

        protected void mapOperation() {
            result = mapFunction.apply(getInputEventStream().getAsInt());
        }

    }

    @EqualsAndHashCode(callSuper = true)
    public static class MapDouble2RefFlowFunction extends MapFlowFunction {

        private final SerializableDoubleFunction mapFunction;

        public MapDouble2RefFlowFunction(DoubleFlowFunction inputEventStream, SerializableDoubleFunction mapFunction) {
            super(inputEventStream, mapFunction);
            this.mapFunction = mapFunction;
        }

        protected void mapOperation() {
            result = mapFunction.apply(getInputEventStream().getAsDouble());
        }

    }

    @EqualsAndHashCode(callSuper = true)
    public static class MapLong2RefFlowFunction extends MapFlowFunction {

        private final SerializableLongFunction mapFunction;

        public MapLong2RefFlowFunction(LongFlowFunction inputEventStream, SerializableLongFunction mapFunction) {
            super(inputEventStream, mapFunction);
            this.mapFunction = mapFunction;
        }

        protected void mapOperation() {
            result = mapFunction.apply(getInputEventStream().getAsLong());
        }

    }

    //***************** REFERENCE map producers START *****************//


    //***************** INTEGER map producers START *****************//

    /**
     * Base class for mapping to an {@link IntFlowFunction}
     *
     * @param  Input type
     * @param  {@link FlowFunction} input type
     */
    abstract static class AbstractMapToIntFlowFunction> extends MapFlowFunction implements IntFlowFunction {

        protected transient int result;

        public AbstractMapToIntFlowFunction(S inputEventStream, MethodReferenceReflection method) {
            super(inputEventStream, method);
        }

        protected void resetOperation() {
            result = resetFunction.reset();
        }

        @Override
        public Integer get() {
            return getAsInt();
        }

        @Override
        public int getAsInt() {
            return result;
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapRef2ToIntFlowFunction> extends AbstractMapToIntFlowFunction {
        private final SerializableToIntFunction intUnaryOperator;

        public MapRef2ToIntFlowFunction(S inputEventStream, SerializableToIntFunction intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsInt(getInputEventStream().get());
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapInt2ToIntFlowFunction extends AbstractMapToIntFlowFunction {
        @NoTriggerReference
        private final SerializableIntUnaryOperator intUnaryOperator;

        public MapInt2ToIntFlowFunction(IntFlowFunction inputEventStream, SerializableIntUnaryOperator intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsInt(getInputEventStream().getAsInt());
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapDouble2ToIntFlowFunction extends AbstractMapToIntFlowFunction {
        private final SerializableDoubleToIntFunction intUnaryOperator;

        public MapDouble2ToIntFlowFunction(DoubleFlowFunction inputEventStream, SerializableDoubleToIntFunction intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsInt(getInputEventStream().getAsDouble());
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapLong2ToIntFlowFunction extends AbstractMapToIntFlowFunction {
        private final SerializableLongToIntFunction intUnaryOperator;

        public MapLong2ToIntFlowFunction(LongFlowFunction inputEventStream, SerializableLongToIntFunction intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsInt(getInputEventStream().getAsLong());
        }
    }

    //***************** INTEGER map producers END *****************//


    //***************** DOUBLE map producers START *****************//

    /**
     * Base class for mapping to an {@link DoubleFlowFunction}
     *
     * @param  Input type
     * @param  {@link FlowFunction} input type
     */
    abstract static class AbstractMapToDoubleFlowFunction> extends MapFlowFunction implements DoubleFlowFunction {

        protected transient double result;

        public AbstractMapToDoubleFlowFunction(S inputEventStream, MethodReferenceReflection method) {
            super(inputEventStream, method);
        }

        protected void resetOperation() {
            result = resetFunction.reset();
        }

        @Override
        public Double get() {
            return getAsDouble();
        }

        @Override
        public double getAsDouble() {
            return result;
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapRef2ToDoubleFlowFunction> extends AbstractMapToDoubleFlowFunction {
        private final SerializableToDoubleFunction intUnaryOperator;

        public MapRef2ToDoubleFlowFunction(S inputEventStream, SerializableToDoubleFunction intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsDouble(getInputEventStream().get());
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapDouble2ToDoubleFlowFunction extends AbstractMapToDoubleFlowFunction {
        private final SerializableDoubleUnaryOperator intUnaryOperator;

        public MapDouble2ToDoubleFlowFunction(DoubleFlowFunction inputEventStream, SerializableDoubleUnaryOperator intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsDouble(getInputEventStream().getAsDouble());
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapInt2ToDoubleFlowFunction extends AbstractMapToDoubleFlowFunction {
        private final SerializableIntToDoubleFunction intUnaryOperator;

        public MapInt2ToDoubleFlowFunction(IntFlowFunction inputEventStream, SerializableIntToDoubleFunction intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsDouble(getInputEventStream().getAsInt());
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapLong2ToDoubleFlowFunction extends AbstractMapToDoubleFlowFunction {
        private final SerializableLongToDoubleFunction intUnaryOperator;

        public MapLong2ToDoubleFlowFunction(LongFlowFunction inputEventStream, SerializableLongToDoubleFunction intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsDouble(getInputEventStream().getAsLong());
        }
    }

    //***************** DOUBLE map producers END *****************//


    //***************** LONG map producers START *****************//

    /**
     * Base class for mapping to an {@link LongFlowFunction}
     *
     * @param  Input type
     * @param  {@link FlowFunction} input type
     */
    abstract static class AbstractMapToLongFlowFunction> extends MapFlowFunction implements LongFlowFunction {

        protected transient long result;

        public AbstractMapToLongFlowFunction(S inputEventStream, MethodReferenceReflection method) {
            super(inputEventStream, method);
        }

        protected void resetOperation() {
            result = resetFunction.reset();
        }

        @Override
        public Long get() {
            return getAsLong();
        }

        @Override
        public long getAsLong() {
            return result;
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapRef2ToLongFlowFunction> extends AbstractMapToLongFlowFunction {
        private final LambdaReflection.SerializableToLongFunction intUnaryOperator;

        public MapRef2ToLongFlowFunction(S inputEventStream, LambdaReflection.SerializableToLongFunction intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsLong(getInputEventStream().get());
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapLong2ToLongFlowFunction extends AbstractMapToLongFlowFunction {
        private final SerializableLongUnaryOperator intUnaryOperator;

        public MapLong2ToLongFlowFunction(LongFlowFunction inputEventStream, SerializableLongUnaryOperator intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsLong(getInputEventStream().getAsLong());
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapInt2ToLongFlowFunction extends AbstractMapToLongFlowFunction {
        private final SerializableIntToLongFunction intUnaryOperator;

        public MapInt2ToLongFlowFunction(IntFlowFunction inputEventStream, SerializableIntToLongFunction intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsLong(getInputEventStream().getAsInt());
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @ToString
    public static class MapDouble2ToLongFlowFunction extends AbstractMapToLongFlowFunction {
        private final SerializableDoubleToLongFunction intUnaryOperator;

        public MapDouble2ToLongFlowFunction(DoubleFlowFunction inputEventStream, SerializableDoubleToLongFunction intUnaryOperator) {
            super(inputEventStream, intUnaryOperator);
            this.intUnaryOperator = intUnaryOperator;
        }

        @Override
        protected void mapOperation() {
            result = intUnaryOperator.applyAsLong(getInputEventStream().getAsDouble());
        }
    }
    //***************** LONG map producers END *****************//
}