Grundbegriffe im Framework
|
Option
|
(serialisierbar) Kapselung optionaler Werte, Vermeidung von Nullchecks (Some oder None)
|
|
Try
|
Erfolg oder Misserfolg, Ausführung für success oder recover möglich
|
|
Either
|
Links oder Rechts
|
|
Function0...Function8
|
Funktionen mit 0 bis 8 Parametern vgl. Supplier
|
|
Tupel
|
Tupel mit bis zu 8 Parametern möglich
|
Function2<Integer, Integer, Integer> divide=(a,b)->a/b;
Function2<Integer, Integer, Option<Integer>> safeDivide = Function2.lift(divide);
Function0<Double> hashCache=Function0.of(Math::random).memoized();
double randomValue1 = hashCache.apply();
double randomValue2 = hashCache.apply();
then(randomValue1).isEqualTo(randomValue2);
String helloWorld = Option.of("Hello")
.map( value -> value + "world")
.getOrElse(()-> "Recover");
Tuple2<String, Integer> java8 = Tuple.of("Java","8");
Tuple2<String, Integer> vavr2 = Java8.map(
s -> s + "slang",
i -> i / 4
);
String first = vavr2._1;
int second = vavr2._2;
Lazy<Double> lazy=Lazy.of(Math::random);
lazy.isEvaluated(); // false
lazy.get() // 0.123 (random)
lazy.isEvaluated(); // true
lazy.get() // 0.123 (cached)
// Serviceinterface mit Checked exceptions
public interface HelloWorldService{
String sayHelloWorld(String name) throws BusinessException;
}
public class HelloWorldService{
Try<String> sayHelloWorld( String name){
return Try.of(()->backendDao.sayHelloWorld(input));
}
}
String result = helloWorldService.sayHelloWorld("Robert")
.map(value -> value + " and all readers")
.recover(throwable -> "Handle exception and recover")
.get();
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.