Package org.n52.janmayen.function
Class Functions
- java.lang.Object
-
- org.n52.janmayen.function.Functions
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,U extends T>
Function<T,U>cast(Class<? extends U> clazz)Returns aFunctionthat casts it's input argument to any type.static <T,U extends T>
Ucast(T t)Casts it's input argument to any type.static <T,U extends T>
Function<T,Optional<U>>castIfInstanceOf(Class<? extends U> clazz)Returns aFunctionthat casts it'S input argument to the specified type if it's the right type.static <T,U>
Function<T,U>constant(U u)Creates a function returning the same result for each invocation.static <T,R>
Supplier<R>curry(Function<? super T,? extends R> function, T t)Curries the parameter of theFunctionand creates aConsumer.static <T1,T2,R>
Function<T2,R>curryFirst(BiFunction<T1,T2,R> bifunction, T1 t1)Curries the first parameter of theBiFunctionand creates aFunction.static <T1,T2,R>
Function<T1,R>currySecond(BiFunction<T1,T2,R> bifunction, T2 t2)Curries the second parameter of theBiFunctionand creates aFunction.static <S,T,X extends Exception>
Function<S,T>errorWrapper(ThrowingFunction<S,T,X> fun)Wraps anThrowingFunctioninto anFunctionthat throws anErrorif the function throws anException.static <T,U>
Function<T,U>forSupplier(Supplier<U> supplier)Create a function for the the supplier, ignoring any supplied input.static <K,V>
Function<Map<K,V>,Set<K>>keySetWhereValues(Predicate<? super V> predicate)static <K,V>
Function<Map<K,V>,Stream<K>>keyStreamWhereValues(Predicate<? super V> predicate)static <T> BinaryOperator<T>mergeLeft(BiConsumer<T,T> merger)static <T> BinaryOperator<T>mergeRight(BiConsumer<T,T> merger)static <K,V>
BinaryOperator<Map<K,V>>mergeToLeftMap(BinaryOperator<V> valueMerger)static <K,V>
BinaryOperator<Map<K,V>>mergeToRightMap(BinaryOperator<V> valueMerger)static <T> Function<T,T>mutate(Consumer<? super T> action)
-
-
-
Method Detail
-
cast
public static <T,U extends T> U cast(T t) throws ClassCastExceptionCasts 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 aFunctionthat 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:
cast(java.lang.Object)
-
castIfInstanceOf
public static <T,U extends T> Function<T,Optional<U>> castIfInstanceOf(@Nonnull Class<? extends U> clazz)
Returns aFunctionthat casts it'S input argument to the specified type if it's the right type. The returnedOptionaleither 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
Optionalcontaining 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 functionU- 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 functionU- 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 theFunctionand creates aConsumer.- Type Parameters:
T- the parameter typeR- the return type- Parameters:
function- the functiont- 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 theBiFunctionand creates aFunction.- Type Parameters:
T1- the first parameter typeT2- the second parameter typeR- the return type- Parameters:
bifunction- the functiont1- 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 theBiFunctionand creates aFunction.- Type Parameters:
T1- the first parameter typeT2- the second parameter typeR- the return type- Parameters:
bifunction- the functiont2- 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)
- 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 anThrowingFunctioninto anFunctionthat throws anErrorif the function throws anException.- 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.
-
-