Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.teststeps.thekla4j.utils.vavr.LiftEither Maven / Gradle / Ivy
package com.teststeps.thekla4j.utils.vavr;
import io.vavr.*;
import io.vavr.collection.HashMap;
import io.vavr.collection.List;
import io.vavr.control.Either;
import io.vavr.control.Option;
import io.vavr.control.Try;
public class LiftEither {
/**
* transforming an Either{L,R} into a Try{R}
*
* @param either Either{L,R}
* @return Either{L,R}
*/
public static Try toTry(Either either) {
return either.isLeft() ? Try.failure((Throwable) either.getLeft()) : Try.success(either.get());
}
/**
* transforming an Option{R} into an Either{L,R}
*
* @param errorFunction creating the error object of type L
* @return Either{L,R}
*/
public static Function1, Either> fromOption(Function0 errorFunction) {
return option -> option.isEmpty() ? Either.left(errorFunction.apply()) :
Either.right(option.get());
}
/**
* returns a function that transforms a Tuple1{Either{T}} to Either{Tuple1{T}}
*
* @param
* @return an Either lifted from the Tuple
*/
public static Function1>, Either>> fromTuple1() {
return tuple1 -> tuple1.apply(toTuple1());
}
public static Function1, Either>> toTuple1() {
return u1 -> u1.map(Tuple::of);
}
/**
* returns a function that transforms a Tuple2{Either{T}, U} to Either{Tuple2{T,U}}
*
* @param
* @param
* @return
*/
public static Function1, U>, Either>> fromTuple2$1() {
return tuple2 -> tuple2.apply(toTuple2$1());
}
public static Function2, U2, Either>> toTuple2$1() {
return (t1, u2) -> t1.map(u1 -> Tuple.of(u1, u2));
}
/**
* returns a function that transforms a Tuple2{T, Either{U}} to Either{Tuple2{T,U}}
*
* @param
* @param
* @return
*/
public static Function1>, Either>> fromTuple2$2() {
return tuple2 -> tuple2.apply(toTuple2$2());
}
public static Function2, Either>> toTuple2$2() {
return (u1, t2) -> t2.map(u2 -> Tuple.of(u1, u2));
}
/**
* returns a function that transforms a Tuple2{Either{T}, Either{U}} to Either{Tuple2{T,U}}
*
* @param
* @param
* @return
*/
public static Function1, Either>, Either extends Throwable, Tuple2>> fromTuple2() {
return tuple2 -> tuple2.apply(toTuple2());
}
public static Function2<
Either,
Either,
Either>> toTuple2() {
return (t1, t2) -> t1.flatMap(u1 -> t2.map(u2 -> Tuple.of(u1, u2)));
}
/**
* returns a function that transforms a Tuple3{Either{T}, Either{U}, Either{V}} to Either{Tuple3{T,U,V}}
*
* @param
* @param
* @return
*/
public static Function1,
Either,
Either>,
Either>> fromTuple3() {
return tuple3 -> tuple3.apply(toTuple3());
}
public static Function3<
Either,
Either,
Either,
Either>> 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{Either{T}, U, V} to Either{Tuple3{T,U,V}}
*/
public static
Function1, U, V>, Either>> fromTuple3$1() {
return tup3 -> tup3.apply(toTuple3$1());
}
public static Function3<
Either, U2, U3,
Either>> toTuple3$1() {
return (et1, t2, t3) -> et1.map(u1 -> Tuple.of(u1, t2, t3));
}
/**
* returns a function that transforms a Tuple3{T, Either{U}, V} to Either{Tuple3{T,U,V}}
*/
public static
Function1, V>, Either>> fromTuple3$2() {
return tup3 -> tup3.apply(toTuple3$2());
}
public static Function3<
U1, Either, U3,
Either>> toTuple3$2() {
return (t1, et2, t3) -> et2.map(u2 -> Tuple.of(t1, u2, t3));
}
/**
* returns a function that transforms a Tuple3{T, U, Either{V)} to Either{Tuple3{T,U,V}}
*/
public static
Function1>, Either>> fromTuple3$3() {
return tup3 -> tup3.apply(toTuple3$3());
}
public static Function3<
U1, U2, Either,
Either>> toTuple3$3() {
return (t1, t2, et3) -> et3.map(u3 -> Tuple.of(t1, t2, u3));
}
/**
* returns a function that transforms a Tuple4{Either{T}, Either{U}, Either{V}, Either{W}} to Either{Tuple4{T,U,V, W}}
*
* @param
* @param
* @return
*/
public static Function1<
Tuple4, Either, Either, Either>,
Either>> fromTuple4() {
return tuple4 -> tuple4.apply(toTuple4());
}
public static Function4<
Either,
Either,
Either,
Either,
Either>> toTuple4() {
return (t1, t2, t3, t4) -> t1.flatMap(u1 -> t2.flatMap(u2 -> t3.flatMap(u3 -> t4.map(u4 -> Tuple.of(u1, u2, u3, u4)))));
}
/**
* returns a function that transforms a Tuple5{Either{T}, Either{U}, Either{V}, Either{W}, Either{X}} to Either{Tuple5{T,U,V, W, X}}
*
* @param
* @param
* @return
*/
public static Function1<
Tuple5, Either, Either, Either, Either>,
Either>> fromTuple5() {
return tuple5 -> tuple5.apply(toTuple5());
}
public static Function5<
Either,
Either,
Either,
Either,
Either,
Either>> toTuple5() {
return (t1, t2, t3, t4, t5) -> t1.flatMap(u1 -> t2.flatMap(u2 -> t3.flatMap(u3 -> t4.flatMap(u4 -> t5.map(u5 -> Tuple.of(u1, u2, u3, u4, u5))))));
}
/**
* returns a function that transforms a HashMap{T, Either{Throwable, U}} to Either{Throwable, HashMap{T, U}}
*
* @param
* @param
* @return Either{
*/
public static Function1>, Either>> fromHashMap() {
return hashMapE -> hashMapE.foldLeft(
Either.>right(HashMap.empty()),
(theEither, entry) -> theEither.flatMap(newMap -> entry._2.map(value -> newMap.put(entry._1, value)))
);
}
public static Function1>, Either>> fromList() {
return list -> list.foldLeft(
Either.>right(List.empty()),
(theEither, entry) -> theEither.flatMap(newList -> entry.map(newList::append))
);
}
public static Function2>, Either, Either>> toList() {
return (acc, element) -> acc.flatMap(list -> element.map(list::append));
}
}