Skip to content

Commit e422eda

Browse files
authored
Either refactor (eugenp#2237)
1 parent a219184 commit e422eda

3 files changed

Lines changed: 15 additions & 64 deletions

File tree

‎vavr/src/main/java/com/baeldung/vavr/either/EitherDemo.java‎

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static Object[] computeWithoutEitherUsingArray(int marks) {
1818
}
1919

2020
public static Map<String, Object> computeWithoutEitherUsingMap(int marks) {
21-
Map<String, Object> results = new HashMap<String, Object>();
21+
Map<String, Object> results = new HashMap<>();
2222
if (marks < 85) {
2323
results.put("FAILURE", "Marks not acceptable");
2424
} else {
@@ -27,64 +27,11 @@ public static Map<String, Object> computeWithoutEitherUsingMap(int marks) {
2727
return results;
2828
}
2929

30-
public static Either<String, Integer> computeWithEither(int marks) {
30+
static Either<String, Integer> computeWithEither(int marks) {
3131
if (marks < 85) {
3232
return Either.left("Marks not acceptable");
3333
} else {
3434
return Either.right(marks);
3535
}
3636
}
37-
38-
public static String getError(Either<String, Integer> result) {
39-
return result.getLeft();
40-
}
41-
42-
public static int getMarks(Either<String, Integer> result) {
43-
return result.get();
44-
}
45-
46-
public static int getModifiedMarks(Either<String, Integer> result) {
47-
result = result.right().map(i -> i * 2).toEither();
48-
return result.get();
49-
}
50-
51-
public void utilities() {
52-
53-
String error;
54-
int marks;
55-
56-
Either<String, Integer> result = computeWithEither(100);
57-
58-
result.toArray();
59-
result.toCharSeq();
60-
result.toLinkedSet();
61-
result.toList();
62-
result.toOption();
63-
result.toPriorityQueue();
64-
result.iterator();
65-
result.toVector();
66-
result.toTree();
67-
result.toStream();
68-
69-
result.toJavaArray();
70-
result.toJavaList();
71-
result.toJavaOptional();
72-
result.toJavaParallelStream();
73-
result.toJavaSet();
74-
result.toJavaStream();
75-
result.toJavaList();
76-
77-
Either.RightProjection<String, Integer> projection = computeWithEither(9).right();
78-
79-
result.contains(800);
80-
result.isLeft();
81-
result.isRight();
82-
83-
if (result.isLeft()) {
84-
error = result.getLeft();
85-
} else {
86-
marks = result.get();
87-
}
88-
}
89-
9037
}

‎vavr/src/test/java/com/baeldung/vavr/PatternMatchingUnitTest.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,11 @@ public void whenMatchCreatesSideEffects_thenCorrect() {
100100
})));
101101
}
102102

103-
104-
105-
public void displayEven() {
103+
private void displayEven() {
106104
System.out.println("Input is even");
107105
}
108106

109-
public void displayOdd() {
107+
private void displayOdd() {
110108
System.out.println("Input is odd");
111109
}
112110
}

‎vavr/src/test/java/com/baeldung/vavr/either/EitherUnitTest.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.baeldung.vavr.either;
22

3-
import org.junit.Test;
4-
53
import io.vavr.control.Either;
4+
import org.junit.Test;
65

76
import static org.junit.Assert.assertEquals;
87

@@ -11,21 +10,28 @@ public class EitherUnitTest {
1110
@Test
1211
public void givenMarks_whenPassNumber_thenExpectNumber() {
1312
Either<String, Integer> result = EitherDemo.computeWithEither(100);
14-
int marks = EitherDemo.getMarks(result);
13+
int marks = result.right()
14+
.getOrElseThrow(x -> new IllegalStateException());
15+
1516
assertEquals(100, marks);
1617
}
1718

1819
@Test
1920
public void givenMarks_whenFailNumber_thenExpectErrorMesssage() {
2021
Either<String, Integer> result = EitherDemo.computeWithEither(50);
21-
String error = EitherDemo.getError(result);
22+
String error = result.left()
23+
.getOrNull();
24+
2225
assertEquals("Marks not acceptable", error);
2326
}
2427

2528
@Test
2629
public void givenPassMarks_whenModified_thenExpectNumber() {
2730
Either<String, Integer> result = EitherDemo.computeWithEither(90);
28-
int marks = EitherDemo.getModifiedMarks(result);
31+
int marks = result.right()
32+
.map(x -> x * 2)
33+
.get();
34+
2935
assertEquals(180, marks);
3036
}
3137

0 commit comments

Comments
 (0)