diff --git a/vavr/src/main/java/com/baeldung/vavr/either/EitherDemo.java b/vavr/src/main/java/com/baeldung/vavr/either/EitherDemo.java new file mode 100644 index 0000000000..38df03980f --- /dev/null +++ b/vavr/src/main/java/com/baeldung/vavr/either/EitherDemo.java @@ -0,0 +1,90 @@ +package com.baeldung.vavr.either; + +import java.util.HashMap; +import java.util.Map; + +import io.vavr.control.Either; + +public class EitherDemo { + + public static Object[] computeWithoutEitherUsingArray(int marks) { + Object[] results = new Object[2]; + if (marks < 85) { + results[0] = "Marks not acceptable"; + } else { + results[1] = marks; + } + return results; + } + + public static Map computeWithoutEitherUsingMap(int marks) { + Map results = new HashMap(); + if (marks < 85) { + results.put("FAILURE", "Marks not acceptable"); + } else { + results.put("SUCCESS", marks); + } + return results; + } + + public static Either computeWithEither(int marks) { + if (marks < 85) { + return Either.left("Marks not acceptable"); + } else { + return Either.right(marks); + } + } + + public static String getError(Either result) { + return result.getLeft(); + } + + public static int getMarks(Either result) { + return result.get(); + } + + public static int getModifiedMarks(Either result) { + result = result.right().map(i -> i * 2).toEither(); + return result.get(); + } + + public void utilities() { + + String error; + int marks; + + Either result = computeWithEither(100); + + result.toArray(); + result.toCharSeq(); + result.toLinkedSet(); + result.toList(); + result.toOption(); + result.toPriorityQueue(); + result.iterator(); + result.toVector(); + result.toTree(); + result.toStream(); + + result.toJavaArray(); + result.toJavaList(); + result.toJavaOptional(); + result.toJavaParallelStream(); + result.toJavaSet(); + result.toJavaStream(); + result.toJavaList(); + + Either.RightProjection projection = computeWithEither(9).right(); + + result.contains(800); + result.isLeft(); + result.isRight(); + + if (result.isLeft()) { + error = result.getLeft(); + } else { + marks = result.get(); + } + } + +} diff --git a/vavr/src/test/java/com/baeldung/vavr/either/EitherUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/either/EitherUnitTest.java new file mode 100644 index 0000000000..90cd1ace35 --- /dev/null +++ b/vavr/src/test/java/com/baeldung/vavr/either/EitherUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.vavr.either; + +import org.junit.Test; + +import io.vavr.control.Either; + +import static org.junit.Assert.assertEquals; + +public class EitherUnitTest { + + @Test + public void givenMarks_whenPassNumber_thenExpectNumber() { + Either result = EitherDemo.computeWithEither(100); + int marks = EitherDemo.getMarks(result); + assertEquals(100, marks); + } + + @Test + public void givenMarks_whenFailNumber_thenExpectErrorMesssage() { + Either result = EitherDemo.computeWithEither(50); + String error = EitherDemo.getError(result); + assertEquals("Marks not acceptable", error); + } + + @Test + public void givenPassMarks_whenModified_thenExpectNumber() { + Either result = EitherDemo.computeWithEither(90); + int marks = EitherDemo.getModifiedMarks(result); + assertEquals(180, marks); + } + +}