diff --git a/src/main/java/io/github/joselion/maybe/EffectHandler.java b/src/main/java/io/github/joselion/maybe/EffectHandler.java index f980e7a..593d9fa 100644 --- a/src/main/java/io/github/joselion/maybe/EffectHandler.java +++ b/src/main/java/io/github/joselion/maybe/EffectHandler.java @@ -82,7 +82,7 @@ public EffectHandler doOnSuccess(final Runnable effect) { * @param effect a consumer function that recieves the caught error * @return the same handler to continue chainning operations */ - public EffectHandler doOnError(final Class ofType, final Consumer effect) { + public EffectHandler doOnError(final Class ofType, final Consumer effect) { this.error .filter(ofType::isInstance) .map(ofType::cast) @@ -98,7 +98,7 @@ public EffectHandler doOnError(final Class ofType, f * @param effect a consumer function that recieves the caught error * @return the same handler to continue chainning operations */ - public EffectHandler doOnError(final Consumer effect) { + public EffectHandler doOnError(final Consumer effect) { this.error.ifPresent(effect); return this; @@ -116,7 +116,7 @@ public EffectHandler doOnError(final Consumer effect) { * @return an empty handler if an error instance of the provided type was * caught. The same handler instance otherwise */ - public EffectHandler catchError(final Class ofType, final Consumer handler) { + public EffectHandler catchError(final Class ofType, final Consumer handler) { return this.error .filter(ofType::isInstance) .map(ofType::cast) @@ -137,7 +137,7 @@ public EffectHandler catchError(final Class ofType, final Co * @return an empty handler if the error is present. The same handler * instance otherwise */ - public EffectHandler catchError(final Consumer handler) { + public EffectHandler catchError(final Consumer handler) { return this.error .map(caught -> { handler.accept(caught); @@ -157,11 +157,11 @@ public EffectHandler catchError(final Consumer handler) { * invoked callback */ public EffectHandler runEffect( - final ThrowingRunnable onSuccess, - final ThrowingConsumer onError + final ThrowingRunnable onSuccess, + final ThrowingConsumer onError ) { return this.error - .map(Maybe.partialEffect(onError)) + .map(Maybe.partialEffect(onError)) .orElseGet(() -> Maybe.fromEffect(onSuccess)); } @@ -175,7 +175,7 @@ public EffectHandler runEffect( * @return a new {@link EffectHandler} that is either empty or with the * thrown error */ - public EffectHandler runEffect(final ThrowingRunnable effect) { + public EffectHandler runEffect(final ThrowingRunnable effect) { return this.runEffect(effect, err -> { }); } @@ -185,7 +185,7 @@ public EffectHandler runEffect(final ThrowingRunnable effect) { + public void orElse(final Consumer effect) { this.error.ifPresent(effect); } @@ -208,7 +208,7 @@ public void orThrow() throws E { * @param mapper a function that maps the new exception to throw * @throws X a mapped exception */ - public void orThrow(final Function mapper) throws X { + public void orThrow(final Function mapper) throws X { if (this.error.isPresent()) { throw mapper.apply(this.error.get()); } diff --git a/src/main/java/io/github/joselion/maybe/Maybe.java b/src/main/java/io/github/joselion/maybe/Maybe.java index cf675dc..4f022cf 100644 --- a/src/main/java/io/github/joselion/maybe/Maybe.java +++ b/src/main/java/io/github/joselion/maybe/Maybe.java @@ -97,7 +97,9 @@ public static Maybe fromOptional(final Optional value) { * @return a {@link ResolveHandler} with either the value resolved or the thrown * exception to be handled */ - public static ResolveHandler fromResolver(final ThrowingSupplier resolver) { + public static ResolveHandler fromResolver( + final ThrowingSupplier resolver + ) { try { return ResolveHandler.ofSuccess(resolver.get()); } catch (Throwable e) { // NOSONAR @@ -116,7 +118,7 @@ public static ResolveHandler fromResolver(final T * @return an {@link EffectHandler} with either the thrown exception to be * handled or nothing */ - public static EffectHandler fromEffect(final ThrowingRunnable effect) { + public static EffectHandler fromEffect(final ThrowingRunnable effect) { try { effect.run(); return EffectHandler.empty(); @@ -153,7 +155,7 @@ public static EffectHandler fromEffect(final ThrowingRu * that receives an {@code S} value, and produces a {@code ResolveHandler} */ public static Function> partialResolver( - final ThrowingFunction resolver + final ThrowingFunction resolver ) { return value -> Maybe.fromResolver(() -> resolver.apply(value)); } @@ -183,7 +185,7 @@ public static Function> part * that receives an {@code S} value, and produces an {@code EffectHandler} */ public static Function> partialEffect( - final ThrowingConsumer effect + final ThrowingConsumer effect ) { return value -> Maybe.fromEffect(() -> effect.accept(value)); } @@ -217,7 +219,7 @@ public static ResourceHolder ResourceHolder solveResource( - final ThrowingSupplier supplier + final ThrowingSupplier supplier ) { return Maybe .fromResolver(supplier) @@ -234,10 +236,10 @@ public static ResourceHolder so * @return a {@code Maybe} with the mapped value if present, * {@link #nothing()} otherwise */ - public Maybe map(final Function mapper) { + public Maybe map(final Function mapper) { return Maybe .fromOptional(this.value) - .resolve(mapper::apply) + .resolve(mapper::apply) .toMaybe(); } @@ -254,10 +256,11 @@ public Maybe map(final Function mapper) { * @return a {@code Maybe} with the mapped value if present, * {@link #nothing()} otherwise */ - public Maybe flatMap(final Function> mapper) { + public Maybe flatMap(final Function> mapper) { return Maybe .fromOptional(this.value) .resolve(mapper::apply) + .map(Commons::>cast) .orElseGet(Maybe::nothing); } @@ -273,10 +276,12 @@ public Maybe flatMap(final Function> mapper) { * @return a {@link ResolveHandler} with either the resolved value, or the * thrown exception to be handled */ - public ResolveHandler resolve(final ThrowingFunction resolver) { + public ResolveHandler resolve( + final ThrowingFunction resolver + ) { try { return this.value - .map(Maybe.partialResolver(resolver)) + .map(Maybe.partialResolver(resolver)) .orElseThrow(); } catch (final NoSuchElementException e) { final var error = Commons.cast(e); @@ -293,10 +298,10 @@ public ResolveHandler resolve(final ThrowingFunct * @return an {@link EffectHandler} with either the thrown exception to be * handled or nothing */ - public EffectHandler runEffect(final ThrowingConsumer effect) { + public EffectHandler runEffect(final ThrowingConsumer effect) { try { return this.value - .map(Maybe.partialEffect(effect)) + .map(Maybe.partialEffect(effect)) .orElseThrow(); } catch (final NoSuchElementException e) { final var error = Commons.cast(e); diff --git a/src/main/java/io/github/joselion/maybe/ResolveHandler.java b/src/main/java/io/github/joselion/maybe/ResolveHandler.java index ceebbe5..c742486 100644 --- a/src/main/java/io/github/joselion/maybe/ResolveHandler.java +++ b/src/main/java/io/github/joselion/maybe/ResolveHandler.java @@ -80,7 +80,7 @@ Optional error() { * @param effect a function that receives the resolved value * @return the same handler to continue chainning operations */ - public ResolveHandler doOnSuccess(final Consumer effect) { + public ResolveHandler doOnSuccess(final Consumer effect) { this.value.doOnRight(effect); return this; @@ -96,7 +96,7 @@ public ResolveHandler doOnSuccess(final Consumer effect) { * @param effect a consumer function that receives the caught error * @return the same handler to continue chainning operations */ - public ResolveHandler doOnError(final Class ofType, final Consumer effect) { + public ResolveHandler doOnError(final Class ofType, final Consumer effect) { this.value .leftToOptional() .filter(ofType::isInstance) @@ -113,7 +113,7 @@ public ResolveHandler doOnError(final Class ofTyp * @param effect a consumer function that receives the caught error * @return the same handler to continue chainning operations */ - public ResolveHandler doOnError(final Consumer effect) { + public ResolveHandler doOnError(final Consumer effect) { this.value.doOnLeft(effect); return this; @@ -131,7 +131,10 @@ public ResolveHandler doOnError(final Consumer effect) { * @return a handler containing a new value if an error instance of the * provided type was caught. The same handler instance otherwise */ - public ResolveHandler catchError(final Class ofType, final Function handler) { + public ResolveHandler catchError( + final Class ofType, + final Function handler + ) { return this.value .leftToOptional() .filter(ofType::isInstance) @@ -150,7 +153,7 @@ public ResolveHandler catchError(final Class ofType, fina * @return a handler containing a new value if an error was caught. The same * handler instance otherwise */ - public ResolveHandler catchError(final Function handler) { + public ResolveHandler catchError(final Function handler) { return this.value .mapLeft(handler) .mapLeft(ResolveHandler::ofSuccess) @@ -175,8 +178,8 @@ public ResolveHandler catchError(final Function handler) { * @return a new handler with either the resolved value or the error */ public ResolveHandler resolve( - final ThrowingFunction onSuccess, - final ThrowingFunction onError + final ThrowingFunction onSuccess, + final ThrowingFunction onError ) { return this.value.unwrap( Maybe.partialResolver(onError), @@ -194,7 +197,9 @@ public ResolveHandler resolve( * resolves another * @return a new handler with either the resolved value or an error */ - public ResolveHandler resolve(final ThrowingFunction resolver) { + public ResolveHandler resolve( + final ThrowingFunction resolver + ) { return this.value .mapLeft(Commons::cast) .unwrap( @@ -214,8 +219,8 @@ public ResolveHandler resolve(final ThrowingFunct * invoked callback */ public EffectHandler runEffect( - final ThrowingConsumer onSuccess, - final ThrowingConsumer onError + final ThrowingConsumer onSuccess, + final ThrowingConsumer onError ) { return this.value.unwrap( Maybe.partialEffect(onError), @@ -233,7 +238,7 @@ public EffectHandler runEffect( * @return a new {@link EffectHandler} representing the result of the success * callback or containg the error */ - public EffectHandler runEffect(final ThrowingConsumer effect) { + public EffectHandler runEffect(final ThrowingConsumer effect) { return this.value .mapLeft(Commons::cast) .unwrap( @@ -251,7 +256,7 @@ public EffectHandler runEffect(final ThrowingConsumer ResolveHandler map(final Function mapper) { + public ResolveHandler map(final Function mapper) { return this.value .mapRight(mapper) .unwrap( @@ -302,7 +307,7 @@ public T orElse(final T fallback) { * another value * @return the resolved value if present. Another value otherwise */ - public T orElse(final Function mapper) { + public T orElse(final Function mapper) { return this.value.unwrap(mapper, Function.identity()); } @@ -317,7 +322,7 @@ public T orElse(final Function mapper) { * opration failed to resolve * @return the resolved value if present. Another value otherwise */ - public T orElseGet(final Supplier supplier) { + public T orElseGet(final Supplier supplier) { return this.value .rightToOptional() .orElseGet(supplier); @@ -360,7 +365,7 @@ public T orThrow() throws E { * @return the resolved/handled value if present * @throws X a mapped exception */ - public T orThrow(final Function mapper) throws X { + public T orThrow(final Function mapper) throws X { return this.value .rightToOptional() .orElseThrow(() -> mapper.apply(this.value.leftOrNull())); @@ -444,7 +449,7 @@ public ResourceHolder mapToResource(final Functi * present or the error otherwise. */ public ResourceHolder solveResource( - final ThrowingFunction solver + final ThrowingFunction solver ) { return this.value .mapLeft(Commons::cast) diff --git a/src/main/java/io/github/joselion/maybe/ResourceHolder.java b/src/main/java/io/github/joselion/maybe/ResourceHolder.java index fd4d756..f93b198 100644 --- a/src/main/java/io/github/joselion/maybe/ResourceHolder.java +++ b/src/main/java/io/github/joselion/maybe/ResourceHolder.java @@ -90,7 +90,9 @@ Optional error() { * @return a {@link ResolveHandler} with either the value resolved or the thrown * exception to be handled */ - public ResolveHandler resolveClosing(final ThrowingFunction resolver) { + public ResolveHandler resolveClosing( + final ThrowingFunction resolver + ) { return this.value .mapLeft(Commons::cast) .unwrap( @@ -122,7 +124,9 @@ public ResolveHandler resolveClosing(final Throwi * @return an {@link EffectHandler} with either the thrown exception to be * handled or nothing */ - public EffectHandler runEffectClosing(final ThrowingConsumer effect) { + public EffectHandler runEffectClosing( + final ThrowingConsumer effect + ) { return this.value .mapLeft(Commons::cast) .unwrap( diff --git a/src/main/java/io/github/joselion/maybe/util/Either.java b/src/main/java/io/github/joselion/maybe/util/Either.java index 5fb6af2..1e197ef 100644 --- a/src/main/java/io/github/joselion/maybe/util/Either.java +++ b/src/main/java/io/github/joselion/maybe/util/Either.java @@ -7,6 +7,8 @@ import org.eclipse.jdt.annotation.Nullable; +import io.github.joselion.maybe.helpers.Commons; + /** * Either is a monadic wrapper that contains one of two possible values which * are represented as {@code Left} or {@code Right}. the values can be of @@ -61,7 +63,7 @@ static Either ofRight(final R value) { * @param onRight a function to handle the right value if present * @return either the left or the right handled value */ - T unwrap(Function onLeft, Function onRight); + T unwrap(Function onLeft, Function onRight); /** * Returns true if the {@code Left} value is present, false otherwise. @@ -69,7 +71,7 @@ static Either ofRight(final R value) { * @return true if left is present, false otherwise */ default boolean isLeft() { - return unwrap(left -> true, right -> false); + return this.unwrap(left -> true, right -> false); } /** @@ -78,7 +80,7 @@ default boolean isLeft() { * @return true if right is present, false otherwise */ default boolean isRight() { - return unwrap(left -> false, rigth -> true); + return this.unwrap(left -> false, rigth -> true); } /** @@ -87,8 +89,8 @@ default boolean isRight() { * @param effect a consumer function that receives the left value * @return the same {@code Either} instance */ - default Either doOnLeft(final Consumer effect) { - return unwrap( + default Either doOnLeft(final Consumer effect) { + return this.unwrap( left -> { effect.accept(left); return Either.ofLeft(left); @@ -103,8 +105,8 @@ default Either doOnLeft(final Consumer effect) { * @param effect effect a consumer function that receives the right value * @return the same {@code Either} instance */ - default Either doOnRight(final Consumer effect) { - return unwrap( + default Either doOnRight(final Consumer effect) { + return this.unwrap( Either::ofLeft, right -> { effect.accept(right); @@ -120,8 +122,8 @@ default Either doOnRight(final Consumer effect) { * @param mapper a function that receives the left value and returns another * @return an {@code Either} instance with the mapped left value */ - default Either mapLeft(final Function mapper) { - return unwrap( + default Either mapLeft(final Function mapper) { + return this.unwrap( left -> Either.ofLeft(mapper.apply(left)), Either::ofRight ); @@ -134,8 +136,8 @@ default Either mapLeft(final Function mapper) { * @param mapper a function that receives the right value and returns another * @return an {@code Either} instance with the mapped right value */ - default Either mapRight(final Function mapper) { - return unwrap( + default Either mapRight(final Function mapper) { + return this.unwrap( Either::ofLeft, right -> Either.ofRight(mapper.apply(right)) ); @@ -153,8 +155,11 @@ default Either mapRight(final Function mapper) { * @param rigthMapper a function that receives the right value and returns another * @return an {@code Either} instance with the mapped left or right value */ - default Either map(final Function leftMapper, final Function rigthMapper) { - return unwrap( + default Either map( + final Function leftMapper, + final Function rigthMapper + ) { + return this.unwrap( left -> Either.ofLeft(leftMapper.apply(left)), right -> Either.ofRight(rigthMapper.apply(right)) ); @@ -171,8 +176,10 @@ default Either map(final Function leftMapper, final Function< * @param mapper a function that receives the left value and returns an {@code Either} * @return an {@code Either} instance with the mapped left value */ - default Either flatMapLeft(final Function> mapper) { - return unwrap(mapper, Either::ofRight); + default Either flatMapLeft(final Function> mapper) { + return this + .mapLeft(mapper) + .unwrap(Commons::cast, Either::ofRight); } /** @@ -186,8 +193,10 @@ default Either flatMapLeft(final Function> mapper) { * @param mapper a function that receives the right value and returns an {@code Either} * @return an {@code Either} instance with the mapped right value */ - default Either flatMapRight(final Function> mapper) { - return unwrap(Either::ofLeft, mapper); + default Either flatMapRight(final Function> mapper) { + return this + .mapRight(mapper) + .unwrap(Either::ofLeft, Commons::cast); } /** @@ -204,10 +213,13 @@ default Either flatMapRight(final Function> mapper) { * @return an {@code Either} instance with the mapped left or right value */ default Either flatMap( - final Function> leftMapper, - final Function> rigthMapper + final Function> leftMapper, + final Function> rigthMapper ) { - return unwrap(leftMapper, rigthMapper); + return this + .mapLeft(leftMapper) + .mapRight(rigthMapper) + .unwrap(Commons::cast, Commons::cast); } /** @@ -218,7 +230,7 @@ default Either flatMap( * @return the left value or a fallback */ default L leftOrElse(final L fallback) { - return unwrap(Function.identity(), rigth -> fallback); + return this.unwrap(Function.identity(), rigth -> fallback); } /** @@ -229,7 +241,7 @@ default L leftOrElse(final L fallback) { * @return the right value or a fallback */ default R rightOrElse(final R fallback) { - return unwrap(left -> fallback, Function.identity()); + return this.unwrap(left -> fallback, Function.identity()); } /** @@ -239,7 +251,7 @@ default R rightOrElse(final R fallback) { * @return the left value or null */ default @Nullable L leftOrNull() { - return unwrap(Function.identity(), rigth -> null); + return this.unwrap(Function.identity(), rigth -> null); } /** @@ -249,7 +261,7 @@ default R rightOrElse(final R fallback) { * @return the right value or null */ default @Nullable R rightOrNull() { - return unwrap(left -> null, Function.identity()); + return this.unwrap(left -> null, Function.identity()); } /** @@ -297,7 +309,7 @@ L value() { } @Override - public T unwrap(final Function onLeft, final Function onRight) { + public T unwrap(final Function onLeft, final Function onRight) { return onLeft.apply(this.value); } @@ -351,7 +363,7 @@ R value() { } @Override - public T unwrap(final Function onLeft, final Function onRight) { + public T unwrap(final Function onLeft, final Function onRight) { return onRight.apply(this.value); } diff --git a/src/main/java17/io/github/joselion/maybe/Maybe.java b/src/main/java17/io/github/joselion/maybe/Maybe.java index 8004d27..17e9d87 100644 --- a/src/main/java17/io/github/joselion/maybe/Maybe.java +++ b/src/main/java17/io/github/joselion/maybe/Maybe.java @@ -97,7 +97,9 @@ public static Maybe fromOptional(final Optional value) { * @return a {@link ResolveHandler} with either the value resolved or the thrown * exception to be handled */ - public static ResolveHandler fromResolver(final ThrowingSupplier resolver) { + public static ResolveHandler fromResolver( + final ThrowingSupplier resolver + ) { try { return ResolveHandler.ofSuccess(resolver.get()); } catch (Throwable e) { // NOSONAR @@ -116,7 +118,7 @@ public static ResolveHandler fromResolver(final T * @return an {@link EffectHandler} with either the thrown exception to be * handled or nothing */ - public static EffectHandler fromEffect(final ThrowingRunnable effect) { + public static EffectHandler fromEffect(final ThrowingRunnable effect) { try { effect.run(); return EffectHandler.empty(); @@ -153,7 +155,7 @@ public static EffectHandler fromEffect(final ThrowingRu * that receives an {@code S} value, and produces a {@code ResolveHandler} */ public static Function> partialResolver( - final ThrowingFunction resolver + final ThrowingFunction resolver ) { return value -> Maybe.fromResolver(() -> resolver.apply(value)); } @@ -183,7 +185,7 @@ public static Function> part * that receives an {@code S} value, and produces an {@code EffectHandler} */ public static Function> partialEffect( - final ThrowingConsumer effect + final ThrowingConsumer effect ) { return value -> Maybe.fromEffect(() -> effect.accept(value)); } @@ -217,7 +219,7 @@ public static ResourceHolder ResourceHolder solveResource( - final ThrowingSupplier supplier + final ThrowingSupplier supplier ) { return Maybe .fromResolver(supplier) @@ -234,10 +236,10 @@ public static ResourceHolder so * @return a {@code Maybe} with the mapped value if present, * {@link #nothing()} otherwise */ - public Maybe map(final Function mapper) { + public Maybe map(final Function mapper) { return Maybe .fromOptional(this.value) - .resolve(mapper::apply) + .resolve(mapper::apply) .toMaybe(); } @@ -254,10 +256,11 @@ public Maybe map(final Function mapper) { * @return a {@code Maybe} with the mapped value if present, * {@link #nothing()} otherwise */ - public Maybe flatMap(final Function> mapper) { + public Maybe flatMap(final Function> mapper) { return Maybe .fromOptional(this.value) .resolve(mapper::apply) + .map(Commons::>cast) .orElseGet(Maybe::nothing); } @@ -273,10 +276,12 @@ public Maybe flatMap(final Function> mapper) { * @return a {@link ResolveHandler} with either the resolved value, or the * thrown exception to be handled */ - public ResolveHandler resolve(final ThrowingFunction resolver) { + public ResolveHandler resolve( + final ThrowingFunction resolver + ) { try { return this.value - .map(Maybe.partialResolver(resolver)) + .map(Maybe.partialResolver(resolver)) .orElseThrow(); } catch (final NoSuchElementException e) { final var error = Commons.cast(e); @@ -293,10 +298,10 @@ public ResolveHandler resolve(final ThrowingFunct * @return an {@link EffectHandler} with either the thrown exception to be * handled or nothing */ - public EffectHandler runEffect(final ThrowingConsumer effect) { + public EffectHandler runEffect(final ThrowingConsumer effect) { try { return this.value - .map(Maybe.partialEffect(effect)) + .map(Maybe.partialEffect(effect)) .orElseThrow(); } catch (final NoSuchElementException e) { final var error = Commons.cast(e); diff --git a/src/main/java17/io/github/joselion/maybe/util/Either.java b/src/main/java17/io/github/joselion/maybe/util/Either.java index c4d61ab..13498fe 100644 --- a/src/main/java17/io/github/joselion/maybe/util/Either.java +++ b/src/main/java17/io/github/joselion/maybe/util/Either.java @@ -7,6 +7,8 @@ import org.eclipse.jdt.annotation.Nullable; +import io.github.joselion.maybe.helpers.Commons; + /** * Either is a monadic wrapper that contains one of two possible values which * are represented as {@code Left} or {@code Right}. the values can be of @@ -61,7 +63,7 @@ static Either ofRight(final R value) { * @param onRight a function to handle the right value if present * @return either the left or the right handled value */ - T unwrap(Function onLeft, Function onRight); + T unwrap(Function onLeft, Function onRight); /** * Returns true if the {@code Left} value is present, false otherwise. @@ -69,7 +71,7 @@ static Either ofRight(final R value) { * @return true if left is present, false otherwise */ default boolean isLeft() { - return unwrap(left -> true, right -> false); + return this.unwrap(left -> true, right -> false); } /** @@ -78,7 +80,7 @@ default boolean isLeft() { * @return true if right is present, false otherwise */ default boolean isRight() { - return unwrap(left -> false, rigth -> true); + return this.unwrap(left -> false, rigth -> true); } /** @@ -87,8 +89,8 @@ default boolean isRight() { * @param effect a consumer function that receives the left value * @return the same {@code Either} instance */ - default Either doOnLeft(final Consumer effect) { - return unwrap( + default Either doOnLeft(final Consumer effect) { + return this.unwrap( left -> { effect.accept(left); return Either.ofLeft(left); @@ -103,8 +105,8 @@ default Either doOnLeft(final Consumer effect) { * @param effect effect a consumer function that receives the right value * @return the same {@code Either} instance */ - default Either doOnRight(final Consumer effect) { - return unwrap( + default Either doOnRight(final Consumer effect) { + return this.unwrap( Either::ofLeft, right -> { effect.accept(right); @@ -120,8 +122,8 @@ default Either doOnRight(final Consumer effect) { * @param mapper a function that receives the left value and returns another * @return an {@code Either} instance with the mapped left value */ - default Either mapLeft(final Function mapper) { - return unwrap( + default Either mapLeft(final Function mapper) { + return this.unwrap( left -> Either.ofLeft(mapper.apply(left)), Either::ofRight ); @@ -134,8 +136,8 @@ default Either mapLeft(final Function mapper) { * @param mapper a function that receives the right value and returns another * @return an {@code Either} instance with the mapped right value */ - default Either mapRight(final Function mapper) { - return unwrap( + default Either mapRight(final Function mapper) { + return this.unwrap( Either::ofLeft, right -> Either.ofRight(mapper.apply(right)) ); @@ -153,8 +155,11 @@ default Either mapRight(final Function mapper) { * @param rigthMapper a function that receives the right value and returns another * @return an {@code Either} instance with the mapped left or right value */ - default Either map(final Function leftMapper, final Function rigthMapper) { - return unwrap( + default Either map( + final Function leftMapper, + final Function rigthMapper + ) { + return this.unwrap( left -> Either.ofLeft(leftMapper.apply(left)), right -> Either.ofRight(rigthMapper.apply(right)) ); @@ -171,8 +176,10 @@ default Either map(final Function leftMapper, final Function< * @param mapper a function that receives the left value and returns an {@code Either} * @return an {@code Either} instance with the mapped left value */ - default Either flatMapLeft(final Function> mapper) { - return unwrap(mapper, Either::ofRight); + default Either flatMapLeft(final Function> mapper) { + return this + .mapLeft(mapper) + .unwrap(Commons::cast, Either::ofRight); } /** @@ -186,8 +193,10 @@ default Either flatMapLeft(final Function> mapper) { * @param mapper a function that receives the right value and returns an {@code Either} * @return an {@code Either} instance with the mapped right value */ - default Either flatMapRight(final Function> mapper) { - return unwrap(Either::ofLeft, mapper); + default Either flatMapRight(final Function> mapper) { + return this + .mapRight(mapper) + .unwrap(Either::ofLeft, Commons::cast); } /** @@ -204,10 +213,13 @@ default Either flatMapRight(final Function> mapper) { * @return an {@code Either} instance with the mapped left or right value */ default Either flatMap( - final Function> leftMapper, - final Function> rigthMapper + final Function> leftMapper, + final Function> rigthMapper ) { - return unwrap(leftMapper, rigthMapper); + return this + .mapLeft(leftMapper) + .mapRight(rigthMapper) + .unwrap(Commons::cast, Commons::cast); } /** @@ -218,7 +230,7 @@ default Either flatMap( * @return the left value or a fallback */ default L leftOrElse(final L fallback) { - return unwrap(Function.identity(), rigth -> fallback); + return this.unwrap(Function.identity(), rigth -> fallback); } /** @@ -229,7 +241,7 @@ default L leftOrElse(final L fallback) { * @return the right value or a fallback */ default R rightOrElse(final R fallback) { - return unwrap(left -> fallback, Function.identity()); + return this.unwrap(left -> fallback, Function.identity()); } /** @@ -239,7 +251,7 @@ default R rightOrElse(final R fallback) { * @return the left value or null */ default @Nullable L leftOrNull() { - return unwrap(Function.identity(), rigth -> null); + return this.unwrap(Function.identity(), rigth -> null); } /** @@ -249,7 +261,7 @@ default R rightOrElse(final R fallback) { * @return the right value or null */ default @Nullable R rightOrNull() { - return unwrap(left -> null, Function.identity()); + return this.unwrap(left -> null, Function.identity()); } /** @@ -291,7 +303,7 @@ record Left(L value) implements Either { } @Override - public T unwrap(final Function onLeft, final Function onRight) { + public T unwrap(final Function onLeft, final Function onRight) { return onLeft.apply(this.value); } @@ -338,7 +350,7 @@ record Right(R value) implements Either { } @Override - public T unwrap(final Function onLeft, final Function onRight) { + public T unwrap(final Function onLeft, final Function onRight) { return onRight.apply(this.value); }