org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper Java Examples

The following examples show how to use org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IOGroovyMethods.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }

}
 
Example #2
Source File: IOGroovyMethods.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * 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() {
            Writer buffer = new StringBuilderWriter();
            try {
                writeTo(buffer);
            } catch (IOException e) {
                throw new StringWriterIOException(e);
            }
            return buffer.toString();
        }
    };
}
 
Example #3
Source File: CpsBooleanClosureWrapper.java    From groovy-cps with Apache License 2.0 4 votes vote down vote up
/**
 * normal closure call
 */
public boolean call(Object... args) {
    return new BooleanClosureWrapper(wrapped).call(args);
}
 
Example #4
Source File: CpsBooleanClosureWrapper.java    From groovy-cps with Apache License 2.0 2 votes vote down vote up
/**
 * Bridge for a call based on a map entry. If the call is done on a {@link Closure}
 * taking one argument, then we give in the {@link Map.Entry}, otherwise we will
 * give in the key and value.
 */
public <K,V> boolean callForMap(Map.Entry<K, V> entry) {
    return new BooleanClosureWrapper(wrapped).callForMap(entry);
}