Added isLeft/isRight methods and unit tests

This commit is contained in:
Marco Cetica 2024-09-17 11:50:34 +02:00
parent 7998bc7761
commit 43ce72e819
Signed by: marco
GPG Key ID: 45060A949E90D0FD
6 changed files with 96 additions and 12 deletions

View File

@ -15,8 +15,8 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit</artifactId> <artifactId>junit-jupiter</artifactId>
<version>RELEASE</version> <version>RELEASE</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>

View File

@ -13,5 +13,33 @@ import java.util.function.Function;
* @param <R> The right type, representing the actual value * @param <R> The right type, representing the actual value
*/ */
public sealed interface Either<L, R> permits Left, Right { public sealed interface Either<L, R> permits Left, Right {
/**
* <p>
* Executing an anonymous function by discriminating the Either data type value
* </p>
*
* @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 <T> The type of the return value of the onLeft/onRight methods
*/
<T> T match(Function<L, T> onLeft, Function<R, T> onRight); <T> T match(Function<L, T> onLeft, Function<R, T> onRight);
/**
* <p>
* Returns true if the Either type is instantiated with the Left subtype,
* false otherwise
* </p>
* @return Boolean value
*/
boolean isLeft();
/**
* <p>
* Returns true if the Either type is instantiated with the Right subtype,
* false otherwise
* </p>
* @return Boolean value
*/
boolean isRight();
} }

View File

@ -16,4 +16,14 @@ public record Left<L, R>(L value) implements Either<L, R> {
public <T> T match(Function<L, T> onLeft, Function<R, T> onRight) { public <T> T match(Function<L, T> onLeft, Function<R, T> onRight) {
return onLeft.apply(this.value); return onLeft.apply(this.value);
} }
@Override
public boolean isLeft() {
return true;
}
@Override
public boolean isRight() {
return false;
}
} }

View File

@ -16,4 +16,14 @@ public record Right<L, R>(R value) implements Either<L, R> {
public <T> T match(Function<L, T> onLeft, Function<R, T> onRight) { public <T> T match(Function<L, T> onLeft, Function<R, T> onRight) {
return onRight.apply(this.value); return onRight.apply(this.value);
} }
@Override
public boolean isLeft() {
return false;
}
@Override
public boolean isRight() {
return true;
}
} }

View File

@ -1,15 +1,23 @@
package com.ceticamarco.lambdatonic; 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 { public class LeftTests {
private Either<Integer, String> resEither;
@BeforeEach
public void tearUp() {
this.resEither = new Left<>(19);
}
@Test @Test
public void testMatchLeft() { public void testMatchLeft() {
Either<Integer, String> resEither = new Left<>(19); var actual = this.resEither.match(
var actual = resEither.match(
errorCode -> "Error code: " + errorCode.toString(), errorCode -> "Error code: " + errorCode.toString(),
successMsg -> successMsg successMsg -> successMsg
); );
@ -18,4 +26,14 @@ public class LeftTests {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testIsLeft() {
assertTrue(this.resEither.isLeft());
}
@Test
public void testIsRight() {
assertFalse(this.resEither.isRight());
}
} }

View File

@ -1,15 +1,23 @@
package com.ceticamarco.lambdatonic; 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 { public class RightTests {
private Either<Integer, String> resEither;
@BeforeEach
public void tearUp() {
this.resEither = new Right<>("Query executed successfully");
}
@Test @Test
public void testMatchRight() { public void testMatchRight() {
Either<Integer, String> resEither = new Right<>("Query executed successfully"); var actual = this.resEither.match(
var actual = resEither.match(
errorCode -> "Error code: " + errorCode.toString(), errorCode -> "Error code: " + errorCode.toString(),
successMsg -> successMsg successMsg -> successMsg
); );
@ -18,4 +26,14 @@ public class RightTests {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testIsLeft() {
assertFalse(this.resEither.isLeft());
}
@Test
public void testIsRight() {
assertTrue(this.resEither.isRight());
}
} }