Either refactor (#2237)

This commit is contained in:
Grzegorz Piwowarek 2017-07-09 12:28:04 +02:00 committed by GitHub
parent a219184b46
commit e422edadd0
3 changed files with 15 additions and 64 deletions

View File

@ -18,7 +18,7 @@ public class EitherDemo {
}
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) {
results.put("FAILURE", "Marks not acceptable");
} else {
@ -27,64 +27,11 @@ public class EitherDemo {
return results;
}
public static Either<String, Integer> computeWithEither(int marks) {
static Either<String, Integer> computeWithEither(int marks) {
if (marks < 85) {
return Either.left("Marks not acceptable");
} else {
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();
}
}
}

View File

@ -100,13 +100,11 @@ public class PatternMatchingUnitTest {
})));
}
public void displayEven() {
private void displayEven() {
System.out.println("Input is even");
}
public void displayOdd() {
private void displayOdd() {
System.out.println("Input is odd");
}
}

View File

@ -1,8 +1,7 @@
package com.baeldung.vavr.either;
import org.junit.Test;
import io.vavr.control.Either;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -11,21 +10,28 @@ public class EitherUnitTest {
@Test
public void givenMarks_whenPassNumber_thenExpectNumber() {
Either<String, Integer> result = EitherDemo.computeWithEither(100);
int marks = EitherDemo.getMarks(result);
int marks = result.right()
.getOrElseThrow(x -> new IllegalStateException());
assertEquals(100, marks);
}
@Test
public void givenMarks_whenFailNumber_thenExpectErrorMesssage() {
Either<String, Integer> result = EitherDemo.computeWithEither(50);
String error = EitherDemo.getError(result);
String error = result.left()
.getOrNull();
assertEquals("Marks not acceptable", error);
}
@Test
public void givenPassMarks_whenModified_thenExpectNumber() {
Either<String, Integer> result = EitherDemo.computeWithEither(90);
int marks = EitherDemo.getModifiedMarks(result);
int marks = result.right()
.map(x -> x * 2)
.get();
assertEquals(180, marks);
}