Skip to content

Commit 91bd06a

Browse files
author
Gary Gregory
committed
[IO-733] RegexFileFilter uses the path and file name instead of just the
file name. Add RegexFileFilter.RegexFileFilter(Pattern, Function<Path, String>).
1 parent a41080e commit 91bd06a

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

src/changes/changes.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,17 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.10.0" date="2021-MM-DD" description="Java 8 required.">
50+
<!-- FIX -->
51+
<action issue="IO-733" dev="ggregory" type="fix" due-to="Jim Sellers, Gary Gregory">
52+
RegexFileFilter uses the path and file name instead of just the file name.
53+
</action>
5054
<!-- ADD -->
5155
<action dev="ggregory" type="add" due-to="Gary Gregory">
5256
Add RegexFileFilter.toString().
5357
</action>
58+
<action dev="ggregory" type="add" due-to="Gary Gregory">
59+
Add RegexFileFilter.RegexFileFilter(Pattern, Function&lt;Path&gt;, String>)
60+
</action>
5461
</release>
5562
<release version="2.9.0" date="2021-05-22" description="Java 8 required.">
5663
<!-- FIX -->

src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.FileVisitResult;
2222
import java.nio.file.Path;
2323
import java.nio.file.attribute.BasicFileAttributes;
24+
import java.util.function.Function;
2425
import java.util.regex.Pattern;
2526

2627
import org.apache.commons.io.IOCase;
@@ -72,7 +73,7 @@ public class RegexFileFilter extends AbstractFileFilter implements Serializable
7273
/**
7374
* Compiles the given pattern source.
7475
*
75-
* @param pattern the source pattern
76+
* @param pattern the source pattern.
7677
* @param flags the compilation flags.
7778
* @return a new Pattern.
7879
*/
@@ -97,20 +98,36 @@ private static int toFlags(final IOCase caseSensitivity) {
9798
return flags;
9899
}
99100

100-
/** The regular expression pattern that will be used to match file names */
101+
/** The regular expression pattern that will be used to match file names. */
101102
private final Pattern pattern;
103+
104+
/** How convert a path to a string. */
105+
private final Function<Path, String> pathToString;
102106

103107
/**
104108
* Constructs a new regular expression filter for a compiled regular expression
105109
*
106-
* @param pattern regular expression to match
107-
* @throws IllegalArgumentException if the pattern is null
110+
* @param pattern regular expression to match.
111+
* @throws IllegalArgumentException if the pattern is null.
108112
*/
109113
public RegexFileFilter(final Pattern pattern) {
114+
this(pattern, p -> p.getFileName().toString());
115+
}
116+
117+
/**
118+
* Constructs a new regular expression filter for a compiled regular expression
119+
*
120+
* @param pattern regular expression to match.
121+
* @param pathToString How convert a path to a string.
122+
* @throws IllegalArgumentException if the pattern is null.
123+
* @since 2.10.0
124+
*/
125+
public RegexFileFilter(final Pattern pattern, final Function<Path, String> pathToString) {
110126
if (pattern == null) {
111127
throw new IllegalArgumentException("Pattern is missing");
112128
}
113129
this.pattern = pattern;
130+
this.pathToString = pathToString;
114131
}
115132

116133
/**
@@ -166,7 +183,7 @@ public boolean accept(final File dir, final String name) {
166183
*/
167184
@Override
168185
public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
169-
return toFileVisitResult(pattern.matcher(path.getFileName().toString()).matches(), path);
186+
return toFileVisitResult(pattern.matcher(pathToString.apply(path)).matches(), path);
170187
}
171188

172189
/**

src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.File;
2323
import java.nio.file.FileVisitResult;
2424
import java.nio.file.Path;
25+
import java.nio.file.Paths;
2526
import java.util.regex.Pattern;
2627

2728
import org.apache.commons.io.IOCase;
@@ -143,4 +144,15 @@ public void testRegexEdgeCases() {
143144
}
144145
}
145146

147+
/**
148+
* Tests https://issues.apache.org/jira/browse/IO-733.
149+
*/
150+
@Test
151+
public void testRegexFileNameOnly() {
152+
final Path path = Paths.get("folder", "Foo.java");
153+
final String patternStr = "Foo.*";
154+
assertFiltering(new RegexFileFilter(patternStr), path, true);
155+
assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), Path::toString), path, false);
156+
}
157+
146158
}

0 commit comments

Comments
 (0)