|
| 1 | +package com.jnape.palatable.lambda.functions.builtin.fn2; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.functions.Fn1; |
| 4 | +import com.jnape.palatable.lambda.functions.Fn2; |
| 5 | +import com.jnape.palatable.lambda.functions.Fn3; |
| 6 | +import com.jnape.palatable.lambda.functor.Bifunctor; |
| 7 | + |
| 8 | +import java.util.function.Consumer; |
| 9 | +import java.util.function.Function; |
| 10 | + |
| 11 | +/** |
| 12 | + * Given two {@link Consumer}s, "peek" at the values contained inside a {@link Bifunctor} via |
| 13 | + * {@link Bifunctor#biMap(Function, Function)}, applying the {@link Consumer}s to the contained values, |
| 14 | + * if there are any. |
| 15 | + * |
| 16 | + * @param <A> the bifunctor's first parameter type |
| 17 | + * @param <B> the bifunctor's second parameter type |
| 18 | + * @param <FAB> the bifunctor type |
| 19 | + */ |
| 20 | +public final class Peek2<A, B, FAB extends Bifunctor<A, B, ?>> implements Fn3<Consumer<? super A>, Consumer<? super B>, FAB, FAB> { |
| 21 | + private static final Peek2 INSTANCE = new Peek2<>(); |
| 22 | + |
| 23 | + private Peek2() { |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + @SuppressWarnings("unchecked") |
| 28 | + public FAB apply(Consumer<? super A> aConsumer, Consumer<? super B> bConsumer, FAB fab) { |
| 29 | + return (FAB) fab.biMap(a -> { |
| 30 | + aConsumer.accept(a); |
| 31 | + return a; |
| 32 | + }, b -> { |
| 33 | + bConsumer.accept(b); |
| 34 | + return b; |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + @SuppressWarnings("unchecked") |
| 39 | + public static <A, B, FAB extends Bifunctor<A, B, ?>> Peek2<A, B, FAB> peek2() { |
| 40 | + return INSTANCE; |
| 41 | + } |
| 42 | + |
| 43 | + public static <A, B, FAB extends Bifunctor<A, B, ?>> Fn2<Consumer<? super B>, FAB, FAB> peek2( |
| 44 | + Consumer<? super A> aConsumer) { |
| 45 | + return Peek2.<A, B, FAB>peek2().apply(aConsumer); |
| 46 | + } |
| 47 | + |
| 48 | + public static <A, B, FAB extends Bifunctor<A, B, ?>> Fn1<FAB, FAB> peek2(Consumer<? super A> aConsumer, |
| 49 | + Consumer<? super B> bConsumer) { |
| 50 | + return Peek2.<A, B, FAB>peek2(aConsumer).apply(bConsumer); |
| 51 | + } |
| 52 | + |
| 53 | + public static <A, B, FAB extends Bifunctor<A, B, ?>> FAB peek2(Consumer<? super A> aConsumer, |
| 54 | + Consumer<? super B> bConsumer, |
| 55 | + FAB fab) { |
| 56 | + return Peek2.<A, B, FAB>peek2(aConsumer, bConsumer).apply(fab); |
| 57 | + } |
| 58 | +} |
0 commit comments