Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ee08d4a

Browse files
committedJul 22, 2021
move static methods to Eithers, add toOptionalList
1 parent 8838863 commit ee08d4a

File tree

8 files changed

+135
-74
lines changed

8 files changed

+135
-74
lines changed
 

‎src/main/java/io/jbock/util/Either.java

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package io.jbock.util;
22

3-
import java.util.List;
43
import java.util.Optional;
54
import java.util.function.Consumer;
65
import java.util.function.Function;
7-
import java.util.stream.Collector;
86

97
/**
108
* A class that acts as a container for a value of one of two types. An Either
@@ -49,56 +47,6 @@ public static <L, R> Either<L, R> right(R value) {
4947
return new Right<>(value);
5048
}
5149

52-
/**
53-
* Returns a collector that accumulates a Right containing all values in the original order,
54-
* if there are no Left instances in the stream.
55-
* If the stream contains a Left, it accumulates a Left containing the first LHS value in the stream.
56-
*
57-
* @param <L> the LHS type
58-
* @param <R> the RHS type
59-
* @return a Right containing all RHS values in the stream,
60-
* or, if an LHS value exists, a Left containing the first such value
61-
*/
62-
public static <L, R> Collector<Either<? extends L, ? extends R>, ?, Either<L, List<R>>> toValidList() {
63-
return new ValidatingCollector<>();
64-
}
65-
66-
/**
67-
* Returns a collector that accumulates a Right containing all values in the original order,
68-
* if there are no Left instances in the stream.
69-
* If the stream contains a Left, it accumulates a Left containing all LHS values in the stream,
70-
* in the original order.
71-
*
72-
* @param <L> the LHS type
73-
* @param <R> the RHS type
74-
* @return a list of the RHS values in the stream,
75-
* or, if an LHS value exists, a nonempty list of all LHS values
76-
*/
77-
public static <L, R> Collector<Either<? extends L, ? extends R>, ?, Either<List<L>, List<R>>> toValidListAll() {
78-
return new ValidatingCollectorAll<>();
79-
}
80-
81-
/**
82-
* If the provided list is empty, returns an empty {@link Optional}.
83-
* Otherwise, returns an {@code Optional} containing the list.
84-
*
85-
* <p>This utility method can sometimes be used to express a
86-
* {@link #filter(Function)} operation more efficiently.
87-
*
88-
* @param values a list of objects
89-
* @param <T> the type of the members of {@code values}
90-
* @return an {@code Optional} which is empty if and only if {@code values}
91-
* is empty
92-
*/
93-
public static <T> Optional<List<T>> optionalList(List<? extends T> values) {
94-
if (values.isEmpty()) {
95-
return Optional.empty();
96-
}
97-
@SuppressWarnings("unchecked")
98-
List<T> result = (List<T>) values;
99-
return Optional.of(result);
100-
}
101-
10250
/**
10351
* If this is a Right, returns a Right containing the result of applying
10452
* the mapper function to the RHS value.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package io.jbock.util;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.function.Function;
6+
import java.util.stream.Collector;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* This class contains static utility methods related to
11+
* the {@link Either} type.
12+
*/
13+
public final class Eithers {
14+
15+
private Eithers() {
16+
}
17+
18+
/**
19+
* Returns a {@code Collector} that accumulates the input elements into
20+
* a Right containing all values in the original order,
21+
* but only if there are no Left instances in the stream.
22+
* If the stream does contain a Left instance, it discards the Right instances and
23+
* accumulates a Left instance, which contains the first LHS value in the stream,
24+
* in encounter order.
25+
*
26+
* @param <L> the type of the LHS values in the stream
27+
* @param <R> the type of the RHS values in the stream
28+
* @return a {@code Collector} which collects all the input elements into
29+
* a Right containing all RHS values in the stream, or,
30+
* if an LHS value exists, a Left containing the first LHS value
31+
*/
32+
public static <L, R> Collector<Either<? extends L, ? extends R>, ?, Either<L, List<R>>> toValidList() {
33+
return new ValidatingCollector<>();
34+
}
35+
36+
/**
37+
* Returns a {@code Collector} that accumulates the input elements into
38+
* a Right containing all values in the original order,
39+
* but only if there are no Left instances in the stream.
40+
* If the stream does contain a Left instance, it discards the Right instances and
41+
* accumulates a Left containing only the LHS values,
42+
* in encounter order.
43+
*
44+
* @param <L> the type of the LHS values in the stream
45+
* @param <R> the type of the RHS values in the stream
46+
* @return a {@code Collector} which collects all the input elements into
47+
* a Right containing all RHS values in the stream,
48+
* or, if an LHS value exists, a Left containing a nonempty list
49+
* of all LHS values in the stream
50+
*/
51+
public static <L, R> Collector<Either<? extends L, ? extends R>, ?, Either<List<L>, List<R>>> toValidListAll() {
52+
return new ValidatingCollectorAll<>();
53+
}
54+
55+
/**
56+
* Returns a {@code Collector} that accumulates the input elements into
57+
* a new {@code List}. There are no guarantees on the type, mutability,
58+
* serializability, or thread-safety of the {@code List} returned.
59+
* The resulting list is wrapped in an {@code Optional},
60+
* which is empty if and only if the list is empty.
61+
*
62+
* @see #optionalList(List)
63+
* @param <T> the type of the input elements
64+
* @return a list of the RHS values in the stream,
65+
* or, if an LHS value exists, a nonempty list of all LHS values
66+
*/
67+
public static <T> Collector<T, ?, Optional<List<T>>> toOptionalList() {
68+
return Collectors.collectingAndThen(
69+
Collectors.toList(),
70+
Eithers::optionalList);
71+
}
72+
73+
/**
74+
* If the provided list is empty, returns an empty {@link Optional}.
75+
* Otherwise, returns an {@code Optional} containing the nonempty
76+
* input list.
77+
*
78+
* <p>Note: The resulting {@code Optional} might be used in a
79+
* {@link Either#filter(Function) filter} or
80+
* {@link Either#filterLeft(Function) filterLeft} operation.
81+
*
82+
* @see #toOptionalList()
83+
* @param values a list of objects
84+
* @param <T> the type of the members of {@code values}
85+
* @return an {@code Optional} which is either empty, or
86+
* contains a nonempty list
87+
*/
88+
public static <T> Optional<List<T>> optionalList(List<? extends T> values) {
89+
if (values.isEmpty()) {
90+
return Optional.empty();
91+
}
92+
@SuppressWarnings("unchecked")
93+
List<T> result = (List<T>) values;
94+
return Optional.of(result);
95+
}
96+
}

