Skip to content

Commit 2827d2c

Browse files
committed
add asLeftOptional, more type flexibility for toValidList
1 parent d2808bd commit 2827d2c

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static <L, R> Either<L, R> right(R value) {
5959
* @return a Right containing all RHS values in the stream,
6060
* or, if an LHS value exists, a Left containing the first such value
6161
*/
62-
public static <L, R> Collector<Either<L, R>, ?, Either<L, List<R>>> toValidList() {
62+
public static <L, R> Collector<Either<? extends L, ? extends R>, ?, Either<L, List<R>>> toValidList() {
6363
return new ValidatingCollector<>();
6464
}
6565

@@ -74,10 +74,28 @@ public static <L, R> Either<L, R> right(R value) {
7474
* @return a list of the RHS values in the stream,
7575
* or, if an LHS value exists, a nonempty list of all LHS values
7676
*/
77-
public static <L, R> Collector<Either<L, R>, ?, Either<List<L>, List<R>>> toValidListAll() {
77+
public static <L, R> Collector<Either<? extends L, ? extends R>, ?, Either<List<L>, List<R>>> toValidListAll() {
7878
return new ValidatingCollectorAll<>();
7979
}
8080

81+
/**
82+
* If the provided list is empty, returns an empty {@link Optional}.
83+
* Otherwise, returns an {@code Optional} containing the list.
84+
*
85+
* @param failures a failures
86+
* @param <L> the type of the members of the failures
87+
* @return an {@code Optional} which is empty if and only if {@code failures}
88+
* is empty
89+
*/
90+
public static <L> Optional<List<L>> asLeftOptional(List<? extends L> failures) {
91+
if (failures.isEmpty()) {
92+
return Optional.empty();
93+
}
94+
@SuppressWarnings("unchecked")
95+
List<L> result = (List<L>) failures;
96+
return Optional.of(result);
97+
}
98+
8199
/**
82100
* If this is a Right, returns a Right containing the result of applying
83101
* the mapper function to the RHS value.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
* @param <L> the type of the LHS values in the stream
1616
* @param <R> the type of the RHS values in the stream
1717
*/
18-
final class ValidatingCollector<L, R> implements Collector<Either<L, R>, ValidatingCollector.Acc<L, R>, Either<L, List<R>>> {
18+
final class ValidatingCollector<L, R> implements Collector<Either<? extends L, ? extends R>, ValidatingCollector.Acc<L, R>, Either<L, List<R>>> {
1919

2020
static final class Acc<L, R> {
2121

2222
private L left;
2323
private final List<R> right = new ArrayList<>();
2424

25-
void accumulate(Either<L, R> either) {
25+
void accumulate(Either<? extends L, ? extends R> either) {
2626
if (left != null) {
2727
return;
2828
}
@@ -56,7 +56,7 @@ public Supplier<Acc<L, R>> supplier() {
5656
}
5757

5858
@Override
59-
public BiConsumer<Acc<L, R>, Either<L, R>> accumulator() {
59+
public BiConsumer<Acc<L, R>, Either<? extends L, ? extends R>> accumulator() {
6060
return Acc::accumulate;
6161
}
6262

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
* @param <L> the type of the LHS values in the stream
1616
* @param <R> the type of the RHS values in the stream
1717
*/
18-
final class ValidatingCollectorAll<L, R> implements Collector<Either<L, R>, ValidatingCollectorAll.Acc<L, R>, Either<List<L>, List<R>>> {
18+
final class ValidatingCollectorAll<L, R> implements Collector<Either<? extends L, ? extends R>, ValidatingCollectorAll.Acc<L, R>, Either<List<L>, List<R>>> {
1919

2020
static final class Acc<L, R> {
2121

2222
private final List<L> left = new ArrayList<>();
2323
private final List<R> right = new ArrayList<>();
2424

25-
void accumulate(Either<L, R> either) {
25+
void accumulate(Either<? extends L, ? extends R> either) {
2626
if (!left.isEmpty()) {
2727
either.ifLeftOrElse(left::add, r -> {
2828
});
@@ -58,7 +58,7 @@ public Supplier<Acc<L, R>> supplier() {
5858
}
5959

6060
@Override
61-
public BiConsumer<Acc<L, R>, Either<L, R>> accumulator() {
61+
public BiConsumer<Acc<L, R>, Either<? extends L, ? extends R>> accumulator() {
6262
return Acc::accumulate;
6363
}
6464

0 commit comments

Comments
 (0)