diff --git a/pom.xml b/pom.xml index ed086bf..c4f4f30 100644 --- a/pom.xml +++ b/pom.xml @@ -15,8 +15,8 @@ - junit - junit + org.junit.jupiter + junit-jupiter RELEASE test diff --git a/src/main/java/com/ceticamarco/lambdatonic/Either.java b/src/main/java/com/ceticamarco/lambdatonic/Either.java index a1df4f7..dc76ec8 100644 --- a/src/main/java/com/ceticamarco/lambdatonic/Either.java +++ b/src/main/java/com/ceticamarco/lambdatonic/Either.java @@ -13,5 +13,33 @@ 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 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, + * false otherwise + *

+ * @return Boolean value + */ + boolean isLeft(); + + /** + *

+ * Returns true if the Either type is instantiated with the Right subtype, + * false otherwise + *

+ * @return Boolean value + */ + boolean isRight(); } \ No newline at end of file diff --git a/src/main/java/com/ceticamarco/lambdatonic/Left.java b/src/main/java/com/ceticamarco/lambdatonic/Left.java index 72eba3b..255430c 100644 --- a/src/main/java/com/ceticamarco/lambdatonic/Left.java +++ b/src/main/java/com/ceticamarco/lambdatonic/Left.java @@ -16,4 +16,14 @@ public record Left(L value) implements Either { public T match(Function onLeft, Function onRight) { return onLeft.apply(this.value); } + + @Override + public boolean isLeft() { + return true; + } + + @Override + public boolean isRight() { + return false; + } } \ No newline at end of file diff --git a/src/main/java/com/ceticamarco/lambdatonic/Right.java b/src/main/java/com/ceticamarco/lambdatonic/Right.java index 288f07d..dd4c0cc 100644 --- a/src/main/java/com/ceticamarco/lambdatonic/Right.java +++ b/src/main/java/com/ceticamarco/lambdatonic/Right.java @@ -16,4 +16,14 @@ public record Right(R value) implements Either { public T match(Function onLeft, Function onRight) { return onRight.apply(this.value); } + + @Override + public boolean isLeft() { + return false; + } + + @Override + public boolean isRight() { + return true; + } } \ No newline at end of file diff --git a/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java b/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java index 64afce1..e98ab04 100644 --- a/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java +++ b/src/test/java/com/ceticamarco/lambdatonic/LeftTests.java @@ -1,15 +1,23 @@ package com.ceticamarco.lambdatonic; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; public class LeftTests { + private Either resEither; + + @BeforeEach + public void tearUp() { + this.resEither = new Left<>(19); + } + @Test public void testMatchLeft() { - Either resEither = new Left<>(19); - - var actual = resEither.match( + var actual = this.resEither.match( errorCode -> "Error code: " + errorCode.toString(), successMsg -> successMsg ); @@ -18,4 +26,14 @@ public class LeftTests { assertEquals(expected, actual); } + + @Test + public void testIsLeft() { + assertTrue(this.resEither.isLeft()); + } + + @Test + public void testIsRight() { + assertFalse(this.resEither.isRight()); + } } diff --git a/src/test/java/com/ceticamarco/lambdatonic/RightTests.java b/src/test/java/com/ceticamarco/lambdatonic/RightTests.java index 743fcb0..ad16f48 100644 --- a/src/test/java/com/ceticamarco/lambdatonic/RightTests.java +++ b/src/test/java/com/ceticamarco/lambdatonic/RightTests.java @@ -1,15 +1,23 @@ package com.ceticamarco.lambdatonic; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; public class RightTests { + private Either resEither; + + @BeforeEach + public void tearUp() { + this.resEither = new Right<>("Query executed successfully"); + } + @Test public void testMatchRight() { - Either resEither = new Right<>("Query executed successfully"); - - var actual = resEither.match( + var actual = this.resEither.match( errorCode -> "Error code: " + errorCode.toString(), successMsg -> successMsg ); @@ -18,4 +26,14 @@ public class RightTests { assertEquals(expected, actual); } + + @Test + public void testIsLeft() { + assertFalse(this.resEither.isLeft()); + } + + @Test + public void testIsRight() { + assertTrue(this.resEither.isRight()); + } }