Package org.organicdesign.fp.function
Interface Fn1<T,U>
-
- All Known Implementing Classes:
Fn1.ConstObjBool
,Fn1.ConstObjObj
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface Fn1<T,U> extends Function<T,U>, Consumer<T>
This is like Java 8's java.util.function.Function, but retrofitted to turn checked exceptions into unchecked ones.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
Fn1.BooleanCombiner
static class
Fn1.ConstObjBool
Constant functions that take an Object and return a Booleanstatic class
Fn1.ConstObjObj
Constant functions that take an Object and return an Object
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <T> @NotNull Fn1<T,Boolean>
accept()
Returns a type-safe version of theFn1.ConstObjBool.ACCEPT
predicate.default void
accept(T t)
For compatibility with java.util.function.Consumer.static <S> @NotNull Fn1<S,Boolean>
and(@NotNull Fn1<S,Boolean> a, @NotNull Fn1<S,Boolean> b)
static <T> @NotNull Fn1<T,Boolean>
and(@Nullable Iterable<@Nullable Fn1<T,Boolean>> in)
Composes multiple predicates into a single predicate to potentially minimize trips through the source data.default U
apply(T t)
Call this convenience method so that you don't have to worry about checked exceptions.U
applyEx(T t)
Implement this one method, and you don't have to worry about checked exceptions.default <S> @NotNull Fn1<S,U>
compose(@NotNull Fn1<? super S,? extends T> f)
static <V> @NotNull Fn1<V,V>
compose(@Nullable Iterable<@Nullable Fn1<V,V>> in)
Composes multiple functions into a single function to potentially minimize trips through the source data.static <V> @NotNull Fn1<V,V>
identity()
static <A,B>
@NotNull Fn1<A,B>memoize(@NotNull Fn1<A,B> f)
Use only on pure functions with no side effects.static <S> @NotNull Fn1<S,Boolean>
negate(@NotNull Fn1<? super S,Boolean> a)
static <S> @NotNull Fn1<S,Boolean>
or(@NotNull Fn1<S,Boolean> a, @NotNull Fn1<S,Boolean> b)
static <T> @NotNull Fn1<T,Boolean>
or(@Nullable Iterable<@Nullable Fn1<T,Boolean>> in)
Composes multiple predicates into a single predicate to potentially minimize trips through the source data.static <T> @NotNull Fn1<T,Boolean>
reject()
Returns a type-safe version of theFn1.ConstObjBool.REJECT
predicate.
-
-
-
Method Detail
-
identity
@NotNull static <V> @NotNull Fn1<V,V> identity()
-
or
@NotNull static <S> @NotNull Fn1<S,Boolean> or(@NotNull @NotNull Fn1<S,Boolean> a, @NotNull @NotNull Fn1<S,Boolean> b)
-
and
@NotNull static <S> @NotNull Fn1<S,Boolean> and(@NotNull @NotNull Fn1<S,Boolean> a, @NotNull @NotNull Fn1<S,Boolean> b)
-
negate
@NotNull static <S> @NotNull Fn1<S,Boolean> negate(@NotNull @NotNull Fn1<? super S,Boolean> a)
-
accept
@NotNull static <T> @NotNull Fn1<T,Boolean> accept()
Returns a type-safe version of theFn1.ConstObjBool.ACCEPT
predicate.
-
reject
@NotNull static <T> @NotNull Fn1<T,Boolean> reject()
Returns a type-safe version of theFn1.ConstObjBool.REJECT
predicate.
-
compose
@NotNull static <V> @NotNull Fn1<V,V> compose(@Nullable @Nullable Iterable<@Nullable Fn1<V,V>> in)
Composes multiple functions into a single function to potentially minimize trips through the source data. The resultant function will loop through the functions for each item in the source. For a few functions and many source items, that takes less memory. Considers no function to mean the IDENTITY function. This decision is based on the way filters work and may or may not prove useful in practice. Please use the identity()/IDENTITY sentinel value in this abstract class since function comparison is done by reference. LIMITATION: You could have a function that maps from T to U then the next from U to V, the next from V to W and so on. So long as the output type of one matches up to the input type of the next, you're golden. But type safety curls up and dies when you try to detect the IDENTITY function at some point in the chain. For arbitrary chaining, it's best to roll your own. The following example shows how simple it is to chain two functions with an intermediate type into a single composite function:
Even with 2 arguments, there are several signatures that would work: imagine where A=B, B=C, or A=C. I just don't see the value to providing a bunch of chain2(), chain3() etc. functions that will ultimately not be type-safe and cannot perform optimizations for you, when you can roll your own type safe versions as you need them. Only the simplest case seems worth providing, along the lines of the and() helper function in Filter()public static <A,B,C> Fn1<A,C> chain2(final Fn1<A,B> f1, final Fn1<B,C> f2) { return new Fn1<A,C>() { @Override public C applyEx(A a) throws Exception { return f2.applyEx(f1.applyEx(a)); } }; }
- Type Parameters:
V
- the type of object to chain functions on- Parameters:
in
- the functions to applyEx in order. Nulls and IDENTITY functions are ignored. No functions: returns IDENTITY.- Returns:
- a function which applies all the given functions in order.
-
and
@NotNull static <T> @NotNull Fn1<T,Boolean> and(@Nullable @Nullable Iterable<@Nullable Fn1<T,Boolean>> in)
Composes multiple predicates into a single predicate to potentially minimize trips through the source data. The resultant predicate will loop through the predicates for each item in the source, but for few predicates and many source items, that takes less memory. Considers no predicate to mean "accept all." Use only accept()/ACCEPT and reject()/REJECT since function comparison is done by reference.- Type Parameters:
T
- the type of object to predicate on.- Parameters:
in
- the predicates to test in order. Nulls and ACCEPT predicates are ignored. Any REJECT predicate will cause this entire method to return a single REJECT predicate. No predicates: returns ACCEPT.- Returns:
- a predicate which returns true if all input predicates return true, false otherwise.
-
or
@NotNull static <T> @NotNull Fn1<T,Boolean> or(@Nullable @Nullable Iterable<@Nullable Fn1<T,Boolean>> in)
Composes multiple predicates into a single predicate to potentially minimize trips through the source data. The resultant predicate will loop through the predicates for each item in the source, but for few predicates and many source items, that takes less memory. Considers no predicate to mean "reject all." Use onlyaccept()
andreject()
since function comparison is done by reference.- Type Parameters:
T
- the type of object to predicate on.- Parameters:
in
- the predicates to test in order. Nulls and REJECT predicates are ignored. Any ACCEPT predicate will cause this entire method to return the ACCEPT predicate. No predicates: returns REJECT.- Returns:
- a predicate which returns true if any of the input predicates return true, false otherwise.
-
memoize
@NotNull static <A,B> @NotNull Fn1<A,B> memoize(@NotNull @NotNull Fn1<A,B> f)
Use only on pure functions with no side effects. Wrap an expensive function with this and for each input value, the output will only be computed once. Subsequent calls with the same input will return identical output very quickly. Please note that the return values from f need to implement equals() and hashCode() correctly for this to work correctly and quickly.
-
applyEx
U applyEx(T t) throws Exception
Implement this one method, and you don't have to worry about checked exceptions.- Throws:
Exception
-
apply
default U apply(T t)
Call this convenience method so that you don't have to worry about checked exceptions.
-
accept
default void accept(T t)
For compatibility with java.util.function.Consumer. Just a wrapper around apply().
-
-