Search in sources :

Example 31 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method removeAll.

/**
     * Modifies this map by removing the elements that are matched according to the
     * specified closure condition. 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.removeAll { k,v -> k == 'b' }
     * assert map == [a:1]</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 removeAll(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 32 with BooleanClosureWrapper

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

the class IOGroovyMethods method filterLine.

/**
     * Filter the lines from a reader and write them on the writer,
     * according to a closure which returns true if the line should be included.
     * Both Reader and Writer are closed after the operation.
     *
     * @param reader  a reader, closed after the call
     * @param writer  a writer, closed after the call
     * @param closure the closure which returns booleans
     * @throws IOException if an IOException occurs.
     * @since 1.0
     */
public static void filterLine(Reader reader, Writer writer, @ClosureParams(value = SimpleType.class, options = "java.lang.String") Closure closure) throws IOException {
    BufferedReader br = new BufferedReader(reader);
    BufferedWriter bw = new BufferedWriter(writer);
    String line;
    try {
        BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
        while ((line = br.readLine()) != null) {
            if (bcw.call(line)) {
                bw.write(line);
                bw.newLine();
            }
        }
        bw.flush();
        Writer temp2 = writer;
        writer = null;
        temp2.close();
        Reader temp1 = reader;
        reader = null;
        temp1.close();
    } finally {
        closeWithWarning(br);
        closeWithWarning(reader);
        closeWithWarning(bw);
        closeWithWarning(writer);
    }
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FromString(groovy.transform.stc.FromString) GroovyPrintWriter(groovy.io.GroovyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 33 with BooleanClosureWrapper

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

the class IOGroovyMethods method filterLine.

/**
     * Filter the lines from this Reader, and return a Writable which can be
     * used to stream the filtered lines to a destination.  The closure should
     * return <code>true</code> if the line should be passed to the writer.
     *
     * @param reader  this reader
     * @param closure a closure used for filtering
     * @return a Writable which will use the closure to filter each line
     *         from the reader when the Writable#writeTo(Writer) is called.
     * @since 1.0
     */
public static Writable filterLine(Reader reader, @ClosureParams(value = SimpleType.class, options = "java.lang.String") final Closure closure) {
    final BufferedReader br = new BufferedReader(reader);
    return new Writable() {

        public Writer writeTo(Writer out) throws IOException {
            BufferedWriter bw = new BufferedWriter(out);
            String line;
            BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
            while ((line = br.readLine()) != null) {
                if (bcw.call(line)) {
                    bw.write(line);
                    bw.newLine();
                }
            }
            bw.flush();
            return out;
        }

        public String toString() {
            StringWriter buffer = new StringWriter();
            try {
                writeTo(buffer);
            } catch (IOException e) {
                throw new StringWriterIOException(e);
            }
            return buffer.toString();
        }
    };
}
Also used : BooleanClosureWrapper(org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper) StringWriter(java.io.StringWriter) StringWriterIOException(groovy.lang.StringWriterIOException) BufferedReader(java.io.BufferedReader) Writable(groovy.lang.Writable) FromString(groovy.transform.stc.FromString) StringWriterIOException(groovy.lang.StringWriterIOException) IOException(java.io.IOException) GroovyPrintWriter(groovy.io.GroovyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 34 with BooleanClosureWrapper

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

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 35 with BooleanClosureWrapper

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

the class DefaultGroovyMethods method findIndexValues.

/**
     * Iterates over the elements of an iterable collection of items, starting from
     * a specified startIndex, and returns the index values of the items that match
     * 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 a list of numbers corresponding to the index values of all matched objects
     * @since 1.5.2
     */
public static List<Number> findIndexValues(Object self, Number startIndex, Closure closure) {
    List<Number> result = new ArrayList<Number>();
    long count = 0;
    long startCount = startIndex.longValue();
    BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
    for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); count++) {
        Object value = iter.next();
        if (count < startCount) {
            continue;
        }
        if (bcw.call(value)) {
            result.add(count);
        }
    }
    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)

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