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 2019/10/10 17:48:13 UTC
[commons-io] branch master updated: [IO-631] Add a
CountingFileVisitor (as the basis for a forthcoming DeletingFileVisitor).
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 7adcc09 [IO-631] Add a CountingFileVisitor (as the basis for a forthcoming DeletingFileVisitor).
7adcc09 is described below
commit 7adcc09a6c291ccde913bd88db45017cfd48a394
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Oct 10 13:48:09 2019 -0400
[IO-631] Add a CountingFileVisitor (as the basis for a forthcoming
DeletingFileVisitor).
---
src/changes/changes.xml | 3 +
.../commons/io/file/CountingFileVisitor.java | 85 ++++++++++++++++++++++
.../commons/io/file/CountingFileVisitorTest.java | 85 ++++++++++++++++++++++
.../commons/io/dirs-1-file-size-0/file-size-0.bin | 0
.../commons/io/dirs-1-file-size-1/file-size-1.bin | 1 +
.../dirs-a-file-size-1/file-size-1.bin | 1 +
.../dirs-b-file-size-1/file-size-1.bin | 1 +
7 files changed, 176 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f3ef725..d08df13 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -143,6 +143,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-630" dev="ggregory" type="update" due-to="Gary Gregory">
Deprecate org.apache.commons.io.output.NullOutputStream.NullOutputStream() in favor of org.apache.commons.io.output.NullOutputStream.NULL_OUTPUT_STREAM.
</action>
+ <action issue="IO-631" dev="ggregory" type="add" due-to="Gary Gregory">
+ Add a CountingFileVisitor (as the basis for a forthcoming DeletingFileVisitor).
+ </action>
</release>
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">
diff --git a/src/main/java/org/apache/commons/io/file/CountingFileVisitor.java b/src/main/java/org/apache/commons/io/file/CountingFileVisitor.java
new file mode 100644
index 0000000..4c882ca
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/file/CountingFileVisitor.java
@@ -0,0 +1,85 @@
+/*
+ * 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.file;
+
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Counts files, directories, and sizes, as a visit proceeds.
+ */
+public class CountingFileVisitor extends SimpleFileVisitor<Path> {
+
+ private final AtomicLong byteCount = new AtomicLong();
+ private final AtomicLong directoryCount = new AtomicLong();
+ private final AtomicLong fileCount = new AtomicLong();
+
+ /**
+ * Gets the byte count of visited files.
+ *
+ * @return the byte count of visited files.
+ */
+ public long getByteCount() {
+ return this.byteCount.get();
+ }
+
+ /**
+ * Gets the count of visited directories.
+ *
+ * @return the count of visited directories.
+ */
+ public long getDirectoryCount() {
+ return this.directoryCount.get();
+ }
+
+ /**
+ * Gets the count of visited files.
+ *
+ * @return the byte count of visited files.
+ */
+ public long getFileCount() {
+ return this.fileCount.get();
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
+ directoryCount.incrementAndGet();
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%,d files in %,d directories for %,d bytes", Long.valueOf(fileCount.longValue()),
+ Long.valueOf(directoryCount.longValue()), Long.valueOf(byteCount.longValue()));
+ }
+
+ @Override
+ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
+ if (Files.exists(file)) {
+ fileCount.incrementAndGet();
+ byteCount.addAndGet(attrs.size());
+ }
+ return FileVisitResult.CONTINUE;
+ }
+
+}
diff --git a/src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java b/src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java
new file mode 100644
index 0000000..b65e783
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.file;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link CountingFileVisitor}.
+ */
+public class CountingFileVisitorTest {
+
+ /**
+ * Tests an empty folder.
+ */
+ @Test
+ public void testEmptyFolder() throws IOException {
+ final Path tempDirectory = Files.createTempDirectory(getClass().getCanonicalName());
+ try {
+ final CountingFileVisitor visitor = new CountingFileVisitor();
+ Files.walkFileTree(tempDirectory, visitor);
+ Assertions.assertEquals(1, visitor.getDirectoryCount());
+ Assertions.assertEquals(0, visitor.getFileCount());
+ Assertions.assertEquals(0, visitor.getByteCount());
+ } finally {
+ Files.delete(tempDirectory);
+ }
+ }
+
+ /**
+ * Tests a directory with one file of size 0.
+ */
+ @Test
+ public void testFolders1FileSize0() throws IOException {
+ final CountingFileVisitor visitor = new CountingFileVisitor();
+ Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), visitor);
+ Assertions.assertEquals(1, visitor.getDirectoryCount());
+ Assertions.assertEquals(1, visitor.getFileCount());
+ Assertions.assertEquals(0, visitor.getByteCount());
+ }
+
+ /**
+ * Tests a directory with one file of size 1.
+ */
+ @Test
+ public void testFolders1FileSize1() throws IOException {
+ final CountingFileVisitor visitor = new CountingFileVisitor();
+ Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), visitor);
+ Assertions.assertEquals(1, visitor.getDirectoryCount());
+ Assertions.assertEquals(1, visitor.getFileCount());
+ Assertions.assertEquals(1, visitor.getByteCount());
+ }
+
+ /**
+ * Tests a directory with two subdirectorys, each containing one file of size 1.
+ */
+ @Test
+ public void testFolders2FileSize2() throws IOException {
+ final CountingFileVisitor visitor = new CountingFileVisitor();
+ Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), visitor);
+ Assertions.assertEquals(3, visitor.getDirectoryCount());
+ Assertions.assertEquals(2, visitor.getFileCount());
+ Assertions.assertEquals(2, visitor.getByteCount());
+ }
+}
diff --git a/src/test/resources/org/apache/commons/io/dirs-1-file-size-0/file-size-0.bin b/src/test/resources/org/apache/commons/io/dirs-1-file-size-0/file-size-0.bin
new file mode 100644
index 0000000..e69de29
diff --git a/src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin b/src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin
new file mode 100644
index 0000000..2e65efe
--- /dev/null
+++ b/src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin
@@ -0,0 +1 @@
+a
\ No newline at end of file
diff --git a/src/test/resources/org/apache/commons/io/dirs-2-file-size-2/dirs-a-file-size-1/file-size-1.bin b/src/test/resources/org/apache/commons/io/dirs-2-file-size-2/dirs-a-file-size-1/file-size-1.bin
new file mode 100644
index 0000000..2e65efe
--- /dev/null
+++ b/src/test/resources/org/apache/commons/io/dirs-2-file-size-2/dirs-a-file-size-1/file-size-1.bin
@@ -0,0 +1 @@
+a
\ No newline at end of file
diff --git a/src/test/resources/org/apache/commons/io/dirs-2-file-size-2/dirs-b-file-size-1/file-size-1.bin b/src/test/resources/org/apache/commons/io/dirs-2-file-size-2/dirs-b-file-size-1/file-size-1.bin
new file mode 100644
index 0000000..2e65efe
--- /dev/null
+++ b/src/test/resources/org/apache/commons/io/dirs-2-file-size-2/dirs-b-file-size-1/file-size-1.bin
@@ -0,0 +1 @@
+a
\ No newline at end of file