You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/10/14 23:13:07 UTC
[commons-io] branch master updated: Only accumulate files and dirs
accepted by the filter.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new 6803ac1 Only accumulate files and dirs accepted by the filter.
6803ac1 is described below
commit 6803ac145c274546d0f2e06374a9723a4d4d7ce6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Oct 14 19:13:02 2020 -0400
Only accumulate files and dirs accepted by the filter.
---
pom.xml | 8 +-
.../commons/io/file/AccumulatorPathVisitor.java | 37 ++++---
.../commons/io/file/CountingPathVisitor.java | 13 ++-
.../commons/io/filefilter/NameFileFilterTest.java | 114 +++++++++++++++++++++
4 files changed, 148 insertions(+), 24 deletions(-)
diff --git a/pom.xml b/pom.xml
index 775a629..ddbff85 100644
--- a/pom.xml
+++ b/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>
diff --git a/src/main/java/org/apache/commons/io/file/AccumulatorPathVisitor.java b/src/main/java/org/apache/commons/io/file/AccumulatorPathVisitor.java
index 7ff43f0..9673844 100644
--- a/src/main/java/org/apache/commons/io/file/AccumulatorPathVisitor.java
+++ b/src/main/java/org/apache/commons/io/file/AccumulatorPathVisitor.java
@@ -18,7 +18,6 @@
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 class AccumulatorPathVisitor extends CountingPathVisitor {
}
/**
- * 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 class AccumulatorPathVisitor extends CountingPathVisitor {
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 class AccumulatorPathVisitor extends CountingPathVisitor {
}
@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);
}
}
diff --git a/src/main/java/org/apache/commons/io/file/CountingPathVisitor.java b/src/main/java/org/apache/commons/io/file/CountingPathVisitor.java
index a7125f5..c9c4185 100644
--- a/src/main/java/org/apache/commons/io/file/CountingPathVisitor.java
+++ b/src/main/java/org/apache/commons/io/file/CountingPathVisitor.java
@@ -108,7 +108,7 @@ public class CountingPathVisitor extends SimplePathVisitor {
@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
- pathCounters.getDirectoryCounter().increment();
+ updateDirCounter(dir, exc);
return FileVisitResult.CONTINUE;
}
@@ -118,6 +118,17 @@ public class CountingPathVisitor extends SimplePathVisitor {
}
/**
+ * 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.
*
* @param file the visited file.
diff --git a/src/test/java/org/apache/commons/io/filefilter/NameFileFilterTest.java b/src/test/java/org/apache/commons/io/filefilter/NameFileFilterTest.java
new file mode 100644
index 0000000..7424acd
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/filefilter/NameFileFilterTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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());
+ }
+}