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

com.fluxtion.compiler.builder.dataflow.MultiPushBuilder Maven / Gradle / Ivy

/*
 * Copyright (c) 2025 gregory higgins.
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program.  If not, see
 * .
 */

package com.fluxtion.compiler.builder.dataflow;

import com.fluxtion.runtime.dataflow.FlowFunction;
import com.fluxtion.runtime.dataflow.TriggeredFlowFunction;
import com.fluxtion.runtime.dataflow.function.MergeMapToNodeFlowFunction;
import com.fluxtion.runtime.dataflow.function.MergeProperty;
import com.fluxtion.runtime.partition.LambdaReflection;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * A builder that merges and maps several {@link FlowFunction}'s into a single event stream of type T
 *
 * @param  The output type of the merged stream
 */
public class MultiPushBuilder {

    private final T resultInstance;
    private final List> required = new ArrayList<>();

    private MultiPushBuilder(T resultInstance) {
        this.resultInstance = resultInstance;
    }


    public static  MultiPushBuilder of(T resultInstance) {
        return new MultiPushBuilder(resultInstance);
    }

    public  MultiPushBuilder required(FlowBuilder trigger, LambdaReflection.SerializableBiConsumer setValue) {
        required.add(new MergeProperty(trigger.eventStream, setValue, true, true));
        return this;
    }

    public  MultiPushBuilder requiredNoTrigger(FlowBuilder trigger, LambdaReflection.SerializableBiConsumer setValue) {
        required.add(new MergeProperty(trigger.eventStream, setValue, false, true));
        return this;
    }

    /**
     * Builds a FlowBuilder that is formed from multiple inouts pushing to a target instance.
     *
     * @param target   Supplier of target instances that store the result of the push
     * @param joinLegs The legs that supply the inputs to the join
     * @param       The key class
     * @param       The join target class
     * @return The GroupByFlow with a new instance of the target allocated to every key
     */
    @SuppressWarnings("all")
    public static  FlowBuilder merge(LambdaReflection.SerializableSupplier target, MergeInput... joinLegs) {
        MultiPushBuilder multiJoinBuilder = new MultiPushBuilder(target);
        for (MergeInput joinLeg : joinLegs) {
            if (joinLeg.isTriggering()) {
                multiJoinBuilder.required(joinLeg.flow, joinLeg.getSetter());
            } else {
                multiJoinBuilder.requiredNoTrigger(joinLeg.flow, joinLeg.getSetter());
            }
        }
        return multiJoinBuilder.dataFlow();
    }

    @SuppressWarnings("all")
    public static  FlowBuilder mergeToNode(T target, MergeInput... joinLegs) {
        MultiPushBuilder multiJoinBuilder = new MultiPushBuilder(target);
        for (MergeInput joinLeg : joinLegs) {
            if (joinLeg.isTriggering()) {
                multiJoinBuilder.required(joinLeg.flow, joinLeg.getSetter());
            } else {
                multiJoinBuilder.requiredNoTrigger(joinLeg.flow, joinLeg.getSetter());
            }
        }
        return multiJoinBuilder.dataFlow();
    }

    public static  MergeInput requiredMergeInput(FlowBuilder flow,
                                                               LambdaReflection.SerializableBiConsumer setter) {
        return new MultiPushBuilder.MergeInput<>(true, flow, setter);
    }

    public static  MergeInput optionalMergeInput(FlowBuilder flow,
                                                               LambdaReflection.SerializableBiConsumer setter) {
        return new MultiPushBuilder.MergeInput<>(false, flow, setter);
    }

    @Data
    public static class MergeInput {
        private final boolean triggering;
        private final FlowBuilder flow;
        private final LambdaReflection.SerializableBiConsumer setter;
    }

    public TriggeredFlowFunction build() {
        TriggeredFlowFunction flowFunction;
        MergeMapToNodeFlowFunction streamNode = new MergeMapToNodeFlowFunction<>(resultInstance);
        required.forEach(streamNode::registerTrigger);
        flowFunction = streamNode;
        return flowFunction;
    }

    /**
     * Merges and maps several {@link FlowFunction}'s into a single event stream of type T
     *
     * @return An {@link FlowBuilder} that can used to construct stream processing logic
     */
    public FlowBuilder dataFlow() {
        return new FlowBuilder<>(build());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy