|
| 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 | +} |
0 commit comments