Add PathUtils for operations on NIO Path.
Refactor, add PathUtils.deleteDirectory(), add countDirectory().
RENAME
src/main/java/org/apache/commons/io/file/CountingFileVisitor.java
src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java
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;
*
* @since 2.7
*/
-public class CountingFileVisitor extends SimpleFileVisitor<Path> {
+public class CountingPathFileVisitor extends SimplePathFileVisitor {
private final AtomicLong byteCount = new AtomicLong();
private final AtomicLong directoryCount = new AtomicLong();

RENAME
src/main/java/org/apache/commons/io/file/DeletingFileVisitor.java
src/main/java/org/apache/commons/io/file/DeletingPathFileVisitor.java
*
* @since 2.7
*/
-public class DeletingFileVisitor extends CountingFileVisitor {
+public class DeletingPathFileVisitor extends CountingPathFileVisitor {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private final String[] skip;
*
* @param skip The files to skip deleting.
*/
- public DeletingFileVisitor(final String... skip) {
+ public DeletingPathFileVisitor(final String... skip) {
final String[] temp = skip != null ? skip.clone() : EMPTY_STRING_ARRAY;
Arrays.sort(temp);
this.skip = temp;

MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
import java.io.IOException;
import java.nio.file.DirectoryStream;
+import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
*
* @since 2.7
*/
-public class PathUtils {
+public final class PathUtils {
+
+ /**
+ * Counts aspects of a directory including sub-directories.
+ *
+ * @param directory directory to delete.
+ * @return The visitor used to count the given directory.
+ * @throws IOException if an I/O error is thrown by a visitor method.
+ */
+ public static CountingPathFileVisitor countDirectory(final Path directory) throws IOException {
+ return visitFileTree(directory, new CountingPathFileVisitor());
+ }
+
+ /**
+ * Deletes a directory including sub-directories.
+ *
+ * @param directory directory to delete.
+ * @return The visitor used to delete the given directory.
+ * @throws IOException if an I/O error is thrown by a visitor method.
+ */
+ public static DeletingPathFileVisitor deleteDirectory(final Path directory) throws IOException {
+ return visitFileTree(directory, new DeletingPathFileVisitor());
+ }
/**
* Returns whether the given file or directory is empty.
@@ -67,4 +90,26 @@ public static boolean isEmptyFile(final Path file) throws IOException {
return Files.size(file) <= 0;
}
+ /**
+ * Performs {@link Files#walkFileTree(Path,FileVisitor)} and returns the given visitor.
+ *
+ * Note that {@link Files#walkFileTree(Path,FileVisitor)} returns the given path.
+ *
+ * @param directory See {@link Files#walkFileTree(Path,FileVisitor)}.
+ * @param visitor See {@link Files#walkFileTree(Path,FileVisitor)}.
+ * @param <T> See {@link Files#walkFileTree(Path,FileVisitor)}.
+ * @return the given visitor.
+ *
+ * @throws IOException if an I/O error is thrown by a visitor method
+ */
+ public static <T extends FileVisitor<? super Path>> T visitFileTree(final Path directory, final T visitor)
+ throws IOException {
+ Files.walkFileTree(directory, visitor);
+ return visitor;
+ }
+
+ private PathUtils() {
+ // do not instantiate.
+ }
+
}

ADD
src/main/java/org/apache/commons/io/file/SimplePathFileVisitor.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.file;
+
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+
+/**
+ * A {@link SimpleFileVisitor} typed to a {@link Path}.
+ *
+ * @since 2.7
+ */
+public abstract class SimplePathFileVisitor extends SimpleFileVisitor<Path> {
+
+ /**
+ * Constructs a new instance.
+ */
+ protected SimplePathFileVisitor() {
+ super();
+ }
+
+}

MODIFY
src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java
src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java
import org.junit.jupiter.api.Test;
/**
- * Tests {@link CountingFileVisitor}.
+ * Tests {@link CountingPathFileVisitor}.
*/
public class CountingFileVisitorTest {
* Tests an empty folder.
*/
@Test
- public void testEmptyFolder() throws IOException {
+ public void testCountEmptyFolder() throws IOException {
final Path tempDirectory = Files.createTempDirectory(getClass().getCanonicalName());
try {
- final CountingFileVisitor visitor = new CountingFileVisitor();
+ final CountingPathFileVisitor visitor = new CountingPathFileVisitor();
Files.walkFileTree(tempDirectory, visitor);
Assertions.assertEquals(1, visitor.getDirectoryCount());
Assertions.assertEquals(0, visitor.getFileCount());
@@ -51,8 +51,8 @@ public void testEmptyFolder() throws IOException {
* Tests a directory with one file of size 0.
*/
@Test
- public void testFolders1FileSize0() throws IOException {
- final CountingFileVisitor visitor = new CountingFileVisitor();
+ public void testCountFolders1FileSize0() throws IOException {
+ final CountingPathFileVisitor visitor = new CountingPathFileVisitor();
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());
@@ -63,8 +63,8 @@ public void testFolders1FileSize0() throws IOException {
* Tests a directory with one file of size 1.
*/
@Test
- public void testFolders1FileSize1() throws IOException {
- final CountingFileVisitor visitor = new CountingFileVisitor();
+ public void testCountFolders1FileSize1() throws IOException {
+ final CountingPathFileVisitor visitor = new CountingPathFileVisitor();
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());
@@ -75,16 +75,17 @@ public void testFolders1FileSize1() throws IOException {
* Tests a directory with two subdirectorys, each containing one file of size 1.
*/
@Test
- public void testFolders2FileSize2() throws IOException {
- final CountingFileVisitor visitor = new CountingFileVisitor();
+ public void testCountFolders2FileSize2() throws IOException {
+ final CountingPathFileVisitor visitor = new CountingPathFileVisitor();
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());
}
- @Test void testToString() {
+ @Test
+ void testToString() {
// Make sure it does not blow up
- new CountingFileVisitor().toString();
+ new CountingPathFileVisitor().toString();
}
}

MODIFY
src/test/java/org/apache/commons/io/file/DeletingFileVisitorTest.java
src/test/java/org/apache/commons/io/file/DeletingFileVisitorTest.java
import org.junit.jupiter.api.Test;
/**
- * Tests {@link DeletingFileVisitor}.
+ * Tests {@link DeletingPathFileVisitor}.
*/
public class DeletingFileVisitorTest {
@@ -52,13 +52,13 @@ public void beforeEach() throws IOException {
* Tests an empty folder.
*/
@Test
- public void testEmptyDirectory() throws IOException {
- testEmptyDirectory(new DeletingFileVisitor());
+ public void testDeleteEmptyDirectory() throws IOException {
+ testDeleteEmptyDirectory(new DeletingPathFileVisitor());
// This will throw if not empty.
Files.deleteIfExists(tempDirectory);
}
- private void testEmptyDirectory(final CountingFileVisitor visitor) throws IOException {
+ private void testDeleteEmptyDirectory(final DeletingPathFileVisitor visitor) throws IOException {
Files.walkFileTree(tempDirectory, visitor);
Assertions.assertEquals(1, visitor.getDirectoryCount());
Assertions.assertEquals(0, visitor.getFileCount());
@@ -69,8 +69,8 @@ private void testEmptyDirectory(final CountingFileVisitor visitor) throws IOExce
* Tests an empty folder.
*/
@Test
- public void testEmptyDirectoryNullCtorArg() throws IOException {
- testEmptyDirectory(new DeletingFileVisitor((String[]) null));
+ public void testDeleteEmptyDirectoryNullCtorArg() throws IOException {
+ testDeleteEmptyDirectory(new DeletingPathFileVisitor((String[]) null));
// This will throw if not empty.
Files.deleteIfExists(tempDirectory);
}
@@ -79,10 +79,10 @@ public void testEmptyDirectoryNullCtorArg() throws IOException {
* Tests a directory with one file of size 0.
*/
@Test
- public void testFolders1FileSize0() throws IOException {
+ public void testDeleteFolders1FileSize0() throws IOException {
FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0").toFile(),
tempDirectory.toFile());
- final CountingFileVisitor visitor = new DeletingFileVisitor();
+ final CountingPathFileVisitor visitor = new DeletingPathFileVisitor();
Files.walkFileTree(tempDirectory, visitor);
Assertions.assertEquals(1, visitor.getDirectoryCount());
Assertions.assertEquals(1, visitor.getFileCount());
@@ -95,10 +95,10 @@ public void testFolders1FileSize0() throws IOException {
* Tests a directory with one file of size 1.
*/
@Test
- public void testFolders1FileSize1() throws IOException {
+ public void testDeleteFolders1FileSize1() throws IOException {
FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1").toFile(),
tempDirectory.toFile());
- final CountingFileVisitor visitor = new DeletingFileVisitor();
+ final CountingPathFileVisitor visitor = new DeletingPathFileVisitor();
Files.walkFileTree(tempDirectory, visitor);
Assertions.assertEquals(1, visitor.getDirectoryCount());
Assertions.assertEquals(1, visitor.getFileCount());
@@ -111,11 +111,11 @@ public void testFolders1FileSize1() throws IOException {
* Tests a directory with one file of size 1 but skip that file.
*/
@Test
- public void testFolders1FileSize1Skip() throws IOException {
+ public void testDeleteFolders1FileSize1Skip() throws IOException {
FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1").toFile(),
tempDirectory.toFile());
final String skipFileName = "file-size-1.bin";
- final CountingFileVisitor visitor = new DeletingFileVisitor(skipFileName);
+ final CountingPathFileVisitor visitor = new DeletingPathFileVisitor(skipFileName);
Files.walkFileTree(tempDirectory, visitor);
Assertions.assertEquals(1, visitor.getDirectoryCount());
Assertions.assertEquals(1, visitor.getFileCount());
@@ -129,10 +129,10 @@ public void testFolders1FileSize1Skip() throws IOException {
* Tests a directory with two subdirectorys, each containing one file of size 1.
*/
@Test
- public void testFolders2FileSize2() throws IOException {
+ public void testDeleteFolders2FileSize2() throws IOException {
FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2").toFile(),
tempDirectory.toFile());
- final CountingFileVisitor visitor = new DeletingFileVisitor();
+ final CountingPathFileVisitor visitor = new DeletingPathFileVisitor();
Files.walkFileTree(tempDirectory, visitor);
Assertions.assertEquals(3, visitor.getDirectoryCount());
Assertions.assertEquals(2, visitor.getFileCount());

ADD
src/test/java/org/apache/commons/io/file/PathUtilsCountingTest.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.file;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.commons.io.file.PathUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link PathUtils}.
+ */
+public class PathUtilsCountingTest {
+
+ private static final Path DIR_SIZE_1 = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1");
+
+ private static final Path FILE_SIZE_0 = Paths
+ .get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/file-size-0.bin");
+
+ private static final Path FILE_SIZE_1 = Paths
+ .get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
+
+ /**
+ * Tests an empty folder.
+ */
+ @Test
+ public void testCountEmptyFolder() throws IOException {
+ final Path tempDirectory = Files.createTempDirectory(getClass().getCanonicalName());
+ try {
+ final CountingPathFileVisitor visitor = PathUtils.countDirectory(tempDirectory);
+ Assertions.assertEquals(1, visitor.getDirectoryCount());
+ Assertions.assertEquals(0, visitor.getFileCount());
+ Assertions.assertEquals(0, visitor.getByteCount());
+ } finally {
+ Files.deleteIfExists(tempDirectory);
+ }
+ }
+
+ /**
+ * Tests a directory with one file of size 0.
+ */
+ @Test
+ public void testCountFolders1FileSize0() throws IOException {
+ final CountingPathFileVisitor visitor = PathUtils
+ .countDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"));
+ 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 testCountFolders1FileSize1() throws IOException {
+ final CountingPathFileVisitor visitor = PathUtils
+ .countDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"));
+ 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 testCountFolders2FileSize2() throws IOException {
+ final CountingPathFileVisitor visitor = PathUtils
+ .countDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"));
+ Assertions.assertEquals(3, visitor.getDirectoryCount());
+ Assertions.assertEquals(2, visitor.getFileCount());
+ Assertions.assertEquals(2, visitor.getByteCount());
+ }
+}

ADD
src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.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.file;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link DeletingPathFileVisitor}.
+ */
+public class PathUtilsDeletingFileVisitorTest {
+
+ private Path tempDirectory;
+
+ @AfterEach
+ public void afterEach() throws IOException {
+ // backstop
+ if (Files.exists(tempDirectory) && PathUtils.isEmptyDirectory(tempDirectory)) {
+ Files.deleteIfExists(tempDirectory);
+ }
+ }
+
+ @BeforeEach
+ public void beforeEach() throws IOException {
+ tempDirectory = Files.createTempDirectory(getClass().getCanonicalName());
+ }
+
+ /**
+ * Tests an empty folder.
+ */
+ @Test
+ public void testDeleteEmptyDirectory() throws IOException {
+ testDeleteEmptyDirectory(PathUtils.deleteDirectory(tempDirectory));
+ // This will throw if not empty.
+ Files.deleteIfExists(tempDirectory);
+ }
+
+ private void testDeleteEmptyDirectory(final DeletingPathFileVisitor visitor) throws IOException {
+ Assertions.assertEquals(1, visitor.getDirectoryCount());
+ Assertions.assertEquals(0, visitor.getFileCount());
+ Assertions.assertEquals(0, visitor.getByteCount());
+ }
+
+ /**
+ * Tests a directory with one file of size 0.
+ */
+ @Test
+ public void testDeleteFolders1FileSize0() throws IOException {
+ FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0").toFile(),
+ tempDirectory.toFile());
+ final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory);
+ Assertions.assertEquals(1, visitor.getDirectoryCount());
+ Assertions.assertEquals(1, visitor.getFileCount());
+ Assertions.assertEquals(0, visitor.getByteCount());
+ // This will throw if not empty.
+ Files.deleteIfExists(tempDirectory);
+ }
+
+ /**
+ * Tests a directory with one file of size 1.
+ */
+ @Test
+ public void testDeleteFolders1FileSize1() throws IOException {
+ FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1").toFile(),
+ tempDirectory.toFile());
+ final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory);
+ Assertions.assertEquals(1, visitor.getDirectoryCount());
+ Assertions.assertEquals(1, visitor.getFileCount());
+ Assertions.assertEquals(1, visitor.getByteCount());
+ // This will throw if not empty.
+ Files.deleteIfExists(tempDirectory);
+ }
+
+ /**
+ * Tests a directory with two subdirectorys, each containing one file of size 1.
+ */
+ @Test
+ public void testDeleteFolders2FileSize2() throws IOException {
+ FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2").toFile(),
+ tempDirectory.toFile());
+ final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory);
+ Assertions.assertEquals(3, visitor.getDirectoryCount());
+ Assertions.assertEquals(2, visitor.getFileCount());
+ Assertions.assertEquals(2, visitor.getByteCount());
+ // This will throw if not empty.
+ Files.deleteIfExists(tempDirectory);
+ }
+}

RENAME
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java
/**
* Tests {@link PathUtils}.
*/
-public class PathUtilsTest {
+public class PathUtilsIsEmptyTest {
private static final Path DIR_SIZE_1 = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1");

[IO-632] Add PathUtils for operations on NIO Path.

Refactor, add PathUtils.deleteDirectory(), add countDirectory().