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
|
* @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,
|
||||||
|
@ -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;
|
||||||
|
@ -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;
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user