‎src/main/java/io/jbock/util/ValidatingCollector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.stream.Collector;
1111

1212
/**
13-
* Internal implementation of {@link Either#toValidList()}.
13+
* Internal implementation of {@link Eithers#toValidList()}.
1414
*
1515
* @param <L> the type of the LHS values in the stream
1616
* @param <R> the type of the RHS values in the stream

‎src/main/java/io/jbock/util/ValidatingCollectorAll.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.stream.Collector;
1111

1212
/**
13-
* Internal implementation of {@link Either#toValidListAll()}.
13+
* Internal implementation of {@link Eithers#toValidListAll()}.
1414
*
1515
* @param <L> the type of the LHS values in the stream
1616
* @param <R> the type of the RHS values in the stream

‎src/test/java/io/jbock/util/EitherTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.junit.jupiter.api.Test;
55

66
import java.io.IOException;
7-
import java.util.List;
87
import java.util.Objects;
98
import java.util.Optional;
109

@@ -135,11 +134,4 @@ void testOrElseThrow() {
135134
Either<String, String> right = Either.right("2");
136135
assertEquals("2", right.orElseThrow(IllegalArgumentException::new));
137136
}
138-
139-
@Test
140-
void testOptionalList() {
141-
assertEquals(Optional.empty(), Either.optionalList(List.of()));
142-
assertEquals(Optional.of(List.of(1)), Either.optionalList(List.of(1)));
143-
assertEquals(Optional.of(List.of("1", "2")), Either.optionalList(List.of("1", "2")));
144-
}
145137
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.jbock.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
import java.util.stream.Stream;
8+
9+
import static io.jbock.util.Eithers.optionalList;
10+
import static io.jbock.util.Eithers.toOptionalList;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
14+
class EithersTest {
15+
16+
@Test
17+
void testOptionalList() {
18+
assertEquals(Optional.empty(), optionalList(List.of()));
19+
assertEquals(Optional.of(List.of(1)), optionalList(List.of(1)));
20+
}
21+
22+
@Test
23+
void testToValidList() {
24+
assertTrue(Eithers.toValidList() instanceof ValidatingCollector);
25+
}
26+
27+
@Test
28+
void testToValidListAll() {
29+
assertTrue(Eithers.toValidListAll() instanceof ValidatingCollectorAll);
30+
}
31+
32+
@Test
33+
void testToOptionalList() {
34+
assertEquals(Optional.empty(), Stream.of().collect(toOptionalList()));
35+
assertEquals(Optional.of(List.of(1)), Stream.of(1).collect(toOptionalList()));
36+
}
37+
}

‎src/test/java/io/jbock/util/ValidatingCollectorAllTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import static io.jbock.util.Either.left;
88
import static io.jbock.util.Either.right;
99
import static org.junit.jupiter.api.Assertions.assertEquals;
10-
import static org.junit.jupiter.api.Assertions.assertTrue;
1110

1211
class ValidatingCollectorAllTest {
1312

@@ -32,11 +31,6 @@ void testRight() {
3231
assertEquals(right(List.of(1, 2, 3)), apply(List.of(right(1), right(2), right(3))));
3332
}
3433

35-
@Test
36-
void testFactoryMethod() {
37-
assertTrue(Either.toValidList() instanceof ValidatingCollector);
38-
}
39-
4034
private Either<List<String>, List<Integer>> apply(List<Either<String, Integer>> data) {
4135
return data.stream().collect(new ValidatingCollectorAll<>());
4236
}

‎src/test/java/io/jbock/util/ValidatingCollectorTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import static io.jbock.util.Either.left;
88
import static io.jbock.util.Either.right;
99
import static org.junit.jupiter.api.Assertions.assertEquals;
10-
import static org.junit.jupiter.api.Assertions.assertTrue;
1110

1211
class ValidatingCollectorTest {
1312

@@ -32,11 +31,6 @@ void testRight() {
3231
assertEquals(right(List.of(1, 2, 3)), apply(List.of(right(1), right(2), right(3))));
3332
}
3433

35-
@Test
36-
void testFactoryMethod() {
37-
assertTrue(Either.toValidListAll() instanceof ValidatingCollectorAll);
38-
}
39-
4034
private Either<String, List<Integer>> apply(List<Either<String, Integer>> data) {
4135
return data.stream().collect(new ValidatingCollector<>());
4236
}

0 commit comments

Comments
 (0)
Please sign in to comment.