Search in sources :

Example 6 with BooleanReturningMethodInvoker

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

the class DefaultGroovyMethods method grep.

/**
     * Iterates over the array of items and returns a collection of items that match
     * the given filter - calling the <code>{@link #isCase(java.lang.Object, java.lang.Object)}</code>
     * method used by switch statements. This method can be used with different
     * kinds of filters like regular expressions, classes, ranges etc.
     * Example:
     * <pre class="groovyTestCase">
     * def items = ['a', 'b', 'aa', 'bc', 3, 4.5] as Object[]
     * assert items.grep( ~/a+/ )  == ['a', 'aa']
     * assert items.grep( ~/../ )  == ['aa', 'bc']
     * assert items.grep( Number ) == [ 3, 4.5 ]
     * assert items.grep{ it.toString().size() == 1 } == [ 'a', 'b', 3 ]
     * </pre>
     *
     * @param self   an array
     * @param filter the filter to perform on each element of the array (using the {@link #isCase(java.lang.Object, java.lang.Object)} method)
     * @return a collection of objects which match the filter
     * @since 2.0
     */
public static <T> Collection<T> grep(T[] self, Object filter) {
    Collection<T> answer = new ArrayList<T>();
    BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker("isCase");
    for (T element : self) {
        if (bmi.invoke(filter, element)) {
            answer.add(element);
        }
    }
    return answer;
}
Also used : BooleanReturningMethodInvoker(org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker)

Example 7 with BooleanReturningMethodInvoker

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

the class ResourceGroovyMethods method eachFileMatch.

/**
     * Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory
     * - calling the {@link DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object)} method to determine if a match occurs.  This method can be used
     * with different kinds of filters like regular expressions, classes, ranges etc.
     * Both regular files and subdirectories may be candidates for matching depending
     * on the value of fileType.
     * <pre>
     * // collect names of files in baseDir matching supplied regex pattern
     * import static groovy.io.FileType.*
     * def names = []
     * baseDir.eachFileMatch FILES, ~/foo\d\.txt/, { names << it.name }
     * assert names == ['foo1.txt', 'foo2.txt']
     *
     * // remove all *.bak files in baseDir
     * baseDir.eachFileMatch FILES, ~/.*\.bak/, { File bak -> bak.delete() }
     *
     * // print out files > 4K in size from baseDir
     * baseDir.eachFileMatch FILES, { new File(baseDir, it).size() > 4096 }, { println "$it.name ${it.size()}" }
     * </pre>
     *
     * @param self       a file
     * @param fileType   whether normal files or directories or both should be processed
     * @param nameFilter the filter to perform on the name of the file/directory (using the {@link DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object)} method)
     * @param closure    the closure to invoke
     * @throws FileNotFoundException    if the given directory does not exist
     * @throws IllegalArgumentException if the provided File object does not represent a directory
     * @since 1.7.1
     */
public static void eachFileMatch(final File self, final FileType fileType, final Object nameFilter, @ClosureParams(value = SimpleType.class, options = "java.io.File") final Closure closure) throws FileNotFoundException, IllegalArgumentException {
    checkDir(self);
    final File[] files = self.listFiles();
    // null check because of http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4803836
    if (files == null)
        return;
    BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker("isCase");
    for (final File currentFile : files) {
        if (fileType == FileType.ANY || (fileType != FileType.FILES && currentFile.isDirectory()) || (fileType != FileType.DIRECTORIES && currentFile.isFile())) {
            if (bmi.invoke(nameFilter, currentFile.getName()))
                closure.call(currentFile);
        }
    }
}
Also used : BooleanReturningMethodInvoker(org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker)

Example 8 with BooleanReturningMethodInvoker

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

the class NioGroovyMethods method eachFileMatch.

/**
     * Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory
     * - calling the {@link org.codehaus.groovy.runtime.DefaultGroovyMethods#isCase(Object, Object)} method to determine if a match occurs.  This method can be used
     * with different kinds of filters like regular expressions, classes, ranges etc.
     * Both regular files and subdirectories may be candidates for matching depending
     * on the value of fileType.
     * <pre>
     * // collect names of files in baseDir matching supplied regex pattern
     * import static groovy.io.FileType.*
     * def names = []
     * baseDir.eachFileMatch FILES, ~/foo\d\.txt/, { names << it.name }
     * assert names == ['foo1.txt', 'foo2.txt']
     *
     * // remove all *.bak files in baseDir
     * baseDir.eachFileMatch FILES, ~/.*\.bak/, { Path bak -> bak.delete() }
     *
     * // print out files > 4K in size from baseDir
     * baseDir.eachFileMatch FILES, { new Path(baseDir, it).size() > 4096 }, { println "$it.name ${it.size()}" }
     * </pre>
     *
     * @param self       a file
     * @param fileType   whether normal files or directories or both should be processed
     * @param nameFilter the filter to perform on the name of the file/directory (using the {@link org.codehaus.groovy.runtime.DefaultGroovyMethods#isCase(Object, Object)} method)
     * @param closure    the closure to invoke
     * @throws java.io.FileNotFoundException    if the given directory does not exist
     * @throws IllegalArgumentException if the provided Path object does not represent a directory
     * @since 2.3.0
     */
