Either refactor (#2237)
This commit is contained in:
parent
a219184b46
commit
e422edadd0
@ -18,7 +18,7 @@ public class EitherDemo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, Object> computeWithoutEitherUsingMap(int marks) {
|
public static Map<String, Object> computeWithoutEitherUsingMap(int marks) {
|
||||||
Map<String, Object> results = new HashMap<String, Object>();
|
Map<String, Object> results = new HashMap<>();
|
||||||
if (marks < 85) {
|
if (marks < 85) {
|
||||||
results.put("FAILURE", "Marks not acceptable");
|
results.put("FAILURE", "Marks not acceptable");
|
||||||
} else {
|
} else {
|
||||||
@ -27,64 +27,11 @@ public class EitherDemo {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Either<String, Integer> computeWithEither(int marks) {
|
static Either<String, Integer> computeWithEither(int marks) {
|
||||||
if (marks < 85) {
|
if (marks < 85) {
|
||||||
return Either.left("Marks not acceptable");
|
return Either.left("Marks not acceptable");
|
||||||
} else {
|
} else {
|
||||||
return Either.right(marks);
|
return Either.right(marks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getError(Either<String, Integer> result) {
|
|
||||||
return result.getLeft();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getMarks(Either<String, Integer> result) {
|
|
||||||
return result.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getModifiedMarks(Either<String, Integer> result) {
|
|
||||||
result = result.right().map(i -> i * 2).toEither();
|
|
||||||
return result.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void utilities() {
|
|
||||||
|
|
||||||
String error;
|
|
||||||
int marks;
|
|
||||||
|
|
||||||
Either<String, Integer> 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<String, Integer> projection = computeWithEither(9).right();
|
|
||||||
|
|
||||||
result.contains(800);
|
|
||||||
result.isLeft();
|
|
||||||
result.isRight();
|
|
||||||
|
|
||||||
if (result.isLeft()) {
|
|
||||||
error = result.getLeft();
|
|
||||||
} else {
|
|
||||||
marks = result.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -100,13 +100,11 @@ public class PatternMatchingUnitTest {
|
|||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void displayEven() {
|
||||||
|
|
||||||
public void displayEven() {
|
|
||||||
System.out.println("Input is even");
|
System.out.println("Input is even");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void displayOdd() {
|
private void displayOdd() {
|
||||||
System.out.println("Input is odd");
|
System.out.println("Input is odd");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package com.baeldung.vavr.either;
|
package com.baeldung.vavr.either;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import io.vavr.control.Either;
|
import io.vavr.control.Either;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@ -11,21 +10,28 @@ public class EitherUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenMarks_whenPassNumber_thenExpectNumber() {
|
public void givenMarks_whenPassNumber_thenExpectNumber() {
|
||||||
Either<String, Integer> result = EitherDemo.computeWithEither(100);
|
Either<String, Integer> result = EitherDemo.computeWithEither(100);
|
||||||
int marks = EitherDemo.getMarks(result);
|
int marks = result.right()
|
||||||
|
.getOrElseThrow(x -> new IllegalStateException());
|
||||||
|
|
||||||
assertEquals(100, marks);
|
assertEquals(100, marks);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMarks_whenFailNumber_thenExpectErrorMesssage() {
|
public void givenMarks_whenFailNumber_thenExpectErrorMesssage() {
|
||||||
Either<String, Integer> result = EitherDemo.computeWithEither(50);
|
Either<String, Integer> result = EitherDemo.computeWithEither(50);
|
||||||
String error = EitherDemo.getError(result);
|
String error = result.left()
|
||||||
|
.getOrNull();
|
||||||
|
|
||||||
assertEquals("Marks not acceptable", error);
|
assertEquals("Marks not acceptable", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPassMarks_whenModified_thenExpectNumber() {
|
public void givenPassMarks_whenModified_thenExpectNumber() {
|
||||||
Either<String, Integer> result = EitherDemo.computeWithEither(90);
|
Either<String, Integer> result = EitherDemo.computeWithEither(90);
|
||||||
int marks = EitherDemo.getModifiedMarks(result);
|
int marks = result.right()
|
||||||
|
.map(x -> x * 2)
|
||||||
|
.get();
|
||||||
|
|
||||||
assertEquals(180, marks);
|
assertEquals(180, marks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user