Skip to content

Commit 5438a65

Browse files
author
Gary Gregory
committed
Let org.apache.commons.io.filefilter classes work with
java.nio.file.Files#newDirectoryStream(Path, DirectoryStream.Filter).
1 parent 2b07be5 commit 5438a65

3 files changed

Lines changed: 130 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ The <action> type attribute can be add,update,fix,remove.
7373
<action dev="ggregory" type="add" due-to="Gary Gregory">
7474
Let org.apache.commons.io.filefilter classes work with java.nio.file.Files.walk* APIs.
7575
</action>
76+
<action dev="ggregory" type="add" due-to="Gary Gregory">
77+
Let org.apache.commons.io.filefilter classes work with java.nio.file.Files#newDirectoryStream(Path, DirectoryStream.Filter).
78+
</action>
7679
<!-- UPDATES -->
7780
<action dev="ggregory" type="update" due-to="Dependabot">
7881
Update junit-jupiter from 5.6.2 to 5.7.0 #153.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.filefilter;
19+
20+
import java.io.IOException;
21+
import java.nio.file.DirectoryStream;
22+
import java.nio.file.FileVisitResult;
23+
import java.nio.file.Path;
24+
import java.util.Objects;
25+
26+
/**
27+
* A {@link java.nio.file.DirectoryStream.Filter DirectoryStream.Filter} that delegates to a {@link PathFilter}.
28+
* <p>
29+
* You pass this filter to {@link java.nio.file.Files#newDirectoryStream(Path, DirectoryStream.Filter)
30+
* Files#newDirectoryStream(Path, DirectoryStream.Filter)}.
31+
* </p>
32+
*
33+
* @since 2.9.0
34+
*/
35+
public class DirectoryStreamFilter implements DirectoryStream.Filter<Path> {
36+
37+
private final PathFilter pathFilter;
38+
39+
/**
40+
* Constructs a new instance for the given path filter.
41+
*
42+
* @param pathFilter How to filter paths.
43+
*/
44+
public DirectoryStreamFilter(final PathFilter pathFilter) {
45+
super();
46+
// TODO Instead of NPE, we could map null to FalseFileFilter.
47+
this.pathFilter = Objects.requireNonNull(pathFilter, "pathFilter");
48+
}
49+
50+
@Override
51+
public boolean accept(final Path entry) throws IOException {
52+
return pathFilter.accept(entry) == FileVisitResult.CONTINUE;
53+
}
54+
55+
/**
56+
* Gets the path filter.
57+
*
58+
* @return the path filter.
59+
*/
60+
public PathFilter getPathFilter() {
61+
return pathFilter;
62+
}
63+
64+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.filefilter;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
23+
24+
import java.nio.file.DirectoryStream;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
import java.util.Iterator;
29+
30+
import org.junit.jupiter.api.Test;
31+
32+
/**
33+
* Tests {@link DirectoryStreamFilter}.
34+
*/
35+
public class DirectoryStreamFilterTest {
36+
37+
private static final String PATH_FIXTURE = "NOTICE.txt";
38+
39+
@Test
40+
public void testFilterByName() throws Exception {
41+
final PathFilter pathFilter = new NameFileFilter(PATH_FIXTURE);
42+
final DirectoryStreamFilter streamFilter = new DirectoryStreamFilter(pathFilter);
43+
assertEquals(pathFilter, streamFilter.getPathFilter());
44+
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("."), streamFilter)) {
45+
final Iterator<Path> iterator = stream.iterator();
46+
final Path path = iterator.next();
47+
assertEquals(PATH_FIXTURE, path.getFileName().toString());
48+
assertFalse(iterator.hasNext());
49+
}
50+
}
51+
52+
@Test
53+
public void testFilterByNameNot() throws Exception {
54+
final PathFilter pathFilter = new NameFileFilter(PATH_FIXTURE).not();
55+
final DirectoryStreamFilter streamFilter = new DirectoryStreamFilter(pathFilter);
56+
assertEquals(pathFilter, streamFilter.getPathFilter());
57+
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("."), streamFilter)) {
58+
for (final Path path : stream) {
59+
assertNotEquals(PATH_FIXTURE, path.getFileName().toString());
60+
}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)