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

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

There is a newer version: 2.10.50
Show newest version
/*
 * Copyright (c) 2019, V12 Technology Ltd.
 * 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.ext.streaming.builder.factory;

import com.fluxtion.api.partition.LambdaReflection.MethodReferenceReflection;
import com.fluxtion.api.partition.LambdaReflection.SerializableBiFunction;
import com.fluxtion.api.partition.LambdaReflection.SerializableFunction;
import com.fluxtion.api.partition.LambdaReflection.SerializableQuadFunction;
import com.fluxtion.api.partition.LambdaReflection.SerializableSupplier;
import com.fluxtion.api.partition.LambdaReflection.SerializableTriFunction;
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;
import com.fluxtion.ext.streaming.builder.stream.StreamOperatorService;
import com.fluxtion.ext.streaming.api.stream.Argument;
import static com.fluxtion.ext.streaming.api.stream.Argument.arg;

/**
 * Provides helper methods to build mapping functions from nodes and Events.
 *
 * @author Greg Higgins [email protected]
 */
public class MappingBuilder {

    /**
     * Build a mapping function with nary inputs
     *
     * @param  The output type of the mapping function
     * @param test the test function to apply
     * @param args the input arguments for the mapping function
     * @return the output of the function as {@link Wrapper} stream
     */
    public static  Wrapper map(MethodReferenceReflection test, Argument... args) {
        final Object mapperInstance = test.captured().length == 0 ? null : test.captured()[0];
        StreamFunctionCompiler builder = StreamFunctionCompiler.map(mapperInstance, test.method(), args);
        final Wrapper wrapper = builder.build();
        wrapper.alwaysReset(false);
        return wrapper;
    }

    public static  Wrapper map(SerializableFunction mapper,
            SerializableFunction supplier) {
        return select(supplier.getContainingClass()).map(mapper, supplier);
    }

    public static  Wrapper map(SerializableFunction mapper,
            Argument arg1) {
        return map((MethodReferenceReflection)mapper, arg1);
    }

    public static  Wrapper map(SerializableFunction mapper,
            SerializableSupplier supplier) {
        return map((MethodReferenceReflection) mapper, arg(supplier));
    }
    
    public static  Wrapper map(SerializableBiFunction mapper,
            Argument arg1,
            Argument arg2) {
        return map((MethodReferenceReflection) mapper, arg1, arg2);
    }

    public static  Wrapper map(SerializableBiFunction mapper,
            SerializableFunction supplier1,
            SerializableFunction supplier2) {
        return map((MethodReferenceReflection) mapper, arg(supplier1), arg(supplier2));
    }

    
    public static  Wrapper map(SerializableTriFunction mapper,
            Argument arg1,
            Argument arg2,
            Argument arg3
            ) {
        return map((MethodReferenceReflection) mapper, arg1, arg2, arg3);
    } 
    
    public static  Wrapper map(SerializableTriFunction mapper,
            SerializableFunction supplier1,
            SerializableFunction supplier2,
            SerializableFunction supplier3
    ) {
        return map((MethodReferenceReflection) mapper, arg(supplier1), arg(supplier2), arg(supplier3));
    }
    
    public static  Wrapper map(SerializableQuadFunction mapper,
            Argument arg1,
            Argument arg2,
            Argument arg3,
            Argument arg4
            ) {
        return map((MethodReferenceReflection) mapper, arg1, arg2, arg3, arg4);
    } 
    
    public static  Wrapper map(SerializableQuadFunction mapper,
            SerializableFunction supplier1,
            SerializableFunction supplier2,
            SerializableFunction supplier3,
            SerializableFunction supplier4
    ) {
        return map((MethodReferenceReflection) mapper, arg(supplier1), arg(supplier2), arg(supplier3), arg(supplier4));
    }
    
    /**
     * Maps a set of nodes with a single mapping function. The function will
     * consult each node on every update.
     *
     * @param 
     * @param 
     * @param mapper
     * @param suppliers
     * @return
     */
    public static  Wrapper mapSet(SerializableFunction mapper,
            Argument... suppliers) {
        StreamFunctionCompiler builder = StreamFunctionCompiler.mapSet(mapper.captured()[0], mapper.method(), suppliers);
        final Wrapper wrapper = builder.build();
        wrapper.alwaysReset(true);
        return wrapper;
    }

    /**
     * Maps a set of nodes with a mapping function. Only nodes that notify in
     * the current execution path are included in the mapping function.
     *
     * @param 
     * @param 
     * @param mapper
     * @param suppliers
     * @return
     */
    public static  Wrapper mapSetOnUpdate(SerializableFunction mapper,
            SerializableSupplier... suppliers) {
        //create instance of function and wrap
        final Object targetInstance = mapper.captured()[0];
        Wrapper stream = (Wrapper) StreamOperatorService.stream(targetInstance);
        for (SerializableSupplier supplier : suppliers) {
            PushBuilder.push(supplier, mapper);
        }
        return stream;
    }
}