/** * 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); } }
/** * Transforms the input to result by executing a closure. * * @param input the input object to transform * @return the transformed result */ public T transform(final T input) { iClosure.execute(input); return input; }
/** * Executes the closure <code>count</code> times. * * @param input the input object */ @Override public void execute(final E input) { for (int i = 0; i < iCount; i++) { iClosure.execute(input); } }
/** * Applies the closure to each element of the provided iterator. * * @param <E> the element type * @param iterator the iterator to use, may be null * @param closure the closure to apply to each element, may not be null * @throws NullPointerException if closure is null * @since 4.1 */ public static <E> void forEach(final Iterator<E> iterator, final Closure<? super E> closure) { if (closure == null) { throw new NullPointerException("Closure must not be null"); } if (iterator != null) { while (iterator.hasNext()) { final E element = iterator.next(); closure.execute(element); } } }
/** * Executes the given closure on each but the last element in the iterator. * <p> * If the input iterator is null no change is made. * * @param <E> the type of object the {@link Iterator} contains * @param iterator the iterator to get the input from, may be null * @param closure the closure to perform, may not be null * @return the last element in the iterator, or null if iterator is null or empty * @throws NullPointerException if closure is null * @since 4.1 */ public static <E> E forEachButLast(final Iterator<E> iterator, final Closure<? super E> closure) { if (closure == null) { throw new NullPointerException("Closure must not be null."); } if (iterator != null) { while (iterator.hasNext()) { final E element = iterator.next(); if (iterator.hasNext()) { closure.execute(element); } else { return element; } } } return null; }