/** * Iterates through this collection transforming each value into a new value using the <code>transform</code> closure * and adding it to the supplied <code>collector</code>. * <pre class="groovyTestCase">assert [1,2,3] as HashSet == [2,4,5,6].collect(new HashSet()) { (int)(it / 2) }</pre> * * @param self an Iterable * @param collector the Collection to which the transformed values are added * @param transform the closure used to transform each item * @return the collector with all transformed values added to it * @since 2.5.0 */ public static <S,T> Collection<T> collect(Iterable<S> self, Collection<T> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<? extends T> transform) { for (S item : self) { collector.add(transform.call(item)); if (transform.getDirective() == Closure.DONE) { break; } } return collector; }
/** * Executes the closure this many times, starting from zero. The current * index is passed to the closure each time. * Example: * <pre>10.times { * println it * }</pre> * Prints the numbers 0 through 9. * * @param self a Number * @param closure the closure to call a number of times * @since 1.0 */ public static void times(Number self, @ClosureParams(value=SimpleType.class,options="int") Closure closure) { for (int i = 0, size = self.intValue(); i < size; i++) { closure.call(i); if (closure.getDirective() == Closure.DONE) { break; } } }
/** * Iterates through this collection transforming each value into a new value using the <code>transform</code> closure * and adding it to the supplied <code>collector</code>. * <pre class="groovyTestCase">assert [1,2,3] as HashSet == [2,4,5,6].collect(new HashSet()) { (int)(it / 2) }</pre> * * @param self a collection * @param collector the Collection to which the transformed values are added * @param transform the closure used to transform each item of the collection * @return the collector with all transformed values added to it * @deprecated use the Iterable version instead * @since 1.0 */ @Deprecated public static <S,T> Collection<T> collect(Collection<S> self, Collection<T> collector, @ClosureParams(FirstParam.FirstGenericType.class) Closure<? extends T> transform) { for (S item : self) { collector.add(transform.call(item)); if (transform.getDirective() == Closure.DONE) { break; } } return collector; }
/** * Recursively iterates through this Iterable transforming each non-Collection value * into a new value using the <code>transform</code> closure. Returns a potentially nested * collection of transformed values. * <pre class="groovyTestCase"> * def x = [1,[2,3],[4],[]].collectNested(new Vector()) { it * 2 } * assert x == [2,[4,6],[8],[]] * assert x instanceof Vector * </pre> * * @param self an Iterable * @param collector an initial Collection to which the transformed values are added * @param transform the closure used to transform each element of the Iterable * @return the collector with all transformed values added to it * @since 2.2.0 */ public static Collection collectNested(Iterable self, Collection collector, Closure transform) { for (Object item : self) { if (item instanceof Collection) { Collection c = (Collection) item; collector.add(collectNested((Iterable)c, createSimilarCollection(collector, c.size()), transform)); } else { collector.add(transform.call(item)); } if (transform.getDirective() == Closure.DONE) { break; } } return collector; }
return getClass(); } else if ("directive".equals(property)) { return getDirective(); } else if ("resolveStrategy".equals(property)) { return getResolveStrategy();
@Override public int getDirective() { return this.target.getDirective(); }
/** * Iterates through this collection transforming each value into a new value using the <code>transform</code> closure * and adding it to the supplied <code>collector</code>. * <pre class="groovyTestCase">assert [1,2,3] as HashSet == [2,4,5,6].collect(new HashSet()) { (int)(it / 2) }</pre> * * @param self a collection * @param collector the Collection to which the transformed values are added * @param transform the closure used to transform each item of the collection * @return the collector with all transformed values added to it * @since 1.0 */ public static <T> Collection<T> collect(Collection<?> self, Collection<T> collector, Closure<? extends T> transform) { for (Object item : self) { collector.add(transform.call(item)); if (transform.getDirective() == Closure.DONE) { break; } } return collector; }
/** * Executes the closure this many times, starting from zero. The current * index is passed to the closure each time. * Example: * <pre>10.times { * println it * }</pre> * Prints the numbers 0 through 9. * * @param self a Number * @param closure the closure to call a number of times * @since 1.0 */ public static void times(Number self, Closure closure) { for (int i = 0, size = self.intValue(); i < size; i++) { closure.call(i); if (closure.getDirective() == Closure.DONE) { break; } } }
/** * Executes the closure this many times, starting from zero. The current * index is passed to the closure each time. * Example: * <pre>10.times { * println it * }</pre> * Prints the numbers 0 through 9. * * @param self a Number * @param closure the closure to call a number of times * @since 1.0 */ public static void times(Number self, Closure closure) { for (int i = 0, size = self.intValue(); i < size; i++) { closure.call(i); if (closure.getDirective() == Closure.DONE) { break; } } }
/** * Executes the closure this many times, starting from zero. The current * index is passed to the closure each time. * Example: * <pre>10.times { * println it * }</pre> * Prints the numbers 0 through 9. * * @param self a Number * @param closure the closure to call a number of times */ public static void times(Number self, Closure closure) { for (int i = 0, size = self.intValue(); i < size; i++) { closure.call(new Integer(i)); if (closure.getDirective() == Closure.DONE) { break; } } }
/** * Executes the closure this many times, starting from zero. The current * index is passed to the closure each time. * Example: * <pre>10.times { * println it * }</pre> * Prints the numbers 0 through 9. * * @param self a Number * @param closure the closure to call a number of times * @since 1.0 */ public static void times(Number self, Closure closure) { for (int i = 0, size = self.intValue(); i < size; i++) { closure.call(Integer.valueOf(i)); if (closure.getDirective() == Closure.DONE) { break; } } }
/** * Iterates through this collection transforming each value into a new value using the closure * as a transformer, returning an initial collection plus the transformed values. * * @param self a collection * @param collection an initial Collection to which the transformed values are added * @param closure the closure used to transform each element of the collection * @return the resulting collection of transformed values * @since 1.0 */ public static Collection collect(Collection self, Collection collection, Closure closure) { for (Iterator iter = self.iterator(); iter.hasNext();) { collection.add(closure.call(iter.next())); if (closure.getDirective() == Closure.DONE) { break; } } return collection; }
/** * Iterates through this collection transforming each value into a new value using the closure * as a transformer, returning an initial collection plus the transformed values. * * @param self a collection * @param collection an initial Collection to which the transformed values are added * @param closure the closure used to transform each element of the collection * @return the resulting collection of transformed values */ public static Collection collect(Collection self, Collection collection, Closure closure) { for (Iterator iter = self.iterator(); iter.hasNext();) { collection.add(closure.call(iter.next())); if (closure.getDirective() == Closure.DONE) { break; } } return collection; }
/** * Iterates through this collection transforming each value into a new value using the closure * as a transformer, returning an initial collection plus the transformed values. * * @param self a collection * @param collection an initial Collection to which the transformed values are added * @param closure the closure used to transform each element of the collection * @return the resulting collection of transformed values * @since 1.0 */ public static Collection collect(Collection self, Collection collection, Closure closure) { for (Iterator iter = self.iterator(); iter.hasNext();) { collection.add(closure.call(iter.next())); if (closure.getDirective() == Closure.DONE) { break; } } return collection; }
/** * Recursively iterates through this collection transforming each non-Collection value * into a new value using the closure as a transformer. Returns a potentially nested * collection of transformed values. * * @param self a collection * @param collection an initial Collection to which the transformed values are added * @param closure the closure used to transform each element of the collection * @return the resultant collection */ public static Collection collectAll(Collection self, Collection collection, Closure closure) { for (Iterator iter = self.iterator(); iter.hasNext();) { final Object o = iter.next(); if (o instanceof Collection) { Collection c = (Collection) o; collection.add(collectAll(c, createSimilarCollection(collection, c.size()), closure)); } else { collection.add(closure.call(o)); } if (closure.getDirective() == Closure.DONE) { break; } } return collection; }
/** * Recursively iterates through this collection transforming each non-Collection value * into a new value using the closure as a transformer. Returns a potentially nested * collection of transformed values. * * @param self a collection * @param collection an initial Collection to which the transformed values are added * @param closure the closure used to transform each element of the collection * @return the resultant collection * @since 1.5.2 */ public static Collection collectAll(Collection self, Collection collection, Closure closure) { for (Iterator iter = self.iterator(); iter.hasNext();) { final Object o = iter.next(); if (o instanceof Collection) { Collection c = (Collection) o; collection.add(collectAll(c, createSimilarCollection(collection, c.size()), closure)); } else { collection.add(closure.call(o)); } if (closure.getDirective() == Closure.DONE) { break; } } return collection; }
/** * Recursively iterates through this collection transforming each non-Collection value * into a new value using the closure as a transformer. Returns a potentially nested * collection of transformed values. * * @param self a collection * @param collection an initial Collection to which the transformed values are added * @param closure the closure used to transform each element of the collection * @return the resultant collection * @since 1.5.2 */ public static Collection collectAll(Collection self, Collection collection, Closure closure) { for (Iterator iter = self.iterator(); iter.hasNext();) { final Object o = iter.next(); if (o instanceof Collection) { Collection c = (Collection) o; collection.add(collectAll(c, createSimilarCollection(collection, c.size()), closure)); } else { collection.add(closure.call(o)); } if (closure.getDirective() == Closure.DONE) { break; } } return collection; }
/** * Recursively iterates through this collection transforming each non-Collection value * into a new value using the <code>transform</code> closure. Returns a potentially nested * collection of transformed values. * <pre class="groovyTestCase">def x = [1,[2,3],[4],[]].collectNested(new Vector()) { it * 2 } * assert x == [2,[4,6],[8],[]] * assert x instanceof Vector</pre> * * @param self a collection * @param collector an initial Collection to which the transformed values are added * @param transform the closure used to transform each element of the collection * @return the collector with all transformed values added to it * @since 1.8.1 */ public static Collection collectNested(Collection self, Collection collector, Closure transform) { for (Object item : self) { if (item instanceof Collection) { Collection c = (Collection) item; collector.add(collectNested(c, createSimilarCollection(collector, c.size()), transform)); } else { collector.add(transform.call(item)); } if (transform.getDirective() == Closure.DONE) { break; } } return collector; }
public Object getProperty(final String property) { if ("delegate".equals(property)) { return getDelegate(); } else if ("owner".equals(property)) { return getOwner(); } else if ("maximumNumberOfParameters".equals(property)) { return Integer.valueOf(getMaximumNumberOfParameters()); } else if ("parameterTypes".equals(property)) { return getParameterTypes(); } else if ("metaClass".equals(property)) { return getMetaClass(); } else if ("class".equals(property)) { return getClass(); } else if ("directive".equals(property)) { return Integer.valueOf(getDirective()); } else { switch(resolveStrategy) { case DELEGATE_FIRST: return getPropertyDelegateFirst(property); case DELEGATE_ONLY: return InvokerHelper.getProperty(this.delegate, property); case OWNER_ONLY: return InvokerHelper.getProperty(this.owner, property); case TO_SELF: return super.getProperty(property); default: return getPropertyOwnerFirst(property); } } }
public Object getProperty(final String property) { if ("delegate".equals(property)) { return getDelegate(); } else if ("owner".equals(property)) { return getOwner(); } else if ("maximumNumberOfParameters".equals(property)) { return new Integer(getMaximumNumberOfParameters()); } else if ("parameterTypes".equals(property)) { return getParameterTypes(); } else if ("metaClass".equals(property)) { return getMetaClass(); } else if ("class".equals(property)) { return getClass(); } else if ("directive".equals(property)) { return new Integer(getDirective()); } else { switch(resolveStrategy) { case DELEGATE_FIRST: return getPropertyDelegateFirst(property); case DELEGATE_ONLY: return InvokerHelper.getProperty(this.delegate, property); case OWNER_ONLY: return InvokerHelper.getProperty(this.owner, property); case TO_SELF: return super.getProperty(property); default: return getPropertyOwnerFirst(property); } } }