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

com.fluxtion.ext.streaming.builder.factory.PushBuilder Maven / Gradle / Ivy

There is a newer version: 2.10.50
Show newest version
package com.fluxtion.ext.streaming.builder.factory;

import com.fluxtion.api.partition.LambdaReflection.MethodReferenceReflection;
import com.fluxtion.api.partition.LambdaReflection.SerializableConsumer;
import com.fluxtion.api.partition.LambdaReflection.SerializableFunction;
import com.fluxtion.api.partition.LambdaReflection.SerializableSupplier;
import com.fluxtion.builder.generation.GenerationContext;
import com.fluxtion.ext.streaming.api.PushNotifier;
import com.fluxtion.ext.streaming.api.Wrapper;
import static com.fluxtion.ext.streaming.builder.factory.EventSelect.select;
import com.fluxtion.ext.streaming.builder.stream.StreamFunctionCompiler;

/**
 * Factory for building {@link PushNotifier} instances.
 *
 * @author V12 Technology Ltd.
 */
public class PushBuilder {

    /**
     * Adds and event graph dependency from the source to the target. The target
     * will be notified if the source is on the currently executing event path.
     *
     * @param 
     * @param 
     * @param source
     * @param target
     * @return
     */
    public static  S pushNotification(S source, T target) {
        PushNotifier p = GenerationContext.SINGLETON.addOrUseExistingNode(new PushNotifier(source, target));
        return source;
    }
        
    public static void pushSource(T source,  SerializableConsumer consumer){
        Object targetInstance = null;
        if(consumer.captured().length >0){
            targetInstance = consumer.captured()[0];
        }
        StreamFunctionCompiler.push(targetInstance, consumer.method(), source, null, true).build();
    }

   public static  void push(SerializableFunction supplier, SerializableConsumer consumer) {
        Object sourceInstance = null;//unWrap(supplier);
        if( supplier.captured().length == 0){
            sourceInstance = select(supplier.getContainingClass());
        }else{
            sourceInstance = supplier.captured()[0];
        }
        final Object targetInstance = consumer.captured()[0];//unWrap(consumer);
        StreamFunctionCompiler.push(targetInstance, consumer.method(), sourceInstance, supplier.method(), true).build();
    }
    
    /**
     * Pushes data from the source method to the target method when the source
     * is on the executing event path.
     *
     * @param 
     * @param 
     * @param supplier
     * @param consumer
     */
    public static  void push(SerializableSupplier supplier, SerializableConsumer consumer) {
        Object sourceInstance = supplier.captured()[0];//unWrap(supplier);
        if(sourceInstance == null){
            sourceInstance = select(supplier.getContainingClass());
        }
        final Object targetInstance = consumer.captured()[0];//unWrap(consumer);
        StreamFunctionCompiler.push(targetInstance, consumer.method(), sourceInstance, supplier.method(), true).build();
    }
    
    public static  Wrapper push(SerializableSupplier supplier, SerializableFunction  consumer) {
        Object sourceInstance = supplier.captured()[0];//unWrap(supplier);
        if(sourceInstance == null){
            sourceInstance = select(supplier.getContainingClass());
        }
        final Object targetInstance = consumer.captured()[0];//unWrap(consumer);
        return StreamFunctionCompiler.push(targetInstance, consumer.method(), sourceInstance, supplier.method(), true).build();
    }

    public static Object unWrap(MethodReferenceReflection supplier) {
        final Object sourceInstance = supplier.captured()[0];
        if (sourceInstance instanceof Wrapper) {
            return ((Wrapper) sourceInstance).event();
        } else {
            return sourceInstance;
        }
    }

}