Removed 'match' method

This commit is contained in:
Marco Cetica 2024-09-19 09:13:19 +02:00
parent 284035e57b
commit d81727e0d9
Signed by: marco
GPG Key ID: 45060A949E90D0FD
5 changed files with 16 additions and 39 deletions

View File

@ -14,19 +14,6 @@ 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 against
* the <i>Either</i> 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 <i>Either</i> type is instantiated with the Left subtype,

View File

@ -13,11 +13,6 @@ import java.util.function.Function;
* @param <R> The right type, representing the actual value
*/
public record Left<L, R>(L value) implements Either<L, R> {
@Override
public <T> T match(Function<L, T> onLeft, Function<R, T> onRight) {
return onLeft.apply(this.value);
}
@Override
public boolean isLeft() {
return true;

View File

@ -13,11 +13,6 @@ import java.util.function.Function;
* @param <R> The right type, representing the actual value
*/
public record Right<L, R>(R value) implements Either<L, R> {
@Override
public <T> T match(Function<L, T> onLeft, Function<R, T> onRight) {
return onRight.apply(this.value);
}
@Override
public boolean isLeft() {
return false;

View File

@ -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<Integer, String> left -> "Error code: " + left.value();
case Right<Integer, String> right -> right;
};
var expected = "Error code: 19";
@ -46,10 +46,10 @@ public class LeftTests {
public void testFunctorMapLeft() {
Either<Error, Integer> res = this.numEither.map(x -> x * x);
assertEquals(res.match(
Throwable::getMessage,
_ -> "Undefined variable"
), "Undefined variable");
switch (res) {
case Left<Error, Integer> left -> assertEquals(left.value().getMessage(), "Undefined variable");
case Right<Error, Integer> _ -> { }
}
}
@Test

View File

@ -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<Integer, String> left -> "Error code: " + left;
case Right<Integer, String> right -> right.value();
};
var expected = "Query executed successfully";
@ -46,10 +46,10 @@ public class RightTests {
public void testFunctorMapRight() {
Either<Error, Integer> res = this.numEither.map(x -> x * x);
assertEquals((int)res.match(
_ -> 0,
x -> x
), 16);
switch (res) {
case Left<Error, Integer> _ -> { }
case Right<Error, Integer> right -> assertEquals(right.value(), 16);
}
}
@Test