/** * Factory to create the predicate. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>one</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ public static <T> Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates) { final Predicate<? super T>[] preds = FunctorUtils.validate(predicates); return new OnePredicate<>(preds); }
/** * Factory method that performs validation and copies the parameter arrays. * * @param <E> the type that the closure acts on * @param predicates array of predicates, cloned, no nulls * @param closures matching array of closures, cloned, no nulls * @param defaultClosure the closure to use if no match, null means nop * @return the <code>chained</code> closure * @throws NullPointerException if array is null * @throws NullPointerException if any element in the array is null * @throws IllegalArgumentException if the array lengths of predicates and closures do not match */ @SuppressWarnings("unchecked") public static <E> Closure<E> switchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures, final Closure<? super E> defaultClosure) { FunctorUtils.validate(predicates); FunctorUtils.validate(closures); if (predicates.length != closures.length) { throw new IllegalArgumentException("The predicate and closure arrays must be the same size"); } if (predicates.length == 0) { return (Closure<E>) (defaultClosure == null ? NOPClosure.<E>nopClosure(): defaultClosure); } return new SwitchClosure<>(predicates, closures, defaultClosure); }
/** * Factory method that performs validation and copies the parameter arrays. * * @param <I> the input type * @param <O> the output type * @param predicates array of predicates, cloned, no nulls * @param transformers matching array of transformers, cloned, no nulls * @param defaultTransformer the transformer to use if no match, null means return null * @return the <code>chained</code> transformer * @throws NullPointerException if array is null * @throws NullPointerException if any element in the array is null */ @SuppressWarnings("unchecked") public static <I, O> Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates, final Transformer<? super I, ? extends O>[] transformers, final Transformer<? super I, ? extends O> defaultTransformer) { FunctorUtils.validate(predicates); FunctorUtils.validate(transformers); if (predicates.length != transformers.length) { throw new IllegalArgumentException("The predicate and transformer arrays must be the same size"); } if (predicates.length == 0) { return (Transformer<I, O>) (defaultTransformer == null ? ConstantTransformer.<I, O>nullTransformer() : defaultTransformer); } return new SwitchTransformer<>(predicates, transformers, defaultTransformer); }
/** * Factory method that performs validation and copies the parameter array. * * @param <E> the type that the closure acts on * @param closures the closures to chain, copied, no nulls * @return the <code>chained</code> closure * @throws NullPointerException if the closures array is null * @throws NullPointerException if any closure in the array is null */ public static <E> Closure<E> chainedClosure(final Closure<? super E>... closures) { FunctorUtils.validate(closures); if (closures.length == 0) { return NOPClosure.<E>nopClosure(); } return new ChainedClosure<>(closures); }
/** * Create a new Transformer that calls each transformer in turn, passing the * result into the next transformer. The ordering is that of the iterator() * method on the collection. * * @param <T> the object type * @param transformers a collection of transformers to chain * @return the <code>chained</code> transformer * @throws NullPointerException if the transformers collection is null * @throws NullPointerException if any transformer in the collection is null */ @SuppressWarnings("unchecked") public static <T> Transformer<T, T> chainedTransformer( final Collection<? extends Transformer<? super T, ? extends T>> transformers) { if (transformers == null) { throw new NullPointerException("Transformer collection must not be null"); } if (transformers.size() == 0) { return NOPTransformer.<T>nopTransformer(); } // convert to array like this to guarantee iterator() ordering final Transformer<T, T>[] cmds = transformers.toArray(new Transformer[transformers.size()]); FunctorUtils.validate(cmds); return new ChainedTransformer<>(false, cmds); }
/** * Factory method that performs validation and copies the parameter array. * * @param <T> the object type * @param transformers the transformers to chain, copied, no nulls * @return the <code>chained</code> transformer * @throws NullPointerException if the transformers array is null * @throws NullPointerException if any transformer in the array is null */ public static <T> Transformer<T, T> chainedTransformer(final Transformer<? super T, ? extends T>... transformers) { FunctorUtils.validate(transformers); if (transformers.length == 0) { return NOPTransformer.<T>nopTransformer(); } return new ChainedTransformer<>(transformers); }
/** * Create a new Closure that calls each closure in turn, passing the * result into the next closure. The ordering is that of the iterator() * method on the collection. * * @param <E> the type that the closure acts on * @param closures a collection of closures to chain * @return the <code>chained</code> closure * @throws NullPointerException if the closures collection is null * @throws NullPointerException if any closure in the collection is null */ @SuppressWarnings("unchecked") public static <E> Closure<E> chainedClosure(final Collection<? extends Closure<? super E>> closures) { if (closures == null) { throw new NullPointerException("Closure collection must not be null"); } if (closures.size() == 0) { return NOPClosure.<E>nopClosure(); } // convert to array like this to guarantee iterator() ordering final Closure<? super E>[] cmds = new Closure[closures.size()]; int i = 0; for (final Closure<? super E> closure : closures) { cmds[i++] = closure; } FunctorUtils.validate(cmds); return new ChainedClosure<>(false, cmds); }
/** * Factory to create the predicate. * <p> * If the collection is size zero, the predicate always returns true. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>one</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ public static <T> Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates) { final Predicate<? super T>[] preds = FunctorUtils.validate(predicates); if (preds.length == 0) { return TruePredicate.<T>truePredicate(); } return new NonePredicate<>(preds); }
/** * Factory to create the predicate. * <p> * If the collection is size zero, the predicate always returns false. * If the collection is size one, then that predicate is returned. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>all</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ @SuppressWarnings("unchecked") public static <T> Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates) { final Predicate<? super T>[] preds = FunctorUtils.validate(predicates); if (preds.length == 0) { return FalsePredicate.<T>falsePredicate(); } if (preds.length == 1) { return (Predicate<T>) preds[0]; } return new AnyPredicate<>(preds); }
/** * Factory to create the predicate. * <p> * If the array is size zero, the predicate always returns true. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>any</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ public static <T> Predicate<T> nonePredicate(final Predicate<? super T>... predicates) { FunctorUtils.validate(predicates); if (predicates.length == 0) { return TruePredicate.<T>truePredicate(); } return new NonePredicate<>(FunctorUtils.copy(predicates)); }
/** * Factory to create the predicate. * <p> * If the array is size zero, the predicate always returns false. * If the array is size one, then that predicate is returned. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>any</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ @SuppressWarnings("unchecked") public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates) { FunctorUtils.validate(predicates); if (predicates.length == 0) { return FalsePredicate.<T>falsePredicate(); } if (predicates.length == 1) { return (Predicate<T>) predicates[0]; } return new OnePredicate<>(FunctorUtils.copy(predicates)); }
/** * Factory to create the predicate. * <p> * If the collection is size zero, the predicate always returns true. * If the collection is size one, then that predicate is returned. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>all</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ public static <T> Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates) { final Predicate<? super T>[] preds = validate(predicates); if (preds.length == 0) { return truePredicate(); } if (preds.length == 1) { return coerce(preds[0]); } return new AllPredicate<>(preds); }
/** * Factory to create the predicate. * <p> * If the array is size zero, the predicate always returns false. * If the array is size one, then that predicate is returned. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>any</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ @SuppressWarnings("unchecked") public static <T> Predicate<T> anyPredicate(final Predicate<? super T>... predicates) { FunctorUtils.validate(predicates); if (predicates.length == 0) { return FalsePredicate.<T>falsePredicate(); } if (predicates.length == 1) { return (Predicate<T>) predicates[0]; } return new AnyPredicate<>(FunctorUtils.copy(predicates)); }
/** * Factory to create the predicate. * <p> * If the array is size zero, the predicate always returns true. * If the array is size one, then that predicate is returned. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>all</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ public static <T> Predicate<T> allPredicate(final Predicate<? super T>... predicates) { FunctorUtils.validate(predicates); if (predicates.length == 0) { return truePredicate(); } if (predicates.length == 1) { return coerce(predicates[0]); } return new AllPredicate<>(FunctorUtils.copy(predicates)); }