package com.teststeps.thekla4j.utils.vavr;
import io.vavr.*;
import io.vavr.collection.HashMap;
import io.vavr.collection.List;
import io.vavr.collection.Map;
import io.vavr.collection.Seq;
import io.vavr.control.Option;
import io.vavr.control.Try;
public class LiftTry {
/**
* returns a function that lifts the Try from an Option
*
* Option{Try{T}} -> Try{Option{T}}
*
* @return the function that lifts the Try from an Option
*/
public static Function1>, Try >> fromOption() {
return option -> option.isEmpty() ? Try.success(Option.none()) : option.get().map(Option::some);
}
/**
* returns a function that lifts the trys from a tuple
*
* Tuple2{Try{T}, Try{U}} -> Try{Tuple{T,U}}
*
* @return the function that transforms the tuple
*/
public static Function1, Try>, Try>> fromTuple2() {
return tuple2 -> tuple2._1().map(t -> Tuple.of(t, tuple2._2))
.flatMap(t -> tuple2._2.map(u -> Tuple.of(t._1, u)));
}
/**
* returns a function that transforms two
* Try{U1} and Try{U2}
* to
* Try{Tuple{U1,U2}}
*
* @return the function that transforms into a tuple
*/
public static Function2, Try, Try>> toTuple2() {
return (t1, t2) -> t1.flatMap((u1) -> t2.map((u2) -> Tuple.of(u1, u2)));
}
/**
* returns a function that transforms a
* Tuple2{Try{T}, U}
* to
* Try{Tuple{T,U}}
*
* @return the function that transforms the tuple
*/
public static Function1, U>, Try>> fromTuple2$1() {
return tuple2 -> tuple2._1().map(t -> Tuple.of(t, tuple2._2));
}
/**
* returns a function that transforms a
* Tuple2{T, Try{U}}
* to
* Try{Tuple{T,U}}
*
* @param
* @param
* @return
*/
public static Function1>, Try>> fromTuple2$2() {
return tuple2 -> tuple2._2.map(u -> Tuple.of(tuple2._1, u));
}
public static Function2, Try>> fromInnerTuple2$2() {
return (t1, t2) -> t2.map(u -> Tuple.of(t1, u));
}
/**
* returns a function that transforms a
* Tuple3{Try{T}, Try{U}, Try{V}}
* to
* Try{Tuple{T,U,V}}
*
* @param
* @param
* @param
* @return
*/
public static Function1, Try, Try>, Try>> fromTuple3() {
return tuple3 -> tuple3._1().map(t -> Tuple.of(t, tuple3._2, tuple3._3))
.flatMap(t -> tuple3._2.map(u -> Tuple.of(t._1, u, t._3)))
.flatMap(t -> tuple3._3.map(v -> Tuple.of(t._1, t._2, v)));
}
public static Function3, Try, Try, Try>> toTuple3() {
return (t1, t2, t3) -> t1.flatMap(u1 -> t2.flatMap(u2 -> t3.map(u3 -> Tuple.of(u1, u2, u3))));
}
/**
* returns a function that transforms a
* Tuple3{Try{T}, U, V}
* to
* Try{Tuple{T,U,V}}
*
* @param
* @param
* @param
* @return
*/
public static Function1, U, V>, Try>> fromTuple3$1() {
return tuple3 -> tuple3._1().map(t -> Tuple.of(t, tuple3._2, tuple3._3));
}
/**
* returns a function that transforms a
* Tuple3{T, Try{U}, V}
* to
* Try{Tuple{T,U,V}}
*
* @param
* @param
* @param
* @return
*/
public static Function1, V>, Try>> fromTuple3$2() {
return tuple3 -> tuple3._2().map(t -> Tuple.of(tuple3._1, t, tuple3._3));
}
/**
* returns a function that transforms a
* Tuple3{T, U, Try{V}}
* to
* Try{Tuple{T,U,V}}
*
* @param
* @param
* @param
* @return
*/
public static Function1>, Try>> fromTuple3$3() {
return tuple3 -> tuple3._3().map(t -> Tuple.of(tuple3._1, tuple3._2, t));
}
/**
* returns a function that transforms a
* Tuple3{T, Try{U}, Try{V}}
* to
* Try{Tuple{T,U,V}}
*
* @param
* @param
* @param
* @return
*/
public static Function1, Try>, Try>> fromTuple3$2$3() {
return tuple3 -> tuple3._2()
.map(t -> Tuple.of(tuple3._1, t, tuple3._3))
.flatMap(t -> tuple3._3.map(v -> Tuple.of(t._1, t._2, v)));
}
/**
* returns a function that transforms a
* Tuple4{Try{T}, U, V, W}
* to
* Try{Tuple4{T,U,V,W}}
*/
public static Function1, U, V, W>, Try>> fromTuple4$1() {
return tuple4 -> tuple4._1().map(t -> Tuple.of(t, tuple4._2, tuple4._3, tuple4._4));
}
/**
* returns a function that transforms a
* Tuple4{T, U, Try{V}, W}
* to
* Try{Tuple4{T,U,V,W}}
*/
public static Function1, W>, Try>> fromTuple4$3() {
return tuple4 -> tuple4._3().map(v -> Tuple.of(tuple4._1, tuple4._2, v, tuple4._4));
}
/**
* returns a function that transforms a
* Tuple4{Try{T}, U, Try{V}, W}
* to
* Try{Tuple4{T,U,V,W}}
*/
public static Function1, U, Try, W>, Try>> fromTuple4$1$3() {
return tuple4 -> LiftTry., W>fromTuple4$1().apply(tuple4)
.flatMap(LiftTry.fromTuple4$3());
}
public static Function1>, Try>> fromHashMapValue() {
return hashMap -> hashMap.foldLeft(
Try.success(HashMap.empty()),
(theTry, entry) -> theTry.flatMap(newMap -> entry._2.map(value -> newMap.put(entry._1, value))));
}
public static Function1, T>, Try>> fromHashMapKey() {
return hashMap -> hashMap.foldLeft(
Try.success(HashMap.empty()),
(theTry, entry) -> theTry.flatMap(newMap -> entry._1.map(key -> newMap.put(key, entry._2))));
}
public static Function1, Try>, Try>> fromHashMap() {
return hashMap -> hashMap.foldLeft(
Try.success(HashMap.empty()),
(theTry, entry) -> theTry.flatMap(accMap -> entry._1.flatMap(key -> entry._2.map(value -> accMap.put(key, value)))));
}
public static Function1>, Try>> fromMap() {
return map -> map.foldLeft(
// have to remove all entries from the map as I don't know which type of map it is (e.g. HashMap, TreeMap, ...)
// values mapping has to be done so that the return types match, it has no effect as the map is already empty
Try.success(map.removeAll(map.keySet()).mapValues(Try::get)),
(theTry, entry) -> theTry.flatMap(newMap -> entry._2.map(value -> newMap.put(entry._1, value))));
}
public static Function1>, Try>> fromList() {
return list -> list.foldLeft(
Try.success(List.empty()), LiftTry.fromListMapper());
}
public static Function2>, Try, Try>> fromListMapper() {
return (theTry, tryElement) -> theTry.flatMap(newList -> tryElement.map(newList::append));
}
public static Function1>, Try>> fromSeq() {
return seq -> seq.foldLeft(
Try.success(List.empty()),
(theTry, entry) -> theTry.flatMap(newList -> entry.map(newList::append)));
}
}