Added match method and unit tests
This commit is contained in:
parent
72e5fc2e03
commit
7998bc7761
8
pom.xml
8
pom.xml
@ -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>
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.ceticamarco;
|
||||
package com.ceticamarco.lambdatonic;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
19
src/main/java/com/ceticamarco/lambdatonic/Left.java
Normal file
19
src/main/java/com/ceticamarco/lambdatonic/Left.java
Normal 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);
|
||||
}
|
||||
}
|
19
src/main/java/com/ceticamarco/lambdatonic/Right.java
Normal file
19
src/main/java/com/ceticamarco/lambdatonic/Right.java
Normal 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);
|
||||
}
|
||||
}
|
21
src/test/java/com/ceticamarco/lambdatonic/LeftTests.java
Normal file
21
src/test/java/com/ceticamarco/lambdatonic/LeftTests.java
Normal 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);
|
||||
}
|
||||
}
|
21
src/test/java/com/ceticamarco/lambdatonic/RightTests.java
Normal file
21
src/test/java/com/ceticamarco/lambdatonic/RightTests.java
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user