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

javafixes.object.changing.builder.DerivedJoinedValueBuilder Maven / Gradle / Ivy

There is a newer version: 1.4.2.0
Show newest version
package javafixes.object.changing.builder;

import javafixes.common.function.TriFunction;
import javafixes.object.changing.ChangingValue;
import javafixes.object.changing.DerivedJoinedValue;
import javafixes.object.changing.FailableValue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

public class DerivedJoinedValueBuilder extends AbstractChangingValueBuilder> {

    private final List> sourceValues;
    private final Function>, ? extends T> valuesMapper;

    private boolean prePopulateValueImmediately = false;

    public  DerivedJoinedValueBuilder(
            List> sourceValues,
            Function>, ? extends T> valuesMapper
    ) {
        this.sourceValues = new ArrayList<>(sourceValues);
        this.valuesMapper = (Function) valuesMapper;
    }


    public static  DerivedJoinedValueBuilder joinBuilder(
            ChangingValue value1,
            ChangingValue value2,
            BiFunction, FailableValue, OutputType> mapFunction
    ) {
        return new DerivedJoinedValueBuilder<>(
                Arrays.asList(value1, value2),
                values -> {
                    FailableValue val1 = (FailableValue) values.get(0);
                    FailableValue val2 = (FailableValue) values.get(1);
                    return mapFunction.apply(val1, val2);
                }
        );
    }

    public static  DerivedJoinedValueBuilder joinBuilder(
            ChangingValue value1,
            ChangingValue value2,
            ChangingValue value3,
            TriFunction, FailableValue, FailableValue, OutputType> mapFunction
    ) {
        return new DerivedJoinedValueBuilder<>(
                Arrays.asList(value1, value2, value3),
                values -> {
                    FailableValue val1 = (FailableValue) values.get(0);
                    FailableValue val2 = (FailableValue) values.get(1);
                    FailableValue val3 = (FailableValue) values.get(2);
                    return mapFunction.apply(val1, val2, val3);
                }
        );
    }

    public static  DerivedJoinedValueBuilder joinBuilder(
            List> values,
            Function>, OutputType> mapFunction
    ) {
        return new DerivedJoinedValueBuilder<>(values, mapFunction);
    }


    public static  DerivedJoinedValue join(
            ChangingValue value1,
            ChangingValue value2,
            BiFunction, FailableValue, OutputType> mapFunction
    ) {
        return joinBuilder(value1, value2, mapFunction).build();
    }

    public static  DerivedJoinedValue join(
            ChangingValue value1,
            ChangingValue value2,
            ChangingValue value3,
            TriFunction, FailableValue, FailableValue, OutputType> mapFunction
    ) {
        return joinBuilder(value1, value2, value3, mapFunction).build();
    }

    public static  DerivedJoinedValue join(
            List> values,
            Function>, OutputType> mapFunction
    ) {
        return joinBuilder(values, mapFunction).build();
    }


    @Override
    public DerivedJoinedValue build() {
        return new DerivedJoinedValue<>(
                valueName,
                sourceValues,
                updateConfig(),
                valuesMapper,
                prePopulateValueImmediately
        );
    }

    public DerivedJoinedValueBuilder withPrePopulateValueImmediately(boolean prePopulateValueImmediately) {
        this.prePopulateValueImmediately = prePopulateValueImmediately;
        return this;
    }

    @Override
    protected DerivedJoinedValueBuilder thisBuilder() {
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy