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> Fn1<T,Boolean>
accept()
Returns a type-safe version of the ConstObjBool.ACCEPT predicate.default void
accept(T t)
For compatibility with java.util.function.Consumer.static <T> Fn1<T,Boolean>
and(Iterable<Fn1<T,Boolean>> in)
Composes multiple predicates into a single predicate to potentially minimize trips through the source data.static <S> Fn1<S,Boolean>
and(Fn1<S,Boolean> a, Fn1<S,Boolean> b)
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.static <V> Fn1<V,V>
compose(Iterable<Fn1<V,V>> in)
Composes multiple functions into a single function to potentially minimize trips through the source data.default <S> Fn1<S,U>
compose(Fn1<? super S,? extends T> f)
static <V> Fn1<V,V>
identity()
static <A,B>
Fn1<A,B>memoize(Fn1<A,B> f)
Use only on pure functions with no side effects.static <S> Fn1<S,Boolean>
negate(Fn1<? super S,Boolean> a)
static <T> Fn1<T,Boolean>
or(Iterable<Fn1<T,Boolean>> in)
Composes multiple predicates into a single predicate to potentially minimize trips through the source data.static <S> Fn1<S,Boolean>
or(Fn1<S,Boolean> a, Fn1<S,Boolean> b)
static <T> Fn1<T,Boolean>
reject()
Returns a type-safe version of the ConstObjBool.REJECT predicate.
-
-
-
Method Detail
-
identity
static <V> Fn1<V,V> identity()
-
accept
static <T> Fn1<T,Boolean> accept()
Returns a type-safe version of the ConstObjBool.ACCEPT predicate.
-
reject
static <T> Fn1<T,Boolean> reject()
Returns a type-safe version of the ConstObjBool.REJECT predicate.
-
compose
static <V> Fn1<V,V> compose(Iterable<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 means IDENTITY.- Returns:
- a function which applies all the given functions in order.
-
and
static <T> Fn1<T,Boolean> and(Iterable<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 means ACCEPT.- Returns:
- a predicate which returns true if all input predicates return true, false otherwise.
-
or
static <T> Fn1<T,Boolean> or(Iterable<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 only accept()/ConstObjBool.ACCEPT and ConstObjBool.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 ConstObjBool.REJECT predicates are ignored. Any ACCEPT predicate will cause this entire method to return the ACCEPT predicate. No predicates means ConstObjBool.REJECT.- Returns:
- a predicate which returns true if any of the input predicates return true, false otherwise.
-
memoize
static <A,B> Fn1<A,B> memoize(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().
-
-