|
| 1 | +import java.util.Collection; |
| 2 | +import java.util.Objects; |
| 3 | +import java.util.function.Consumer; |
| 4 | +import java.util.function.Function; |
| 5 | +import org.apache.commons.collections4.Predicate; |
| 6 | + |
| 7 | +final class FunctorUtils { |
| 8 | + |
| 9 | + private static <T> T[] clone(final T... array) { |
| 10 | + return array != null ? array.clone() : null; |
| 11 | + } |
| 12 | + |
| 13 | + static <R extends java.util.function.Predicate<T>, P extends java.util.function.Predicate<? super T>, T> R coerce(final P predicate) { |
| 14 | + return (R) predicate; |
| 15 | + } |
| 16 | + |
| 17 | + static <R extends Function<I, O>, P extends Function<? super I, ? extends O>, I, O> R coerce(final P transformer) { |
| 18 | + return (R) transformer; |
| 19 | + } |
| 20 | + |
| 21 | + static <T extends Consumer<?>> T[] copy(final T... consumers) { |
| 22 | + return clone(consumers); |
| 23 | + } |
| 24 | + |
| 25 | + static <T extends java.util.function.Predicate<?>> T[] copy(final T... predicates) { |
| 26 | + return clone(predicates); |
| 27 | + } |
| 28 | + |
| 29 | + static <T extends Function<?, ?>> T[] copy(final T... transformers) { |
| 30 | + return clone(transformers); |
| 31 | + } |
| 32 | + |
| 33 | + static <T> Predicate<? super T>[] validate(final Collection<? extends java.util.function.Predicate<? super T>> predicates) { |
| 34 | + Objects.requireNonNull(predicates, "predicates"); |
| 35 | + // convert to array like this to guarantee iterator() ordering |
| 36 | + @SuppressWarnings("unchecked") // OK |
| 37 | + final Predicate<? super T>[] preds = new Predicate[predicates.size()]; |
| 38 | + int i = 0; |
| 39 | + for (final java.util.function.Predicate<? super T> predicate : predicates) { |
| 40 | + preds[i] = (Predicate<? super T>) predicate; |
| 41 | + if (preds[i] == null) { |
| 42 | + throw new NullPointerException("predicates[" + i + "]"); |
| 43 | + } |
| 44 | + i++; |
| 45 | + } |
| 46 | + return preds; |
| 47 | + } |
| 48 | + |
| 49 | + static void validate(final Consumer<?>... consumers) { |
| 50 | + Objects.requireNonNull(consumers, "consumers"); |
| 51 | + for (int i = 0; i < consumers.length; i++) { |
| 52 | + if (consumers[i] == null) { |
| 53 | + throw new NullPointerException("closures[" + i + "]"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + static void validate(final Function<?, ?>... functions) { |
| 59 | + Objects.requireNonNull(functions, "functions"); |
| 60 | + for (int i = 0; i < functions.length; i++) { |
| 61 | + if (functions[i] == null) { |
| 62 | + throw new NullPointerException("functions[" + i + "]"); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + static void validate(final java.util.function.Predicate<?>... predicates) { |
| 68 | + Objects.requireNonNull(predicates, "predicates"); |
| 69 | + for (int i = 0; i < predicates.length; i++) { |
| 70 | + if (predicates[i] == null) { |
| 71 | + throw new NullPointerException("predicates[" + i + "]"); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments