Search in sources :

Example 11 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method dropWhile.

/**
     * Create a suffix of the given array by dropping as many elements as possible from the
     * front of the original array such that calling the given closure condition evaluates to
     * true when passed each of the dropped elements.
     * <pre class="groovyTestCase">
     * def nums = [ 1, 3, 2 ] as Integer[]
     * assert nums.dropWhile{ it <= 3 } == [ ] as Integer[]
     * assert nums.dropWhile{ it < 3 } == [ 3, 2 ] as Integer[]
     * assert nums.dropWhile{ it != 2 } == [ 2 ] as Integer[]
     * assert nums.dropWhile{ it == 0 } == [ 1, 3, 2 ] as Integer[]
     * </pre>
     *
     * @param self      the original array
     * @param condition the closure that must evaluate to true to
     *                  continue dropping elements
     * @return the shortest suffix of the given array such that the given closure condition
     *         evaluates to true for each element dropped from the front of the array
     * @since 1.8.7
     */
public static <T> T[] dropWhile(T[] self, @ClosureParams(FirstParam.Component.class) Closure<?> condition) {
    int num = 0;
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
    while (num < self.length) {
        if (bcw.call(self[num])) {
            num += 1;
        } else {
            break;
        }
    }
    return drop(self, num);
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)

Example 12 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method dropWhile.

/**
     * Returns a suffix of this List where elements are dropped from the front
     * while the given Closure evaluates to true.
     * Similar to {@link #dropWhile(Iterable, groovy.lang.Closure)}
     * except that it attempts to preserve the type of the original list.
     * <pre class="groovyTestCase">
     * def nums = [ 1, 3, 2 ]
     * assert nums.dropWhile{ it < 4 } == []
     * assert nums.dropWhile{ it < 3 } == [ 3, 2 ]
     * assert nums.dropWhile{ it != 2 } == [ 2 ]
     * assert nums.dropWhile{ it == 0 } == [ 1, 3, 2 ]
     * </pre>
     *
     * @param self      the original list
     * @param condition the closure that must evaluate to true to continue dropping elements
     * @return the shortest suffix of the given List such that the given closure condition
     *         evaluates to true for each element dropped from the front of the List
     * @since 1.8.7
     */
public static <T> List<T> dropWhile(List<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
    int num = 0;
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
    for (T value : self) {
        if (bcw.call(value)) {
            num += 1;
        } else {
            break;
        }
    }
    return drop(self, num);
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper)

Example 13 with BooleanClosureWrapper

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

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)

Example 14 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method removeAll.

/**
     * Modifies this collection by removing the elements that are matched according
     * to the specified closure condition.
     *
     * <pre class="groovyTestCase">def list = ['a', 'b']
     * list.removeAll { it == 'b' }
     * assert list == ['a']</pre>
     *
     * See also <code>findAll</code> and <code>grep</code> when wanting to produce a new list
     * containing items which match some criteria but leaving the original collection unchanged.
     *
     * @param  self a Collection to be modified
     * @param  condition a closure condition
     * @return <tt>true</tt> if this collection changed as a result of the call
     * @see    Iterator#remove()
     * @since 1.7.2
     */
public static <T> boolean removeAll(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure condition) {
    Iterator iter = InvokerHelper.asIterator(self);
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
    boolean result = false;
    while (iter.hasNext()) {
        Object value = iter.next();
        if (bcw.call(value)) {
            iter.remove();
            result = true;
        }
    }
    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 15 with BooleanClosureWrapper

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

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)

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