Let org.apache.commons.io.filefilter classes work with
java.nio.file.Files#newDirectoryStream(Path, DirectoryStream.Filter).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -73,6 +73,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Let org.apache.commons.io.filefilter classes work with java.nio.file.Files.walk* APIs.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Let org.apache.commons.io.filefilter classes work with java.nio.file.Files#newDirectoryStream(Path, DirectoryStream.Filter).
+ </action>
<!-- UPDATES -->
<action dev="ggregory" type="update" due-to="Dependabot">
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

ADD
src/main/java/org/apache/commons/io/filefilter/DirectoryStreamFilter.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 java.io.IOException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Path;
+import java.util.Objects;
+
+/**
+ * A {@link java.nio.file.DirectoryStream.Filter DirectoryStream.Filter} that delegates to a {@link PathFilter}.
+ * <p>
+ * You pass this filter to {@link java.nio.file.Files#newDirectoryStream(Path, DirectoryStream.Filter)
+ * Files#newDirectoryStream(Path, DirectoryStream.Filter)}.
+ * </p>
+ *
+ * @since 2.9.0
+ */
+public class DirectoryStreamFilter implements DirectoryStream.Filter<Path> {
+
+ private final PathFilter pathFilter;
+
+ /**
+ * Constructs a new instance for the given path filter.
+ *
+ * @param pathFilter How to filter paths.
+ */
+ public DirectoryStreamFilter(final PathFilter pathFilter) {
+ super();
+ // TODO Instead of NPE, we could map null to FalseFileFilter.
+ this.pathFilter = Objects.requireNonNull(pathFilter, "pathFilter");
+ }
+
+ @Override
+ public boolean accept(final Path entry) throws IOException {
+ return pathFilter.accept(entry) == FileVisitResult.CONTINUE;
+ }
+
+ /**
+ * Gets the path filter.
+ *
+ * @return the path filter.
+ */
+ public PathFilter getPathFilter() {
+ return pathFilter;
+ }
+
+}

ADD
src/test/java/org/apache/commons/io/filefilter/DirectoryStreamFilterTest.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 java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Iterator;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link DirectoryStreamFilter}.
+ */
+public class DirectoryStreamFilterTest {
+
+ private static final String PATH_FIXTURE = "NOTICE.txt";
+
+ @Test
+ public void testFilterByName() throws Exception {
+ final PathFilter pathFilter = new NameFileFilter(PATH_FIXTURE);
+ final DirectoryStreamFilter streamFilter = new DirectoryStreamFilter(pathFilter);
+ assertEquals(pathFilter, streamFilter.getPathFilter());
+ try (final DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("."), streamFilter)) {
+ final Iterator<Path> iterator = stream.iterator();
+ final Path path = iterator.next();
+ assertEquals(PATH_FIXTURE, path.getFileName().toString());
+ assertFalse(iterator.hasNext());
+ }
+ }
+
+ @Test
+ public void testFilterByNameNot() throws Exception {
+ final PathFilter pathFilter = new NameFileFilter(PATH_FIXTURE).not();
+ final DirectoryStreamFilter streamFilter = new DirectoryStreamFilter(pathFilter);
+ assertEquals(pathFilter, streamFilter.getPathFilter());
+ try (final DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("."), streamFilter)) {
+ for (final Path path : stream) {
+ assertNotEquals(PATH_FIXTURE, path.getFileName().toString());
+ }
+ }
+ }
+}

Let org.apache.commons.io.filefilter classes work with
java.nio.file.Files#newDirectoryStream(Path, DirectoryStream.Filter).