Search in sources :

Example 21 with BooleanClosureWrapper

use of org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper in project groovy-core by groovy.

the class DefaultGroovyMethods method count.

/**
     * Counts the number of occurrences which satisfy the given closure from the
     * items within this Iterator.
     * The iterator will become exhausted of elements after determining the count value.
     * <p>
     * Example usage:
     * <pre class="groovyTestCase">assert [2,4,2,1,3,5,2,4,3].toSet().iterator().count{ it % 2 == 0 } == 2</pre>
     *
     * @param self  the Iterator from which we count the number of matching occurrences
     * @param closure a closure condition
     * @return the number of occurrences
     * @since 1.8.0
     */
public static <T> Number count(Iterator<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
    long answer = 0;
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
    while (self.hasNext()) {
        if (bcw.call(self.next())) {
            ++answer;
        }
    }
    // for b/c with Java return an int if we can
    if (answer <= Integer.MAX_VALUE)
        return (int) answer;
    return answer;
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)

Example 22 with BooleanClosureWrapper

use of org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper in project groovy-core by groovy.

the class DefaultGroovyMethods method split.

private static <T> Collection<Collection<T>> split(Closure closure, Collection<T> accept, Collection<T> reject, Iterator<T> iter) {
    List<Collection<T>> answer = new ArrayList<Collection<T>>();
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
    while (iter.hasNext()) {
        T value = iter.next();
        if (bcw.call(value)) {
            accept.add(value);
        } else {
            reject.add(value);
        }
    }
    answer.add(accept);
    answer.add(reject);
    return answer;
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)

Example 23 with BooleanClosureWrapper

use of org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper in project groovy-core by groovy.

the class DefaultGroovyMethods method count.

/**
     * Counts the number of occurrences which satisfy the given closure from inside this map.
     * If the closure takes one parameter then it will be passed the Map.Entry.
     * Otherwise, the closure should take two parameters and will be passed the key and value.
     * <p>
     * Example usage:
     * <pre class="groovyTestCase">assert [a:1, b:1, c:2, d:2].count{ k,v -> k == 'a' || v == 2 } == 3</pre>
     *
     * @param self  the map within which we count the number of occurrences
     * @param closure a 1 or 2 arg Closure condition applying on the entries
     * @return the number of occurrences
     * @since 1.8.0
     */
public static <K, V> Number count(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<?> closure) {
    long answer = 0;
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
    for (Object entry : self.entrySet()) {
        if (bcw.callForMap((Map.Entry) entry)) {
            ++answer;
        }
    }
    // for b/c with Java return an int if we can
    if (answer <= Integer.MAX_VALUE)
        return (int) answer;
    return answer;
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)

Example 24 with BooleanClosureWrapper

use of org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper in project groovy-core by groovy.

the class DefaultGroovyMethods method findAll.

/**
     * Finds all entries matching the closure condition. If the
     * closure takes one parameter then it will be passed the Map.Entry.
     * Otherwise if the closure should take two parameters, which will be
     * the key and the value.
     * <p>
     * If the <code>self</code> map is one of TreeMap, LinkedHashMap, Hashtable
     * or Properties, the returned Map will preserve that type, otherwise a HashMap will
     * be returned.
     * <p>
     * Example usage:
     * <pre class="groovyTestCase">
     * def result = [a:1, b:2, c:4, d:5].findAll { it.value % 2 == 0 }
     * assert result.every { it instanceof Map.Entry }
     * assert result*.key == ["b", "c"]
     * assert result*.value == [2, 4]
     * </pre>
     *
     * @param self    a Map
     * @param closure a 1 or 2 arg Closure condition applying on the entries
     * @return a new subMap
     * @since 1.0
     */
public static <K, V> Map<K, V> findAll(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure closure) {
    Map<K, V> answer = createSimilarMap(self);
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
    for (Map.Entry<K, V> entry : self.entrySet()) {
        if (bcw.callForMap(entry)) {
            answer.put(entry.getKey(), entry.getValue());
        }
    }
    return answer;
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)

Example 25 with BooleanClosureWrapper

use of org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper in project groovy-core by groovy.

the class DefaultGroovyMethods method takeWhile.

/**
     * Returns the longest prefix of this array where each element
     * passed to the given closure evaluates to true.
     * <pre class="groovyTestCase">
     * def nums = [ 1, 3, 2 ] as Integer[]
     * assert nums.takeWhile{ it < 1 } == [] as Integer[]
     * assert nums.takeWhile{ it < 3 } == [ 1 ] as Integer[]
     * assert nums.takeWhile{ it < 4 } == [ 1, 3, 2 ] as Integer[]
     * </pre>
     *
     * @param self      the original array
     * @param condition the closure that must evaluate to true to
     *                  continue taking elements
     * @return a prefix of the given array where each element passed to
     *         the given closure evaluates to true
     * @since 1.8.7
     */
public static <T> T[] takeWhile(T[] self, @ClosureParams(FirstParam.Component.class) Closure condition) {
    int num = 0;
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
    while (num < self.length) {
        T value = self[num];
        if (bcw.call(value)) {
            num += 1;
        } else {
            break;
        }
    }
    return take(self, num);
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)

Aggregations

BooleanClosureWrapper (org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)38 ArrayIterator (org.codehaus.groovy.util.ArrayIterator)10 BufferedIterator (groovy.util.BufferedIterator)5 IteratorBufferedIterator (org.codehaus.groovy.util.IteratorBufferedIterator)5 ListBufferedIterator (org.codehaus.groovy.util.ListBufferedIterator)5 GroovyPrintWriter (groovy.io.GroovyPrintWriter)4 FromString (groovy.transform.stc.FromString)4 BufferedReader (java.io.BufferedReader)4 BufferedWriter (java.io.BufferedWriter)4 OutputStreamWriter (java.io.OutputStreamWriter)4 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 Writer (java.io.Writer)4 StringWriterIOException (groovy.lang.StringWriterIOException)2 Writable (groovy.lang.Writable)2 MapEntry (groovy.util.MapEntry)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2