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

com.alwa.spread.ListSpread Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
package com.alwa.spread;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ListSpread extends Spread {

    private final int steps;

    public ListSpread(
        Function stepFunction,
        Function mapFunction,
        int steps,
        Object... seedsOrExamples) {
            super(stepFunction, mapFunction, seedsOrExamples);
            this.steps = steps;
    }

    @Override
    protected Object applyStep(int totalSteps,
                               int currentStep,
                               Function stepFunction,
                               Object[] seedsOrExamples,
                               Object previousValue) {

        if (seedsOrExamples[0] instanceof Spreader) {

            Spreader targetSpread = (Spreader) seedsOrExamples[0];
            return targetSpread.spread().collect(Collectors.toList());

        } else {

            Spread targetSpread = (Spread) seedsOrExamples[0];

            return new Spreader>()
                .factory(ArrayList::new)
                .mutators(list -> list.add(Spread.in(targetSpread)))
                .steps(this.getSteps())
                .spread()
                .collect(Collectors.toList()).get(0);

        }

    }

    @Override
    public  Spread step(Function stepFunction) {
        this.stepFunction = stepFunction;
        return new ListSpread<>(stepFunction, mapFunction, this.getSteps(), seedsOrExamples);
    }

    @Override
    public  Spread map(Function mapFunction) {
        this.mapFunction = mapFunction;
        return new ListSpread<>(stepFunction, mapFunction, this.getSteps(), seedsOrExamples);
    }

    public int getSteps() {
        return steps;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy