Only accumulate files and dirs accepted by the filter.
MODIFY
pom.xml
pom.xml
@@ -52,7 +52,7 @@ file comparators, endian transformation classes, and much more.
<connection>scm:git:https://gitbox.apache.org/repos/asf/commons-io.git</connection>
<developerConnection>scm:git:https://gitbox.apache.org/repos/asf/commons-io.git</developerConnection>
<url>https://gitbox.apache.org/repos/asf?p=commons-io.git</url>
- <tag>commons-io-2.8.0</tag>
+ <tag>rel/commons-io-2.9.0</tag>
</scm>
<developers>
@@ -278,9 +278,9 @@ file comparators, endian transformation classes, and much more.
<maven.compiler.target>1.8</maven.compiler.target>
<commons.componentid>io</commons.componentid>
<commons.module.name>org.apache.commons.io</commons.module.name>
- <commons.rc.version>RC2</commons.rc.version>
- <commons.bc.version>2.7</commons.bc.version>
- <commons.release.version>2.8.0</commons.release.version>
+ <commons.rc.version>RC1</commons.rc.version>
+ <commons.bc.version>2.8.0</commons.bc.version>
+ <commons.release.version>2.9.0</commons.release.version>
<commons.release.desc>(requires Java 8)</commons.release.desc>
<commons.jira.id>IO</commons.jira.id>
<commons.jira.pid>12310477</commons.jira.pid>

MODIFY
src/main/java/org/apache/commons/io/file/AccumulatorPathVisitor.java
src/main/java/org/apache/commons/io/file/AccumulatorPathVisitor.java
package org.apache.commons.io.file;
import java.io.IOException;
-import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
@@ -70,34 +69,34 @@ public static AccumulatorPathVisitor withBigIntegerCounters() {
}
/**
- * Creates a new instance configured with a long {@link PathCounters}.
+ * Creates a new instance configured with a BigInteger {@link PathCounters}.
*
+ * @param pathFilter Filters files to accumulate and count.
* @return a new instance configured with a long {@link PathCounters}.
+ * @since 2.9.0
*/
- public static AccumulatorPathVisitor withLongCounters() {
- return new AccumulatorPathVisitor(Counters.longPathCounters());
+ public static AccumulatorPathVisitor withBigIntegerCounters(final PathFilter pathFilter) {
+ return new AccumulatorPathVisitor(Counters.bigIntegerPathCounters(), pathFilter);
}
/**
* Creates a new instance configured with a long {@link PathCounters}.
*
- * @param pathFilter Filters files to accumulate and count.
* @return a new instance configured with a long {@link PathCounters}.
- * @since 2.9.0
*/
- public static AccumulatorPathVisitor withLongCounters(final PathFilter pathFilter) {
- return new AccumulatorPathVisitor(Counters.longPathCounters(), pathFilter);
+ public static AccumulatorPathVisitor withLongCounters() {
+ return new AccumulatorPathVisitor(Counters.longPathCounters());
}
/**
- * Creates a new instance configured with a BigInteger {@link PathCounters}.
+ * Creates a new instance configured with a long {@link PathCounters}.
*
* @param pathFilter Filters files to accumulate and count.
* @return a new instance configured with a long {@link PathCounters}.
* @since 2.9.0
*/
- public static AccumulatorPathVisitor withBigIntegerCounters(final PathFilter pathFilter) {
- return new AccumulatorPathVisitor(Counters.bigIntegerPathCounters(), pathFilter);
+ public static AccumulatorPathVisitor withLongCounters(final PathFilter pathFilter) {
+ return new AccumulatorPathVisitor(Counters.longPathCounters(), pathFilter);
}
private final List<Path> dirList = new ArrayList<>();
@@ -178,12 +177,6 @@ public int hashCode() {
return result;
}
- @Override
- public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
- add(dirList, dir);
- return super.postVisitDirectory(dir, exc);
- }
-
/**
* Relativizes each directory path with {@link Path#relativize(Path)} against the given {@code parent}, optionally
* sorting the result.
@@ -213,9 +206,15 @@ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)
}
@Override
- public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
+ protected void updateDirCounter(final Path dir, final IOException exc) {
+ super.updateDirCounter(dir, exc);
+ add(dirList, dir);
+ }
+
+ @Override
+ protected void updateFileCounters(final Path file, final BasicFileAttributes attributes) {
+ super.updateFileCounters(file, attributes);
add(fileList, file);
- return super.visitFile(file, attributes);
}
}

MODIFY
src/main/java/org/apache/commons/io/file/CountingPathVisitor.java
src/main/java/org/apache/commons/io/file/CountingPathVisitor.java
@@ -108,7 +108,7 @@ public int hashCode() {
@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
- pathCounters.getDirectoryCounter().increment();
+ updateDirCounter(dir, exc);
return FileVisitResult.CONTINUE;
}
@@ -117,6 +117,17 @@ public String toString() {
return pathCounters.toString();
}
+ /**
+ * Updates the counter for visiting the given directory.
+ *
+ * @param dir the visited directory.
+ * @param exc Encountered exception.
+ * @since 2.9.0
+ */
+ protected void updateDirCounter(Path dir, IOException exc) {
+ pathCounters.getDirectoryCounter().increment();
+ }
+
/**
* Updates the counters for visiting the given file.
*

ADD
src/test/java/org/apache/commons/io/filefilter/NameFileFilterTest.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.filefilter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Collections;
+
+import org.apache.commons.io.file.AccumulatorPathVisitor;
+import org.apache.commons.io.file.CounterAssertions;
+import org.apache.commons.io.file.Counters;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link NameFileFilter}.
+ */
+public class NameFileFilterTest {
+
+ /**
+ * Javadoc example.
+ *
+ * System.out calls are commented out here but not in the Javadoc.
+ */
+ @Test
+ public void testJavadocExampleUsingIo() {
+ File dir = new File(".");
+ String[] files = dir.list(new NameFileFilter("NOTICE.txt"));
+ for (String file : files) {
+ // System.out.println(file);
+ }
+ // End of Javadoc example
+ assertEquals(1, files.length);
+ }
+
+ /**
+ * Javadoc example.
+ *
+ * System.out calls are commented out here but not in the Javadoc.
+ */
+ @Test
+ public void testJavadocExampleUsingNio() throws IOException {
+ final Path dir = Paths.get(".");
+ // We are interested in files older than one day
+ final AccumulatorPathVisitor visitor = AccumulatorPathVisitor
+ .withLongCounters(new NameFileFilter("NOTICE.txt"));
+ //
+ // Walk one dir
+ Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
+ // System.out.println(visitor.getPathCounters());
+ // System.out.println(visitor.getFileList());
+ // System.out.println(visitor.getDirList());
+ assertEquals(1, visitor.getPathCounters().getFileCounter().get());
+ assertEquals(1, visitor.getPathCounters().getDirectoryCounter().get());
+ assertTrue(visitor.getPathCounters().getByteCounter().get() > 0);
+ assertFalse(visitor.getDirList().isEmpty());
+ assertFalse(visitor.getFileList().isEmpty());
+ assertEquals(1, visitor.getFileList().size());
+ //
+ visitor.getPathCounters().reset();
+ //
+ // Walk dir tree
+ Files.walkFileTree(dir, visitor);
+ // System.out.println(visitor.getPathCounters());
+ // System.out.println(visitor.getDirList());
+ // System.out.println(visitor.getFileList());
+ //
+ // End of Javadoc example
+ assertTrue(visitor.getPathCounters().getFileCounter().get() > 0);
+ assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
+ assertTrue(visitor.getPathCounters().getByteCounter().get() > 0);
+ // We counted and accumulated
+ assertFalse(visitor.getDirList().isEmpty());
+ assertFalse(visitor.getFileList().isEmpty());
+ //
+ assertNotEquals(Counters.noopPathCounters(), visitor.getPathCounters());
+ visitor.getPathCounters().reset();
+ CounterAssertions.assertZeroCounters(visitor.getPathCounters());
+ }
+
+ @Test
+ public void testNoCounting() throws IOException {
+ final Path dir = Paths.get(".");
+ final AccumulatorPathVisitor visitor = new AccumulatorPathVisitor(Counters.noopPathCounters(),
+ new NameFileFilter("NOTICE.txt"));
+ Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
+ //
+ CounterAssertions.assertZeroCounters(visitor.getPathCounters());
+ // We did not count, but we still accumulated
+ assertFalse(visitor.getDirList().isEmpty());
+ assertFalse(visitor.getFileList().isEmpty());
+ }
+}

Only accumulate files and dirs accepted by the filter.