Search in sources :

Example 36 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method retainAll.

/**
     * Modifies this map so that it retains only its elements that are matched
     * according to the specified closure condition.  In other words, removes from
     * this map all of its elements that don't match. If the closure takes one
     * parameter then it will be passed the <code>Map.Entry</code>. Otherwise the closure should
     * take two parameters, which will be the key and the value.
     *
     * <pre class="groovyTestCase">def map = [a:1, b:2]
     * map.retainAll { k,v -> k == 'b' }
     * assert map == [b:2]</pre>
     *
     * See also <code>findAll</code> when wanting to produce a new map containing items
     * which match some criteria but leaving the original map unchanged.
     *
     * @param self a Map to be modified
     * @param condition a 1 or 2 arg Closure condition applying on the entries
     * @return <tt>true</tt> if this map changed as a result of the call
     * @since 2.5
     */
public static <K, V> boolean retainAll(Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure condition) {
    Iterator<Map.Entry<K, V>> iter = self.entrySet().iterator();
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(condition);
    boolean result = false;
    while (iter.hasNext()) {
        Map.Entry<K, V> entry = iter.next();
        if (!bcw.callForMap(entry)) {
            iter.remove();
            result = true;
        }
    }
    return result;
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper) MapEntry(groovy.util.MapEntry)

Example 37 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method retainAll.

/**
     * Modifies this collection so that it retains only its elements
     * that are matched according to the specified closure condition.  In other words,
     * removes from this collection all of its elements that don't match.
     *
     * <pre class="groovyTestCase">def list = ['a', 'b']
     * list.retainAll { it == 'b' }
     * assert list == ['b']</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 retainAll(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 38 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 list where each element
     * passed to the given closure condition evaluates to true.
     * Similar to {@link #takeWhile(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.takeWhile{ it < 1 } == []
     * assert nums.takeWhile{ it < 3 } == [ 1 ]
     * assert nums.takeWhile{ it < 4 } == [ 1, 3, 2 ]
     * </pre>
     *
     * @param self      the original list
     * @param condition the closure that must evaluate to true to
     *                  continue taking elements
     * @return a prefix of the given list where each element passed to
     *         the given closure evaluates to true
     * @since 1.8.7
     */
public static <T> List<T> takeWhile(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 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