Package at.jku.ssw.fp.util
Interface CondFunction<T,R>
-
- Type Parameters:
T
- the type of the argumentR
- the type of the wrapped return value
- All Known Implementing Classes:
TypeTestFunction
public interface CondFunction<T,R> extends Function<T,Optional<R>>
Interface for conditional functions. Conditional functions test argument values before returning the result. A conditional function returns anOptional
. If the argument passes the test, the value is mapped to a result and the result is wrapped into theOptional
. But if the test is not successful,Optional.empty()
is returned.- Author:
- Herbert Praehofer
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <T,R>
CondFunction<T,R>of(Predicate<? super T> pred, Function<? super T,? extends R> func)
Constructs a conditional function from a predicate and a function argument.default CondFunction<T,R>
orElse(Function<? super T,? extends R> func)
Returns a combined conditional function.default CondFunction<T,R>
orElseIf(Predicate<? super T> pred, Function<? super T,? extends R> func)
Returns a combined conditional function.boolean
test(T t)
Tests an argument value if applicable.
-
-
-
Method Detail
-
test
boolean test(T t)
Tests an argument value if applicable.- Parameters:
t
- the value to test- Returns:
- the Boolean result of the test
-
of
static <T,R> CondFunction<T,R> of(Predicate<? super T> pred, Function<? super T,? extends R> func)
Constructs a conditional function from a predicate and a function argument. The resulting conditional function first uses the predicate to test the argument value. If successful, the function is applied and the resulting value is wrapped into anOptional
. OtherwiseOptional.empty()
is returned.- Type Parameters:
T
- the type of the argumentR
- the type of the wrapped return value- Parameters:
pred
- the predicatefunc
- the function- Returns:
- the conditional function
-
orElseIf
default CondFunction<T,R> orElseIf(Predicate<? super T> pred, Function<? super T,? extends R> func)
Returns a combined conditional function. The returned conditional function first tries to apply this conditional function, and if not successful will try to test the value with the given predicate and apply the given function.- Parameters:
pred
- the predicate to test the argumentfunc
- the function to apply on the argument- Returns:
- the combined conditional function
-
orElse
default CondFunction<T,R> orElse(Function<? super T,? extends R> func)
Returns a combined conditional function. The returned conditional function first tries to apply this conditional function, and if not successful will alternatively apply the given function.- Parameters:
func
- the function to apply on the argument as an alternative- Returns:
- the combined conditional function
-
-