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

com.fluxtion.ext.streaming.api.MergingWrapper Maven / Gradle / Ivy

There is a newer version: 2.10.50
Show newest version
/* 
 * Copyright (C) 2018 V12 Technology Ltd.
 *
 * 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.ext.streaming.api;

import com.fluxtion.api.SepContext;
import com.fluxtion.api.annotations.OnEvent;
import com.fluxtion.api.annotations.OnParentUpdate;
import java.util.ArrayList;
import java.util.List;

/**
 * Merges streams into a single node in the SEP execution graph. The merge will make available
 * the last parent that was updated via the {@link #event() } call.
 *
 * @author V12 Technology Ltd.
 */
public class MergingWrapper implements Wrapper {

    private T event;
    private final Class clazz;
    public List wrappedNodes;
    public List nodes;
    
    public static  MergingWrapper merge(Class clazz, Wrapper... nodes){
        MergingWrapper merger = new MergingWrapper<>(clazz);
        merger.mergeWrappers(nodes);
        return SepContext.service().add(merger);
    }
    
    public static  MergingWrapper merge(Wrapper... nodes){
        MergingWrapper merger = new MergingWrapper<>(nodes[0].eventClass());
        merger.mergeWrappers(nodes);
        return SepContext.service().add(merger);
    }
    
    public static  MergingWrapper merge(Class clazz, S... nodes){
        MergingWrapper merger = new MergingWrapper<>(clazz);
        merger.mergeNodes(nodes);
        return SepContext.service().add(merger);
    }
    
    public static  MergingWrapper merge(T... nodes){
        MergingWrapper merger = new MergingWrapper<>((Class)nodes[0].getClass());
        merger.mergeNodes(nodes);
        return SepContext.service().add(merger);
    }

    public MergingWrapper(Class clazz) {
        this.clazz = clazz;
        wrappedNodes = new ArrayList<>();
        nodes = new ArrayList<>();
    }

    @OnParentUpdate("wrappedNodes")
    public void mergeWrapperUpdate(Wrapper wrappedNode) {
        event = wrappedNode.event();
    }

    @OnParentUpdate("nodes")
    public  void mergeUpdate(S node) {
        event = node;
    }

    @OnEvent
    public boolean updated() {
        return true;
    }

    public Wrapper mergeWrappers(Wrapper... nodes) {
        for (Wrapper node : nodes) {
            wrappedNodes.add(node);
        }
        return this;
    }
    
    public  Wrapper mergeNodes(S... nodesT){
        for (S node : nodesT) {
            nodes.add(node);
        }
        return this;
    }

    @Override
    public T event() {
        return event;
    }

    @Override
    public Class eventClass() {
        return clazz;
    }

}