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 2021/05/28 16:08:08 UTC
[commons-io] branch master updated: [IO-733] RegexFileFilter uses
the path and file name instead of just the file name.
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 91bd06a [IO-733] RegexFileFilter uses the path and file name instead of just the file name.
91bd06a is described below
commit 91bd06a73ce3b404b58948e011b3fcff342b8ca3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri May 28 12:08:04 2021 -0400
[IO-733] RegexFileFilter uses the path and file name instead of just the
file name.
Add RegexFileFilter.RegexFileFilter(Pattern, Function<Path, String>).
---
src/changes/changes.xml | 7 ++++++
.../commons/io/filefilter/RegexFileFilter.java | 27 ++++++++++++++++++----
.../io/filefilter/RegexFileFilterTestCase.java | 12 ++++++++++
3 files changed, 41 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0f22caa..136e68b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -47,10 +47,17 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<!-- The release date is the date RC is cut -->
<release version="2.10.0" date="2021-MM-DD" description="Java 8 required.">
+ <!-- FIX -->
+ <action issue="IO-733" dev="ggregory" type="fix" due-to="Jim Sellers, Gary Gregory">
+ RegexFileFilter uses the path and file name instead of just the file name.
+ </action>
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add RegexFileFilter.toString().
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add RegexFileFilter.RegexFileFilter(Pattern, Function<Path>, String>)
+ </action>
</release>
<release version="2.9.0" date="2021-05-22" description="Java 8 required.">
<!-- FIX -->
diff --git a/src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java
index 401ff76..9906432 100644
--- a/src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java
+++ b/src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java
@@ -21,6 +21,7 @@ import java.io.Serializable;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
+import java.util.function.Function;
import java.util.regex.Pattern;
import org.apache.commons.io.IOCase;
@@ -72,7 +73,7 @@ public class RegexFileFilter extends AbstractFileFilter implements Serializable
/**
* Compiles the given pattern source.
*
- * @param pattern the source pattern
+ * @param pattern the source pattern.
* @param flags the compilation flags.
* @return a new Pattern.
*/
@@ -97,20 +98,36 @@ public class RegexFileFilter extends AbstractFileFilter implements Serializable
return flags;
}
- /** The regular expression pattern that will be used to match file names */
+ /** The regular expression pattern that will be used to match file names. */
private final Pattern pattern;
+
+ /** How convert a path to a string. */
+ private final Function<Path, String> pathToString;
/**
* Constructs a new regular expression filter for a compiled regular expression
*
- * @param pattern regular expression to match
- * @throws IllegalArgumentException if the pattern is null
+ * @param pattern regular expression to match.
+ * @throws IllegalArgumentException if the pattern is null.
*/
public RegexFileFilter(final Pattern pattern) {
+ this(pattern, p -> p.getFileName().toString());
+ }
+
+ /**
+ * Constructs a new regular expression filter for a compiled regular expression
+ *
+ * @param pattern regular expression to match.
+ * @param pathToString How convert a path to a string.
+ * @throws IllegalArgumentException if the pattern is null.
+ * @since 2.10.0
+ */
+ public RegexFileFilter(final Pattern pattern, final Function<Path, String> pathToString) {
if (pattern == null) {
throw new IllegalArgumentException("Pattern is missing");
}
this.pattern = pattern;
+ this.pathToString = pathToString;
}
/**
@@ -166,7 +183,7 @@ public class RegexFileFilter extends AbstractFileFilter implements Serializable
*/
@Override
public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
- return toFileVisitResult(pattern.matcher(path.getFileName().toString()).matches(), path);
+ return toFileVisitResult(pattern.matcher(pathToString.apply(path)).matches(), path);
}
/**
diff --git a/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java b/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
index 8eaa1d9..79a39fa 100644
--- a/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
+++ b/src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.regex.Pattern;
import org.apache.commons.io.IOCase;
@@ -143,4 +144,15 @@ public class RegexFileFilterTestCase {
}
}
+ /**
+ * Tests https://issues.apache.org/jira/browse/IO-733.
+ */
+ @Test
+ public void testRegexFileNameOnly() {
+ final Path path = Paths.get("folder", "Foo.java");
+ final String patternStr = "Foo.*";
+ assertFiltering(new RegexFileFilter(patternStr), path, true);
+ assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), Path::toString), path, false);
+ }
+
}