Skip to content

Commit f876c6d

Browse files
AbhinabKanrarpivovarit
authored andcommitted
vavr's either (eugenp#2180)
* moving jmh into libraries module * refactoring jmh * Update pom.xml * manual algorightm * with BM result * fix for space issue * Fixed indentation * change as per suggestion * vavr either * adding unit test and othe rutilities
1 parent 62254d9 commit f876c6d

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.vavr.either;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import io.vavr.control.Either;
7+
8+
public class EitherDemo {
9+
10+
public static Object[] computeWithoutEitherUsingArray(int marks) {
11+
Object[] results = new Object[2];
12+
if (marks < 85) {
13+
results[0] = "Marks not acceptable";
14+
} else {
15+
results[1] = marks;
16+
}
17+
return results;
18+
}
19+
20+
public static Map<String, Object> computeWithoutEitherUsingMap(int marks) {
21+
Map<String, Object> results = new HashMap<String, Object>();
22+
if (marks < 85) {
23+
results.put("FAILURE", "Marks not acceptable");
24+
} else {
25+
results.put("SUCCESS", marks);
26+
}
27+
return results;
28+
}
29+
30+
public static Either<String, Integer> computeWithEither(int marks) {
31+
if (marks < 85) {
32+
return Either.left("Marks not acceptable");
33+
} else {
34+
return Either.right(marks);
35+
}
36+
}
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+
90+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.vavr.either;
2+
3+
import org.junit.Test;
4+
5+
import io.vavr.control.Either;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class EitherUnitTest {
10+
11+
@Test
12+
public void givenMarks_whenPassNumber_thenExpectNumber() {
13+
Either<String, Integer> result = EitherDemo.computeWithEither(100);
14+
int marks = EitherDemo.getMarks(result);
15+
assertEquals(100, marks);
16+
}
17+
18+
@Test
19+
public void givenMarks_whenFailNumber_thenExpectErrorMesssage() {
20+
Either<String, Integer> result = EitherDemo.computeWithEither(50);
21+
String error = EitherDemo.getError(result);
22+
assertEquals("Marks not acceptable", error);
23+
}
24+
25+
@Test
26+
public void givenPassMarks_whenModified_thenExpectNumber() {
27+
Either<String, Integer> result = EitherDemo.computeWithEither(90);
28+
int marks = EitherDemo.getModifiedMarks(result);
29+
assertEquals(180, marks);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)