/** * 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<E>(false, cmds); }
/** * Create a new Closure that calls one of the closures depending * on the predicates. * <p> * The Map consists of Predicate keys and Closure values. A closure * is called if its matching predicate returns true. Each predicate is evaluated * until one returns true. If no predicates evaluate to true, the default * closure is called. The default closure is set in the map with a * null key. The ordering is that of the iterator() method on the entryset * collection of the map. * * @param <E> the type that the closure acts on * @param predicatesAndClosures a map of predicates to closures * @return the <code>switch</code> closure * @throws NullPointerException if the map is null * @throws NullPointerException if any closure in the map is null * @throws ClassCastException if the map elements are of the wrong type */ @SuppressWarnings("unchecked") public static <E> Closure<E> switchClosure(final Map<Predicate<E>, Closure<E>> predicatesAndClosures) { if (predicatesAndClosures == null) { throw new NullPointerException("The predicate and closure map must not be null"); } // convert to array like this to guarantee iterator() ordering final Closure<? super E> defaultClosure = predicatesAndClosures.remove(null); final int size = predicatesAndClosures.size(); if (size == 0) { return (Closure<E>) (defaultClosure == null ? NOPClosure.<E>nopClosure() : defaultClosure); } final Closure<E>[] closures = new Closure[size]; final Predicate<E>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<Predicate<E>, Closure<E>> entry : predicatesAndClosures.entrySet()) { preds[i] = entry.getKey(); closures[i] = entry.getValue(); i++; } return new SwitchClosure<E>(false, preds, closures, defaultClosure); }
/** * A tag processor template method which takes as input a closure that is * responsible for extracting the information from the tag and saving it to * the database. The contents of the closure is called inside the * START_DOCUMENT case of the template code. * * @param parser * a reference to our StAX XMLStreamReader. * @param tagProcessor * a reference to the Closure to process the tag. * @throws Exception * if one is thrown. */ private void processTag(XMLStreamReader parser, Closure<XMLStreamReader> tagProcessor) throws Exception { int depth = 0; int event = parser.getEventType(); String startTag = formatTag(parser.getName()); FOR_LOOP: for (;;) { switch (event) { case XMLStreamConstants.START_ELEMENT: String tagName = formatTag(parser.getName()); tagProcessor.execute(parser); depth++; break; case XMLStreamConstants.END_ELEMENT: tagName = formatTag(parser.getName()); depth--; if (tagName.equals(startTag) && depth == 0) { break FOR_LOOP; } break; default: break; } event = parser.next(); } }
/** * Factory method that performs validation. * * @param <E> the type that the closure acts on * @param predicate predicate to switch on * @param trueClosure closure used if true * @param falseClosure closure used if false * @return the <code>if</code> closure * @throws NullPointerException if any argument is null */ public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure, final Closure<? super E> falseClosure) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } if (trueClosure == null || falseClosure == null) { throw new NullPointerException("Closures must not be null"); } return new IfClosure<E>(predicate, trueClosure, falseClosure); }
/** * Clone the closures to ensure that the internal reference can't be messed with. * * @param closures the closures to copy * @return the cloned closures */ @SuppressWarnings("unchecked") static <E> Closure<E>[] copy(final Closure<? super E>... closures) { if (closures == null) { return null; } return (Closure<E>[]) closures.clone(); }
/** * Validate the closures to ensure that all is well. * * @param closures the closures to validate */ static void validate(final Closure<?>... closures) { if (closures == null) { throw new NullPointerException("The closure array must not be null"); } for (int i = 0; i < closures.length; i++) { if (closures[i] == null) { throw new NullPointerException( "The closure array must not contain a null closure, index " + i + " was null"); } } }
/** * Factory method that performs validation. * * @param <E> the type that the closure acts on * @param predicate the predicate used to evaluate when the loop terminates, not null * @param closure the closure the execute, not null * @param doLoop true to act as a do-while loop, always executing the closure once * @return the <code>while</code> closure * @throws NullPointerException if the predicate or closure is null */ public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure, final boolean doLoop) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } if (closure == null) { throw new NullPointerException("Closure must not be null"); } return new WhileClosure<E>(predicate, closure, doLoop); }
/** * 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<E>(predicates, closures, defaultClosure); }
/** * Hidden constructor for the use by the static factory methods. * * @param clone if {@code true} the input arguments will be cloned * @param predicates array of predicates, no nulls * @param closures matching array of closures, no nulls * @param defaultClosure the closure to use if no match, null means nop */ @SuppressWarnings("unchecked") private SwitchClosure(final boolean clone, final Predicate<? super E>[] predicates, final Closure<? super E>[] closures, final Closure<? super E> defaultClosure) { super(); iPredicates = clone ? FunctorUtils.copy(predicates) : predicates; iClosures = clone ? FunctorUtils.copy(closures) : closures; iDefault = (Closure<? super E>) (defaultClosure == null ? NOPClosure.<E>nopClosure() : defaultClosure); }
public void test() { List<A> aList = new ArrayList<A>(); A a = new A(1); aList.add(a); aList.add(new A(2)); CollectionUtils.forAllDo(aList, new Closure<A>() { @Override public void execute(A input) { input.setId(5); } }); System.out.println(aList); }
private static <T>Closure<T> generateIOExceptionClosure() { return new CatchAndRethrowClosure<T>() { @Override protected void executeAndThrow(final T input) throws IOException { throw new IOException(); } }; }
private static <T>Closure<T> generateNullPointerExceptionClosure() { return new CatchAndRethrowClosure<T>() { @Override protected void executeAndThrow(final T input) { throw new NullPointerException(); } }; }
private static <T>Closure<T> generateNoExceptionClosure() { return new CatchAndRethrowClosure<T>() { @Override protected void executeAndThrow(final T input) { } }; }
@Test public void closureSanityTests() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "closureSanityTests"); final Closure<?> closure = generateClosure(); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),4410,closure); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
private static Map<Integer, String> inspectTypes() { Collection<Field> fields = CollectionUtils.select(Arrays.asList( java.sql.Types.class.getFields() ), (Predicate<? super Field>) new Predicate<Field>() { @Override public boolean evaluate(Field f) { return Modifier.isStatic(f.getModifiers()) && f.getType().getName().equals("int"); } }); final HashMap<Integer, String> intToName = new HashMap<Integer, String>( 0 ); CollectionUtils.forAllDo(fields, (Closure<? super Field>) new Closure<Field>() { @Override public void execute(Field arg0) { try { String name = arg0.getName(); Integer val; val = (Integer)( arg0.get(java.sql.Types.class) ); intToName.put(val, name ); } catch (IllegalArgumentException | IllegalAccessException e) { // ok, we cannot access it } } }); return intToName; }
/** * Execute a list of closures. * * @param input the input object passed to each closure */ public void execute(final E input) { for (final Closure<? super E> iClosure : iClosures) { iClosure.execute(input); } }
@Override protected <T>Closure<T> generateClosure() { return CatchAndRethrowClosureTest.generateNoExceptionClosure(); }
/** * Factory method that performs validation. * <p> * A null closure or zero count returns the <code>NOPClosure</code>. * A count of one returns the specified closure. * * @param <E> the type that the closure acts on * @param count the number of times to execute the closure * @param closure the closure to execute, not null * @return the <code>for</code> closure */ @SuppressWarnings("unchecked") public static <E> Closure<E> forClosure(final int count, final Closure<? super E> closure) { if (count <= 0 || closure == null) { return NOPClosure.<E>nopClosure(); } if (count == 1) { return (Closure<E>) closure; } return new ForClosure<E>(count, closure); }
/** * Constructor that performs no validation. * Use <code>ifClosure</code> if you want that. * * @param predicate predicate to switch on, not null * @param trueClosure closure used if true, not null * @param falseClosure closure used if false, not null */ public IfClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure, final Closure<? super E> falseClosure) { super(); iPredicate = predicate; iTrueClosure = trueClosure; iFalseClosure = falseClosure; }
/** * Factory method that performs validation. * <p> * A null transformer will return the <code>NOPClosure</code>. * * @param <E> the type that the closure acts on * @param transformer the transformer to call, null means nop * @return the <code>transformer</code> closure */ public static <E> Closure<E> transformerClosure(final Transformer<? super E, ?> transformer) { if (transformer == null) { return NOPClosure.<E>nopClosure(); } return new TransformerClosure<E>(transformer); }
/** * 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<E>(closures); }
/** * Constructor that performs no validation. * Use <code>whileClosure</code> if you want that. * * @param predicate the predicate used to evaluate when the loop terminates, not null * @param closure the closure the execute, not null * @param doLoop true to act as a do-while loop, always executing the closure once */ public WhileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure, final boolean doLoop) { super(); iPredicate = predicate; iClosure = closure; iDoLoop = doLoop; }
/** * Factory method that performs validation. * * @param <T> the type of the object to transform * @param closure the closure to call, not null * @return the <code>closure</code> transformer * @throws NullPointerException if the closure is null */ public static <T> Transformer<T, T> closureTransformer(final Closure<? super T> closure) { if (closure == null) { throw new NullPointerException("Closure must not be null"); } return new ClosureTransformer<T>(closure); }
/** * Factory returning the singleton instance. * * @param <E> the type that the closure acts on * @return the singleton instance * @since 3.1 */ @SuppressWarnings("unchecked") public static <E> Closure<E> nopClosure() { return (Closure<E>) INSTANCE; }
/** * Factory returning the singleton instance. * * @param <E> the type that the closure acts on * @return the singleton instance * @since 3.1 */ @SuppressWarnings("unchecked") // the static instance works for all types public static <E> Closure<E> exceptionClosure() { return (Closure<E>) INSTANCE; }