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

io.virtdata.libbasics.shared.functionadapters.Flow Maven / Gradle / Ivy

There is a newer version: 2.12.15
Show newest version
package io.virtdata.libbasics.shared.functionadapters;

import io.virtdata.annotations.Categories;
import io.virtdata.annotations.Category;
import io.virtdata.annotations.ThreadSafeMapper;
import io.virtdata.api.composers.FunctionAssembly;
import io.virtdata.api.composers.FunctionComposer;
import io.virtdata.core.ResolvedFunction;
import io.virtdata.util.VirtDataFunctions;

import java.util.function.LongFunction;

/**
 * 

Combine functions into one.

* *

This function allows you to combine multiple other functions into one. This is often useful * for constructing more sophisticated recipes, when you don't have the ability to use * control flow or non-functional forms.

* *

The functions will be stitched together using the same logic that VirtData uses when * combining flows outside functions. That said, if the functions selected are not the right ones, * then it is possible to end up with the wrong data type at the end. To remedy this, be sure * to add input and output qualifiers, like long-> or ->String where * appropriate, to ensure that VirtData selects the right functions within the flow.

*/ @Categories(Category.conversion) @ThreadSafeMapper public class Flow implements LongFunction { private final LongFunction f; public Flow(Object... funcs) { FunctionComposer assembly = new FunctionAssembly(); for (Object func : funcs) { assembly = assembly.andThen(func); } ResolvedFunction rf = assembly.getResolvedFunction(); Object functionObject = rf.getFunctionObject(); f = VirtDataFunctions.adapt(functionObject,LongFunction.class, Object.class, true); // f = LongFunction.class.cast(functionObject); } @Override public Object apply(long value) { Object o = f.apply(value); return o; } }