Removed 'match' method
This commit is contained in:
parent
284035e57b
commit
d81727e0d9
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user