Package at.jku.ssw.fp.util
Class When
- java.lang.Object
-
- at.jku.ssw.fp.util.When
-
public class When extends Object
Class with static methods implementing some methods functioning like conditional statements.- Author:
- Herbert Praehofer
-
-
Constructor Summary
Constructors Constructor Description When()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,R>
TypeTestFunction<T,R>is(Class<T> clazz, Function<? super T,? extends R> func)
Creates a typetest function from a class and a function.static <T,R>
CondFunction<T,R>is(Predicate<? super T> pred, Function<? super T,? extends R> func)
Creates a conditional function from a predicate and a function.static <T,R>
CondFunction<T,R>otherwise(Function<? super T,? extends R> func)
Creates a conditional function from a function and with a test predicate always returning truestatic <R> R
when(Object o, TypeTestFunction<?,? extends R>... ttFns)
Methods applying typetest functions on a given value.static <T,R>
Rwhen(T value, CondFunction<? super T,? extends R>... cases)
Methods applying conditional functions on a given value.
-
-
-
Method Detail
-
when
@SafeVarargs public static <T,R> R when(T value, CondFunction<? super T,? extends R>... cases)
Methods applying conditional functions on a given value. The first conditional function which is applicable is applied and the result is returned. Throws aNoSuchElementException
if none of the conditional functions is applicable.- Type Parameters:
T
- the type of the argumentR
- the type of the return value- Parameters:
value
- the value to applycases
- the conditional functions to test and apply- Returns:
- the result value of the conditional application
-
is
public static <T,R> CondFunction<T,R> is(Predicate<? super T> pred, Function<? super T,? extends R> func)
Creates a conditional function from a predicate and a function.- Type Parameters:
T
- the type of the argumentR
- the type of the return value of the function- Parameters:
pred
- the predicatefunc
- the function- Returns:
- the constructed conditional function
-
otherwise
public static <T,R> CondFunction<T,R> otherwise(Function<? super T,? extends R> func)
Creates a conditional function from a function and with a test predicate always returning true- Type Parameters:
T
- the type of the argumentR
- the type of the return value of the function- Parameters:
func
- the function- Returns:
- the constructed conditional function
-
when
@SafeVarargs public static <R> R when(Object o, TypeTestFunction<?,? extends R>... ttFns)
Methods applying typetest functions on a given value. The first typetest function which is applicable is applied and the result is returned. Throws aNoSuchElementException
if none of the conditional functions is applicable.- Type Parameters:
R
- the type of the return value- Parameters:
o
- the value to applyttFns
- the typetest functions- Returns:
- the result value of the application of the first applicable typtest function
-
is
public static <T,R> TypeTestFunction<T,R> is(Class<T> clazz, Function<? super T,? extends R> func)
Creates a typetest function from a class and a function.- Type Parameters:
T
- the type of the argumentR
- the type of the return value of the function- Parameters:
clazz
- the class for the typetestfunc
- the function- Returns:
- the constructed typetest function
-
-