Interface Fn1<T,​U>

  • All Superinterfaces:
    Consumer<T>, Function<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.
    • 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:
        
             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));
                     }
                 };
             }
        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()
        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.
        Specified by:
        apply in interface Function<T,​U>
      • accept

        default void accept​(T t)
        For compatibility with java.util.function.Consumer. Just a wrapper around apply().
        Specified by:
        accept in interface Consumer<T>
      • compose

        default <S> Fn1<S,​U> compose​(Fn1<? super S,​? extends T> f)