Search in sources :

Example 26 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method findLastIndexOf.

/**
     * Iterates over the elements of an iterable collection of items, starting
     * from a specified startIndex, and returns the index of the last item that
     * matches the condition specified in the closure.
     *
     * @param self       the iteration object over which to iterate
     * @param startIndex start matching from this index
     * @param closure    the filter to perform a match on the collection
     * @return an integer that is the index of the last matched object or -1 if no match was found
     * @since 1.5.2
     */
public static int findLastIndexOf(Object self, int startIndex, Closure closure) {
    int result = -1;
    int i = 0;
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
    for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); i++) {
        Object value = iter.next();
        if (i < startIndex) {
            continue;
        }
        if (bcw.call(value)) {
            result = i;
        }
    }
    return result;
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper) ArrayIterator(org.codehaus.groovy.util.ArrayIterator)

Example 27 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method dropWhile.

/**
     * Create a suffix of the given Map by dropping as many entries as possible from the
     * front of the original Map such that calling the given closure condition evaluates to
     * true when passed each of the dropped entries (or key/value pairs).
     * <pre class="groovyTestCase">
     * def shopping = [milk:1, bread:2, chocolate:3]
     * assert shopping.dropWhile{ it.key.size() < 6 } == [chocolate:3]
     * assert shopping.dropWhile{ it.value % 2 } == [bread:2, chocolate:3]
     * assert shopping.dropWhile{ k, v -> k.size() + v <= 7 } == [chocolate:3]
     * </pre>
     * If the map instance does not have ordered keys, then this function could appear to drop random
     * entries. Groovy by default uses LinkedHashMap, so this shouldn't be an issue in the main.
     *
     * @param self      a Map
     * @param condition a 1 (or 2) arg Closure that must evaluate to true for the
     *                  entry (or key and value) to continue dropping elements
     * @return the shortest suffix of the given Map such that the given closure condition
     *         evaluates to true for each element dropped from the front of the Map
     * @since 1.8.7
     */
public static <K, V> Map<K, V> dropWhile(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<?> condition) {
    if (self.isEmpty()) {
        return createSimilarMap(self);
    }
    Map<K, V> ret = createSimilarMap(self);
    boolean dropping = true;
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
    for (Map.Entry<K, V> entry : self.entrySet()) {
        if (dropping && !bcw.callForMap(entry))
            dropping = false;
        if (!dropping)
            ret.put(entry.getKey(), entry.getValue());
    }
    return ret;
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)

Example 28 with BooleanClosureWrapper

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

the class StringGroovyMethods method dropWhile.

/**
     * Create a suffix of the given CharSequence by dropping as many characters as possible from the
     * front of the original CharSequence such that calling the given closure condition evaluates to
     * true when passed each of the dropped characters.
     * <p>
     * <pre class="groovyTestCase">
     * def text = "Groovy"
     * assert text.dropWhile{ false } == 'Groovy'
     * assert text.dropWhile{ true } == ''
     * assert text.dropWhile{ it < 'Z' } == 'roovy'
     * assert text.dropWhile{ it != 'v' } == 'vy'
     * </pre>
     *
     * @param self      the original CharSequence
     * @param condition the closure that while continuously evaluating to true will cause us to drop elements from
     *                  the front of the original CharSequence
     * @return the shortest suffix of the given CharSequence such that the given closure condition
     *         evaluates to true for each element dropped from the front of the CharSequence
     * @since 2.0.0
     */
public static CharSequence dropWhile(CharSequence self, @ClosureParams(value = SimpleType.class, options = "char") Closure condition) {
    int num = 0;
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
    while (num < self.length()) {
        char value = self.charAt(num);
        if (bcw.call(value)) {
            num += 1;
        } else {
            break;
        }
    }
    return drop(self, num);
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)

Example 29 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method findLastIndexOf.

/**
     * Iterates over the elements of an iterable collection of items, starting
     * from a specified startIndex, and returns the index of the last item that
     * matches the condition specified in the closure.
     *
     * @param self       the iteration object over which to iterate
     * @param startIndex start matching from this index
     * @param closure    the filter to perform a match on the collection
     * @return an integer that is the index of the last matched object or -1 if no match was found
     * @since 1.5.2
     */
public static int findLastIndexOf(Object self, int startIndex, Closure closure) {
    int result = -1;
    int i = 0;
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
    for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); i++) {
        Object value = iter.next();
        if (i < startIndex) {
            continue;
        }
        if (bcw.call(value)) {
            result = i;
        }
    }
    return result;
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper) BufferedIterator(groovy.util.BufferedIterator) IteratorBufferedIterator(org.codehaus.groovy.util.IteratorBufferedIterator) ArrayIterator(org.codehaus.groovy.util.ArrayIterator) ListBufferedIterator(org.codehaus.groovy.util.ListBufferedIterator)

Example 30 with BooleanClosureWrapper

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

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)

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