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

com.unit16.z.function.FluentConsumer Maven / Gradle / Ivy

package com.unit16.z.function;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class FluentConsumer implements Consumer {

    private final Consumer wrapped;

    private FluentConsumer(Consumer w) { wrapped = w; }

    public static 

FluentConsumer

on(Consumer w) { return new FluentConsumer

(w); } @Override public final void accept(T t) { wrapped.accept(t); } public final FluentConsumer filter(final Predicate pred) { return new Filters<>(pred, wrapped); } public final FluentConsumer map(final Function func) { return new MapsTo<>(func, wrapped); } private static final class Filters extends FluentConsumer { private Filters(Predicate pred, Consumer ct) { super(new Consumer(){ @Override public void accept(S t) { if (pred.test(t)) ct.accept(t); }}); } } private static final class MapsTo extends FluentConsumer { private MapsTo(Function map, Consumer ct) { super(new Consumer(){ @Override public void accept(S t) { ct.accept(map.apply(t)); }}); } } }