From d81727e0d991e195e25391e1b0c111af82d5337f Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Thu, 19 Sep 2024 09:13:19 +0200 Subject: [PATCH] Removed 'match' method --- .../java/com/ceticamarco/lambdatonic/Either.java | 13 ------------- .../java/com/ceticamarco/lambdatonic/Left.java | 5 ----- .../java/com/ceticamarco/lambdatonic/Right.java | 5 ----- .../com/ceticamarco/lambdatonic/LeftTests.java | 16 ++++++++-------- .../com/ceticamarco/lambdatonic/RightTests.java | 16 ++++++++-------- 5 files changed, 16 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/ceticamarco/lambdatonic/Either.java b/src/main/java/com/ceticamarco/lambdatonic/Either.java index 8fc8d52..ad7feba 100644 --- a/src/main/java/com/ceticamarco/lambdatonic/Either.java +++ b/src/main/java/com/ceticamarco/lambdatonic/Either.java @@ -14,19 +14,6 @@ import java.util.function.Function; * @param The right type, representing the actual value */ public sealed interface Either permits Left, Right { - /** - *

- * Executing an anonymous function by discriminating against - * the Either data type value - *

- * - * @param onLeft The function to execute on the Left case - * @param onRight The function to execute on the Right case - * @return The return value of the onLeft/onRight methods - * @param The type of the return value of the onLeft/onRight methods - */ - T match(Function onLeft, Function onRight); - /** *

* Returns true if the Either type is instantiated with the Left subtype, diff --git a/src/main/java/com/ceticamarco/lambdatonic/Left.java b/src/main/java/com/ceticamarco/lambdatonic/Left.java index e9f89ac..d74faac 100644 --- a/src/main/java/com/ceticamarco/lambdatonic/Left.java +++ b/src/main/java/com/ceticamarco/lambdatonic/Left.java @@ -13,11 +13,6 @@ import java.util.function.Function; * @param The right type, representing the actual value */ public record Left(L value) implements Either { - @Override - public T match(Function onLeft, Function onRight) { - return onLeft.apply(this.value); - } - @Override public boolean isLeft() { return true; diff --git a/src/main/java/com/ceticamarco/lambdatonic/Right.java b/src/main/java/com/ceticamarco/lambdatonic/Right.java index d4b8cdd..fe07414 100644 --- a/src/main/java/com/ceticamarco/lambdatonic/Right.java +++ b/src/main/java/com/ceticamarco/lambdatonic/Right.java @@ -13,11 +13,6 @@ import java.util.function.Function; * @param The right type, representing the actual value */ public record Right(R value) implements Either { - @Override - public T match(Function onLeft, Function onRight) { - return onRight.apply(this.value); - } - @Override public boolean isLeft() { return false; diff --git a/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java b/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java index cb10c01..8eeeefe 100644 --- a/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java +++ b/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java @@ -22,10 +22,10 @@ public class LeftTests { @Test public void testMatchLeft() { - var actual = this.resEither.match( - errorCode -> "Error code: " + errorCode.toString(), - successMsg -> successMsg - ); + var actual = switch (this.resEither) { + case Left left -> "Error code: " + left.value(); + case Right right -> right; + }; var expected = "Error code: 19"; @@ -46,10 +46,10 @@ public class LeftTests { public void testFunctorMapLeft() { Either res = this.numEither.map(x -> x * x); - assertEquals(res.match( - Throwable::getMessage, - _ -> "Undefined variable" - ), "Undefined variable"); + switch (res) { + case Left left -> assertEquals(left.value().getMessage(), "Undefined variable"); + case Right _ -> { } + } } @Test diff --git a/src/test/java/com/ceticamarco/lambdatonic/RightTests.java b/src/test/java/com/ceticamarco/lambdatonic/RightTests.java index 0076f80..4bab9ff 100644 --- a/src/test/java/com/ceticamarco/lambdatonic/RightTests.java +++ b/src/test/java/com/ceticamarco/lambdatonic/RightTests.java @@ -22,10 +22,10 @@ public class RightTests { @Test public void testMatchRight() { - var actual = this.resEither.match( - errorCode -> "Error code: " + errorCode.toString(), - successMsg -> successMsg - ); + var actual = switch (this.resEither) { + case Left left -> "Error code: " + left; + case Right right -> right.value(); + }; var expected = "Query executed successfully"; @@ -46,10 +46,10 @@ public class RightTests { public void testFunctorMapRight() { Either res = this.numEither.map(x -> x * x); - assertEquals((int)res.match( - _ -> 0, - x -> x - ), 16); + switch (res) { + case Left _ -> { } + case Right right -> assertEquals(right.value(), 16); + } } @Test