Class Functions

java.lang.Object
org.n52.janmayen.function.Functions

public final class Functions extends Object
Utility functions for Function.
  • Method Details

    • cast

      public static <T, U extends T> U cast(T t) throws ClassCastException
      Casts it's input argument to any type.
      Type Parameters:
      T - The input type.
      U - The output type.
      Parameters:
      t - The object to be casted.
      Returns:
      The casted object.
      Throws:
      ClassCastException - if the object can not be casted.
    • cast

      public static <T, U extends T> Function<T,U> cast(@Nonnull Class<? extends U> clazz)
      Returns a Function that casts it's input argument to any type.
      Type Parameters:
      T - The input type.
      U - The output type.
      Parameters:
      clazz - The class the input should be casted to.
      Returns:
      The casting function.
      See Also:
    • castIfInstanceOf

      public static <T, U extends T> Function<T,Optional<U>> castIfInstanceOf(@Nonnull Class<? extends U> clazz)
      Returns a Function that casts it'S input argument to the specified type if it's the right type. The returned Optional either contains the casted input arguemnt or is empty.
      Type Parameters:
      T - The input type.
      U - The output type.
      Parameters:
      clazz - The class to
      Returns:
      An Optional containing the input
    • forSupplier

      public static <T, U> Function<T,U> forSupplier(@Nonnull Supplier<U> supplier)
      Create a function for the the supplier, ignoring any supplied input.
      Type Parameters:
      T - the type of the input to the function
      U - the type of the result to the function
      Parameters:
      supplier - the supplier
      Returns:
      the function wrapping the supplier
    • constant

      public static <T, U> Function<T,U> constant(@Nullable U u)
      Creates a function returning the same result for each invocation.
      Type Parameters:
      T - the type of the input to the function
      U - the type of the result to the function
      Parameters:
      u - the constant result
      Returns:
      the function
    • curry

      public static <T, R> Supplier<R> curry(@Nonnull Function<? super T,? extends R> function, T t)
      Curries the parameter of the Function and creates a Consumer.
      Type Parameters:
      T - the parameter type
      R - the return type
      Parameters:
      function - the function
      t - the curried parameter
      Returns:
      the curried function
    • curryFirst

      public static <T1, T2, R> Function<T2,R> curryFirst(@Nonnull BiFunction<T1,T2,R> bifunction, T1 t1)
      Curries the first parameter of the BiFunction and creates a Function.
      Type Parameters:
      T1 - the first parameter type
      T2 - the second parameter type
      R - the return type
      Parameters:
      bifunction - the function
      t1 - the curried parameter
      Returns:
      the curried function
    • currySecond

      public static <T1, T2, R> Function<T1,R> currySecond(@Nonnull BiFunction<T1,T2,R> bifunction, T2 t2)
      Curries the second parameter of the BiFunction and creates a Function.
      Type Parameters:
      T1 - the first parameter type
      T2 - the second parameter type
      R - the return type
      Parameters:
      bifunction - the function
      t2 - the curried parameter
      Returns:
      the curried function
    • mergeLeft

      public static <T> BinaryOperator<T> mergeLeft(@Nonnull BiConsumer<T,T> merger)
    • mergeRight

      public static <T> BinaryOperator<T> mergeRight(@Nonnull BiConsumer<T,T> merger)
    • mergeToLeftMap

      public static <K, V> BinaryOperator<Map<K,V>> mergeToLeftMap(@Nonnull BinaryOperator<V> valueMerger)
    • mergeToRightMap

      public static <K, V> BinaryOperator<Map<K,V>> mergeToRightMap(@Nonnull BinaryOperator<V> valueMerger)
    • keySetWhereValues

      public static <K, V> Function<Map<K,V>,Set<K>> keySetWhereValues(Predicate<? super V> predicate)
    • keyStreamWhereValues

      public static <K, V> Function<Map<K,V>,Stream<K>> keyStreamWhereValues(Predicate<? super V> predicate)
    • mutate

      public static <T> Function<T,T> mutate(@Nonnull Consumer<? super T> action)
      Wrapps a Consumer into a Function that returns it's input argument.
      Type Parameters:
      T - The consumed element's type.
      Parameters:
      action - The consumer.
      Returns:
      A function that applies the consumer and returns it's input.
    • errorWrapper

      public static <S, T, X extends Exception> Function<S,T> errorWrapper(@Nonnull ThrowingFunction<S,T,X> fun)
      Wraps an ThrowingFunction into an Function that throws an Error if the function throws an Exception.
      Type Parameters:
      S - the function's input type.
      T - the function's output type.
      X - the type of the the exception that might be thrown.
      Parameters:
      fun - the function.
      Returns:
      the wrapped function.