Added match method and unit tests

This commit is contained in:
Marco Cetica 2024-09-17 11:00:21 +02:00
parent 72e5fc2e03
commit 7998bc7761
Signed by: marco
GPG Key ID: 45060A949E90D0FD
8 changed files with 89 additions and 29 deletions

View File

@ -13,5 +13,13 @@
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,18 +0,0 @@
package com.ceticamarco;
import java.util.function.Function;
/**
* <p>
*
* </p>
* @param value
* @param <L>
* @param <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);
}
}

View File

@ -1,10 +0,0 @@
package com.ceticamarco;
import java.util.function.Function;
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);
}
}

View File

@ -1,4 +1,4 @@
package com.ceticamarco;
package com.ceticamarco.lambdatonic;
import java.util.function.Function;

View File

@ -0,0 +1,19 @@
package com.ceticamarco.lambdatonic;
import java.util.function.Function;
/**
* <p>
* Subtype of the <i>Either</i> protocol.
* This class sets the left value of the Either type
* </p>
* @param value The left value of the <i>Either</i> type
* @param <L> The left type, representing the error value
* @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);
}
}

View File

@ -0,0 +1,19 @@
package com.ceticamarco.lambdatonic;
import java.util.function.Function;
/**
* <p>
* Subtype of the <i>Either</i> protocol.
* This class sets the right value of the either type
* </p>
* @param value The right value of the <i>Either</i> type
* @param <L> The left type, representing the error value
* @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);
}
}

View File

@ -0,0 +1,21 @@
package com.ceticamarco.lambdatonic;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class LeftTests {
@Test
public void testMatchLeft() {
Either<Integer, String> resEither = new Left<>(19);
var actual = resEither.match(
errorCode -> "Error code: " + errorCode.toString(),
successMsg -> successMsg
);
var expected = "Error code: 19";
assertEquals(expected, actual);
}
}

View File

@ -0,0 +1,21 @@
package com.ceticamarco.lambdatonic;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RightTests {
@Test
public void testMatchRight() {
Either<Integer, String> resEither = new Right<>("Query executed successfully");
var actual = resEither.match(
errorCode -> "Error code: " + errorCode.toString(),
successMsg -> successMsg
);
var expected = "Query executed successfully";
assertEquals(expected, actual);
}
}