com.annimon.stream.function.Function Maven / Gradle / Ivy
Show all versions of stream Show documentation
package com.annimon.stream.function;
/**
* Represents a function which produces result from input arguments.
*
* @param the type of the input of the function
* @param the type of the result of the function
*/
@FunctionalInterface
public interface Function {
/**
* Applies this function to the given argument.
*
* @param t an argument
* @return the function result
*/
R apply(T t);
class Util {
private Util() { }
/**
* Composes {@code Function} calls.
*
* {@code f1.apply(f2.apply(v)) }
*
* @param the type of the input argument of first function
* @param the type of the result of {@code f2} and input argument of {@code f1}
* @param the type of the result of composed function {@code f1}
* @param f1 the function for transform {@code Function f2} result to the type {@code R}
* @param f2 the {@code Function} which is called first
* @return the result of composed function
* @throws NullPointerException if {@code f1} or {@code f2} or result of {@code Function f1} is null
* @see #andThen(com.annimon.stream.function.Function, com.annimon.stream.function.Function)
*/
public static Function compose(
final Function super T, ? extends R> f1,
final Function super V, ? extends T> f2) {
return Util.andThen(f2, f1);
}
/**
* Composes {@code Function} calls.
*
* {@code f2.apply(f1.apply(t)) }
*
* @param the type of the input argument of first function
* @param the type of the result of {@code f1} and input argument of {@code f2}
* @param the type of the result of composed function {@code f2}
* @param f1 the {@code Function} which is called first
* @param f2 the function for transform {@code Function f1} result to the type {@code V}
* @return the result of composed function
* @throws NullPointerException if {@code f1} or {@code f2} or result of {@code Function f1} is null
* @see #compose(com.annimon.stream.function.Function, com.annimon.stream.function.Function)
*/
public static Function andThen(
final Function super T, ? extends R> f1,
final Function super R, ? extends V> f2) {
return new Function() {
@Override
public V apply(T t) {
return f2.apply(f1.apply(t));
}
};
}
/**
* Creates a safe {@code Function},
*
* @param the type of the input of the function
* @param the type of the result of the function
* @param throwableFunction the function that may throw an exception
* @return the function result or {@code null} if exception was thrown
* @throws NullPointerException if {@code throwableFunction} is null
* @see #safe(com.annimon.stream.function.ThrowableFunction, java.lang.Object)
*/
public static Function safe(
ThrowableFunction super T, ? extends R, Throwable> throwableFunction) {
return Util.safe(throwableFunction, null);
}
/**
* Creates a safe {@code Function},
*
* @param the type of the input of the function
* @param the type of the result of the function
* @param throwableFunction the function that may throw an exception
* @param resultIfFailed the result which returned if exception was thrown
* @return the function result or {@code resultIfFailed}
* @throws NullPointerException if {@code throwableFunction} is null
* @see #safe(com.annimon.stream.function.ThrowableFunction)
*/
public static Function safe(
final ThrowableFunction super T, ? extends R, Throwable> throwableFunction,
final R resultIfFailed) {
return new Function() {
@Override
public R apply(T value) {
try {
return throwableFunction.apply(value);
} catch (Throwable throwable) {
return resultIfFailed;
}
}
};
}
}
}