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 * @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 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> * <p>
* Returns true if the <i>Either</i> type is instantiated with the Left subtype, * 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 * @param <R> The right type, representing the actual value
*/ */
public record Left<L, R>(L value) implements Either<L, R> { 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 @Override
public boolean isLeft() { public boolean isLeft() {
return true; return true;

View File

@ -13,11 +13,6 @@ import java.util.function.Function;
* @param <R> The right type, representing the actual value * @param <R> The right type, representing the actual value
*/ */
public record Right<L, R>(R value) implements Either<L, R> { 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 @Override
public boolean isLeft() { public boolean isLeft() {
return false; return false;

View File

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

View File

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