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

com.jongsoft.lang.collection.support.PipeCommand Maven / Gradle / Ivy

The newest version!
package com.jongsoft.lang.collection.support;

import java.util.Iterator;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

import com.jongsoft.lang.collection.Collection;
import com.jongsoft.lang.collection.Pipeline;

public class PipeCommand implements Pipeline {

    private final Supplier> command;

    public PipeCommand(Collection origin) {
        this.command = () -> origin;
    }

    private PipeCommand(Supplier> command) {
        this.command = command;
    }

    @Override
    public  Pipeline map(Function mapper) {
        return new PipeCommand<>(() -> command.get().map(mapper));
    }

    @Override
    public Pipeline filter(Predicate predicate) {
        return new PipeCommand<>(() -> command.get().filter(predicate));
    }

    @Override
    public Pipeline reject(Predicate predicate) {
        return new PipeCommand<>(() -> command.get().reject(predicate));
    }

    @Override
    public Stream stream() {
        return command.get().stream();
    }

    @Override
    public Iterator iterator() {
        return command.get().iterator();
    }

    @Override
    public  U foldLeft(U start, BiFunction combiner) {
        return command.get().foldLeft(start, combiner);
    }

    @Override
    public  U foldRight(U start, BiFunction combiner) {
        return command.get().foldRight(start, combiner);
    }

    @Override
    public T reduceLeft(BiFunction reducer) {
        return command.get().reduceLeft(reducer);
    }

    @Override
    public void consume(final Consumer consumer) {
        command.get().forEach(consumer);
    }

}