public static void eachFileMatch(final Path self, final FileType fileType, final Object nameFilter, @ClosureParams(value = SimpleType.class, options = "java.nio.file.Path") final Closure closure) throws IOException {
    // throws FileNotFoundException, IllegalArgumentException {
    checkDir(self);
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(self)) {
        Iterator<Path> itr = stream.iterator();
        BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker("isCase");
        while (itr.hasNext()) {
            Path currentPath = itr.next();
            if ((fileType != FileType.FILES && Files.isDirectory(currentPath)) || (fileType != FileType.DIRECTORIES && Files.isRegularFile(currentPath))) {
                if (bmi.invoke(nameFilter, currentPath.getFileName().toString()))
                    closure.call(currentPath);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) BooleanReturningMethodInvoker(org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker)

Example 9 with BooleanReturningMethodInvoker

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

the class DefaultGroovyMethods method grep.

/**
     * Iterates over the collection of items which this Object represents and returns each item that matches
     * the given filter - calling the <code>{@link #isCase(java.lang.Object, java.lang.Object)}</code>
     * method used by switch statements.  This method can be used with different
     * kinds of filters like regular expressions, classes, ranges etc.
     * Example:
     * <pre class="groovyTestCase">
     * def list = ['a', 'b', 'aa', 'bc', 3, 4.5]
     * assert list.grep( ~/a+/ )  == ['a', 'aa']
     * assert list.grep( ~/../ )  == ['aa', 'bc']
     * assert list.grep( Number ) == [ 3, 4.5 ]
     * assert list.grep{ it.toString().size() == 1 } == [ 'a', 'b', 3 ]
     * </pre>
     *
     * @param self   the object over which we iterate
     * @param filter the filter to perform on the object (using the {@link #isCase(java.lang.Object, java.lang.Object)} method)
     * @return a collection of objects which match the filter
     * @since 1.5.6
     */
public static Collection grep(Object self, Object filter) {
    Collection answer = createSimilarOrDefaultCollection(self);
    BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker("isCase");
    for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); ) {
        Object object = iter.next();
        if (bmi.invoke(filter, object)) {
            answer.add(object);
        }
    }
    return answer;
}
Also used : BooleanReturningMethodInvoker(org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker) ArrayIterator(org.codehaus.groovy.util.ArrayIterator)

Example 10 with BooleanReturningMethodInvoker

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

the class DefaultGroovyMethods method grep.

/**
     * Iterates over the collection of items which this Object represents and returns each item that matches
     * the given filter - calling the <code>{@link #isCase(java.lang.Object, java.lang.Object)}</code>
     * method used by switch statements.  This method can be used with different
     * kinds of filters like regular expressions, classes, ranges etc.
     * Example:
     * <pre class="groovyTestCase">
     * def list = ['a', 'b', 'aa', 'bc', 3, 4.5]
     * assert list.grep( ~/a+/ )  == ['a', 'aa']
     * assert list.grep( ~/../ )  == ['aa', 'bc']
     * assert list.grep( Number ) == [ 3, 4.5 ]
     * assert list.grep{ it.toString().size() == 1 } == [ 'a', 'b', 3 ]
     * </pre>
     *
     * @param self   the object over which we iterate
     * @param filter the filter to perform on the object (using the {@link #isCase(java.lang.Object, java.lang.Object)} method)
     * @return a collection of objects which match the filter
     * @since 1.5.6
     */
public static Collection grep(Object self, Object filter) {
    Collection answer = createSimilarOrDefaultCollection(self);
    BooleanReturningMethodInvoker bmi = new BooleanReturningMethodInvoker("isCase");
    for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); ) {
        Object object = iter.next();
        if (bmi.invoke(filter, object)) {
            answer.add(object);
        }
    }
    return answer;
}
Also used : BooleanReturningMethodInvoker(org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker) BufferedIterator(groovy.util.BufferedIterator) IteratorBufferedIterator(org.codehaus.groovy.util.IteratorBufferedIterator) ArrayIterator(org.codehaus.groovy.util.ArrayIterator) ListBufferedIterator(org.codehaus.groovy.util.ListBufferedIterator)

Aggregations

BooleanReturningMethodInvoker (org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker)10 Path (java.nio.file.Path)2 ArrayIterator (org.codehaus.groovy.util.ArrayIterator)2 BufferedIterator (groovy.util.BufferedIterator)1 IteratorBufferedIterator (org.codehaus.groovy.util.IteratorBufferedIterator)1 ListBufferedIterator (org.codehaus.groovy.util.ListBufferedIterator)1