use of groovy.lang.Closure in project groovy-core by groovy.
the class ResourceGroovyMethods method traverse.
private static FileVisitResult traverse(final File self, final Map<String, Object> options, final Closure closure, final int maxDepth) throws FileNotFoundException, IllegalArgumentException {
checkDir(self);
final Closure pre = (Closure) options.get("preDir");
final Closure post = (Closure) options.get("postDir");
final FileType type = (FileType) options.get("type");
final Object filter = options.get("filter");
final Object nameFilter = options.get("nameFilter");
final Object excludeFilter = options.get("excludeFilter");
final Object excludeNameFilter = options.get("excludeNameFilter");
final Closure sort = (Closure) options.get("sort");
final File[] origFiles = self.listFiles();
// null check because of http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4803836
if (origFiles != null) {
List<File> files = Arrays.asList(origFiles);
if (sort != null)
files = DefaultGroovyMethods.sort(files, sort);
for (File file : files) {
if (file.isDirectory()) {
if (type != FileType.FILES) {
if (closure != null && notFiltered(file, filter, nameFilter, excludeFilter, excludeNameFilter)) {
Object closureResult = closure.call(file);
if (closureResult == FileVisitResult.SKIP_SIBLINGS)
break;
if (closureResult == FileVisitResult.TERMINATE)
return FileVisitResult.TERMINATE;
}
}
if (maxDepth != 0) {
Object preResult = null;
if (pre != null) {
preResult = pre.call(file);
}
if (preResult == FileVisitResult.SKIP_SIBLINGS)
break;
if (preResult == FileVisitResult.TERMINATE)
return FileVisitResult.TERMINATE;
if (preResult != FileVisitResult.SKIP_SUBTREE) {
FileVisitResult terminated = traverse(file, options, closure, maxDepth - 1);
if (terminated == FileVisitResult.TERMINATE)
return terminated;
}
Object postResult = null;
if (post != null) {
postResult = post.call(file);
}
if (postResult == FileVisitResult.SKIP_SIBLINGS)
break;
if (postResult == FileVisitResult.TERMINATE)
return FileVisitResult.TERMINATE;
}
} else if (type != FileType.DIRECTORIES) {
if (closure != null && notFiltered(file, filter, nameFilter, excludeFilter, excludeNameFilter)) {
Object closureResult = closure.call(file);
if (closureResult == FileVisitResult.SKIP_SIBLINGS)
break;
if (closureResult == FileVisitResult.TERMINATE)
return FileVisitResult.TERMINATE;
}
}
}
}
return FileVisitResult.CONTINUE;
}
use of groovy.lang.Closure in project groovy-core by groovy.
the class ResourceGroovyMethods method traverse.
/**
* Invokes <code>closure</code> for each descendant file in this directory tree.
* Sub-directories are recursively traversed as found.
* The traversal can be adapted by providing various options in the <code>options</code> Map according
* to the following keys:<dl>
* <dt>type</dt><dd>A {@link groovy.io.FileType} enum to determine if normal files or directories or both are processed</dd>
* <dt>preDir</dt><dd>A {@link groovy.lang.Closure} run before each directory is processed and optionally returning a {@link groovy.io.FileVisitResult} value
* which can be used to control subsequent processing.</dd>
* <dt>preRoot</dt><dd>A boolean indicating that the 'preDir' closure should be applied at the root level</dd>
* <dt>postDir</dt><dd>A {@link groovy.lang.Closure} run after each directory is processed and optionally returning a {@link groovy.io.FileVisitResult} value
* which can be used to control subsequent processing.</dd>
* <dt>postRoot</dt><dd>A boolean indicating that the 'postDir' closure should be applied at the root level</dd>
* <dt>visitRoot</dt><dd>A boolean indicating that the given closure should be applied for the root dir
* (not applicable if the 'type' is set to {@link groovy.io.FileType#FILES})</dd>
* <dt>maxDepth</dt><dd>The maximum number of directory levels when recursing
* (default is -1 which means infinite, set to 0 for no recursion)</dd>
* <dt>filter</dt><dd>A filter to perform on traversed files/directories (using the {@link DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object)} method). If set,
* only files/dirs which match are candidates for visiting.</dd>
* <dt>nameFilter</dt><dd>A filter to perform on the name of traversed files/directories (using the {@link DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object)} method). If set,
* only files/dirs which match are candidates for visiting. (Must not be set if 'filter' is set)</dd>
* <dt>excludeFilter</dt><dd>A filter to perform on traversed files/directories (using the {@link DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object)} method).
* If set, any candidates which match won't be visited.</dd>
* <dt>excludeNameFilter</dt><dd>A filter to perform on the names of traversed files/directories (using the {@link DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object)} method).
* If set, any candidates which match won't be visited. (Must not be set if 'excludeFilter' is set)</dd>
* <dt>sort</dt><dd>A {@link groovy.lang.Closure} which if set causes the files and subdirectories for each directory to be processed in sorted order.
* Note that even when processing only files, the order of visited subdirectories will be affected by this parameter.</dd>
* </dl>
* This example prints out file counts and size aggregates for groovy source files within a directory tree:
* <pre>
* def totalSize = 0
* def count = 0
* def sortByTypeThenName = { a, b ->
* a.isFile() != b.isFile() ? a.isFile() <=> b.isFile() : a.name <=> b.name
* }
* rootDir.traverse(
* type : FILES,
* nameFilter : ~/.*\.groovy/,
* preDir : { if (it.name == '.svn') return SKIP_SUBTREE },
* postDir : { println "Found $count files in $it.name totalling $totalSize bytes"
* totalSize = 0; count = 0 },
* postRoot : true
* sort : sortByTypeThenName
* ) {it -> totalSize += it.size(); count++ }
* </pre>
*
* @param self a File
* @param options a Map of options to alter the traversal behavior
* @param closure the Closure to invoke on each file/directory and optionally returning a {@link groovy.io.FileVisitResult} value
* which can be used to control subsequent processing
* @throws FileNotFoundException if the given directory does not exist
* @throws IllegalArgumentException if the provided File object does not represent a directory or illegal filter combinations are supplied
* @see DefaultGroovyMethods#sort(java.util.Collection, groovy.lang.Closure)
* @see groovy.io.FileVisitResult
* @see groovy.io.FileType
* @since 1.7.1
*/
public static void traverse(final File self, final Map<String, Object> options, @ClosureParams(value = SimpleType.class, options = "java.io.File") final Closure closure) throws FileNotFoundException, IllegalArgumentException {
Number maxDepthNumber = DefaultGroovyMethods.asType(options.remove("maxDepth"), Number.class);
int maxDepth = maxDepthNumber == null ? -1 : maxDepthNumber.intValue();
Boolean visitRoot = DefaultGroovyMethods.asType(get(options, "visitRoot", false), Boolean.class);
Boolean preRoot = DefaultGroovyMethods.asType(get(options, "preRoot", false), Boolean.class);
Boolean postRoot = DefaultGroovyMethods.asType(get(options, "postRoot", false), Boolean.class);
final Closure pre = (Closure) options.get("preDir");
final Closure post = (Closure) options.get("postDir");
final FileType type = (FileType) options.get("type");
final Object filter = options.get("filter");
final Object nameFilter = options.get("nameFilter");
final Object excludeFilter = options.get("excludeFilter");
final Object excludeNameFilter = options.get("excludeNameFilter");
Object preResult = null;
if (preRoot && pre != null) {
preResult = pre.call(self);
}
if (preResult == FileVisitResult.TERMINATE || preResult == FileVisitResult.SKIP_SUBTREE)
return;
FileVisitResult terminated = traverse(self, options, closure, maxDepth);
if (type != FileType.FILES && visitRoot) {
if (closure != null && notFiltered(self, filter, nameFilter, excludeFilter, excludeNameFilter)) {
Object closureResult = closure.call(self);
if (closureResult == FileVisitResult.TERMINATE)
return;
}
}
if (postRoot && post != null && terminated != FileVisitResult.TERMINATE)
post.call(self);
}
use of groovy.lang.Closure in project groovy-core by groovy.
the class JName1Test method testMetaClassNameHandlingWithClosures.
public void testMetaClassNameHandlingWithClosures() {
// Test class implementing GroovyObject
final Tt1cgi obj = new Tt1cgi();
final Closure newX = new Closure(null) {
public Object doCall(final Object params) {
return "new x";
}
};
final Closure newX1 = new Closure(null) {
public Object doCall(final Object params) {
return "new x1";
}
};
final Closure newX2 = new Closure(null) {
public Object doCall(final Object params) {
return "new x2";
}
};
final Closure newX3 = new Closure(null) {
public Object doCall(final Object params) {
return "new x3";
}
};
assertTrue(((Closure) obj.getProperty("x")).call() == obj.getX().call());
assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == obj.x.call());
assertTrue(obj.invokeMethod("x", new Object[] {}) == obj.x());
obj.setProperty("x", newX);
obj.getMetaClass().setAttribute(obj, "x", newX1);
assertTrue(((Closure) obj.getProperty("x")).call() == newX.call());
assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == newX1.call());
obj.setX(newX2);
obj.x = newX3;
assertTrue(((Closure) obj.getProperty("x")).call() == newX2.call());
assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == newX3.call());
}
use of groovy.lang.Closure in project groovy-core by groovy.
the class JName1Test method testObjectSupportNameHandlingWitnClosureValuesi.
public void testObjectSupportNameHandlingWitnClosureValuesi() {
final Tt1cgo obj = new Tt1cgo() {
};
// repeat test with subclass
final Closure newX = new Closure(null) {
public Object doCall(final Object params) {
return "new x";
}
};
final Closure newX1 = new Closure(null) {
public Object doCall(final Object params) {
return "new x1";
}
};
final Closure newX2 = new Closure(null) {
public Object doCall(final Object params) {
return "new x2";
}
};
final Closure newX3 = new Closure(null) {
public Object doCall(final Object params) {
return "new x3";
}
};
assertTrue(((Closure) obj.getProperty("x")).call() == obj.getX().call());
assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == obj.x.call());
assertTrue(obj.invokeMethod("x", new Object[] {}) == obj.x());
obj.setProperty("x", newX);
obj.getMetaClass().setAttribute(obj, "x", newX1);
assertTrue(((Closure) obj.getProperty("x")).call() == newX.call());
assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == newX1.call());
obj.setX(newX2);
obj.x = newX3;
assertTrue(((Closure) obj.getProperty("x")).call() == newX2.call());
assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == newX3.call());
}
use of groovy.lang.Closure in project groovy-core by groovy.
the class JName1Test method testMetaClassNameHandlingWithClosures1.
public void testMetaClassNameHandlingWithClosures1() {
final Tt1cgi obj = new Tt1cgi() {
};
// repeat test with subclass
final Closure newX = new Closure(null) {
public Object doCall(final Object params) {
return "new x";
}
};
final Closure newX1 = new Closure(null) {
public Object doCall(final Object params) {
return "new x1";
}
};
final Closure newX2 = new Closure(null) {
public Object doCall(final Object params) {
return "new x2";
}
};
final Closure newX3 = new Closure(null) {
public Object doCall(final Object params) {
return "new x3";
}
};
assertTrue(((Closure) obj.getProperty("x")).call() == obj.getX().call());
assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == obj.x.call());
assertTrue(obj.invokeMethod("x", new Object[] {}) == obj.x());
obj.setProperty("x", newX);
obj.getMetaClass().setAttribute(obj, "x", newX1);
assertTrue(((Closure) obj.getProperty("x")).call() == newX.call());
assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == newX1.call());
obj.setX(newX2);
obj.x = newX3;
assertTrue(((Closure) obj.getProperty("x")).call() == newX2.call());
assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == newX3.call());
}
Aggregations