Added isLeft/isRight methods and unit tests
This commit is contained in:
parent
7998bc7761
commit
43ce72e819
4
pom.xml
4
pom.xml
@ -15,8 +15,8 @@
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
@ -13,5 +13,33 @@ import java.util.function.Function;
|
||||
* @param <R> The right type, representing the actual value
|
||||
*/
|
||||
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);
|
||||
|
||||
/**
|
||||
* <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();
|
||||
}
|
@ -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) {
|
||||
return onLeft.apply(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeft() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRight() {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -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) {
|
||||
return onRight.apply(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeft() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRight() {
|
||||
return true;
|
||||
}
|
||||
}
|
@ -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<Integer, String> resEither;
|
||||
|
||||
@BeforeEach
|
||||
public void tearUp() {
|
||||
this.resEither = new Left<>(19);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMatchLeft() {
|
||||
Either<Integer, String> 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());
|
||||
}
|
||||
}
|
||||
|
@ -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<Integer, String> resEither;
|
||||
|
||||
@BeforeEach
|
||||
public void tearUp() {
|
||||
this.resEither = new Right<>("Query executed successfully");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMatchRight() {
|
||||
Either<Integer, String> 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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user