Skip to content

Commit a8ee7b9

Browse files
committed
Add and use PathUtils.getFileNameString()
1 parent 40af9d6 commit a8ee7b9

13 files changed

Lines changed: 72 additions & 27 deletions

File tree

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<modelVersion>4.0.0</modelVersion>
2525
<groupId>commons-io</groupId>
2626
<artifactId>commons-io</artifactId>
27-
<version>2.15.2-SNAPSHOT</version>
27+
<version>2.16.0-SNAPSHOT</version>
2828
<name>Apache Commons IO</name>
2929

3030
<inceptionYear>2002</inceptionYear>
@@ -317,9 +317,9 @@ file comparators, endian transformation classes, and much more.
317317
<commons.componentid>io</commons.componentid>
318318
<commons.module.name>org.apache.commons.io</commons.module.name>
319319
<commons.rc.version>RC1</commons.rc.version>
320-
<commons.bc.version>2.15.0</commons.bc.version>
321-
<commons.release.version>2.15.1</commons.release.version>
322-
<commons.release.next>2.15.2</commons.release.next>
320+
<commons.bc.version>2.15.1</commons.bc.version>
321+
<commons.release.version>2.16.0</commons.release.version>
322+
<commons.release.next>2.16.1</commons.release.next>
323323
<commons.release.desc>(requires Java 8)</commons.release.desc>
324324
<commons.jira.id>IO</commons.jira.id>
325325
<commons.jira.pid>12310477</commons.jira.pid>

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ The <action> type attribute can be add,update,fix,remove.
4747
</properties>
4848

4949
<body>
50-
<release version="2.15.2" date="202Y-MM-DD" description="Java 8 is required.">
50+
<release version="2.16.0" date="202Y-MM-DD" description="Java 8 is required.">
5151
<!-- Fix -->
5252
<action dev="ggregory" type="fix" due-to="Elliotte Rusty Harold">Fix and reenable testSkip_RequiredCharsets #518.</action>
5353
<action dev="ggregory" type="fix" issue="IO-824" due-to="Miguel Munoz, Gary Gregory">SymbolicLineFileFilter documentation fixes.</action>
5454
<action dev="ggregory" type="fix" issue="IO-795" due-to="Miguel Munoz, Gary Gregory">CharSequenceInputStream.reset() only works once #520.</action>
5555
<action dev="ggregory" type="fix" issue="IO-825" due-to="Arthur Chan, Gary Gregory">Add byte array size validation for methods in EndianUtils #521.</action>
5656
<action dev="ggregory" type="fix" issue="IO-825" due-to="dkdal, Gary Gregory">Add missing test case CircularByteBufferTest.</action>
57+
<!-- Add -->
58+
<action dev="ggregory" type="fix" issue="IO-825" due-to="dkdal, Gary Gregory">Add and use PathUtils.getFileName(Path, Function&lt;Path, R&gt;).</action>
59+
<action dev="ggregory" type="fix" issue="IO-825" due-to="dkdal, Gary Gregory">Add and use PathUtils.getFileNameString().</action>
5760
</release>
5861
<release version="2.15.1" date="2023-11-24" description="Java 8 is required.">
5962
<!-- FIX -->

src/main/java/org/apache/commons/io/file/CleaningPathVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public CleaningPathVisitor(final PathCounters pathCounter, final String... skip)
8989
* @return true to process the given path, false if not.
9090
*/
9191
private boolean accept(final Path path) {
92-
return Arrays.binarySearch(skip, Objects.toString(path.getFileName(), null)) < 0;
92+
return Arrays.binarySearch(skip, PathUtils.getFileNameString(path)) < 0;
9393
}
9494

9595
@Override

src/main/java/org/apache/commons/io/file/DeletingPathVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public DeletingPathVisitor(final PathCounters pathCounter, final String... skip)
107107
* @return true to process the given path, false if not.
108108
*/
109109
private boolean accept(final Path path) {
110-
return Arrays.binarySearch(skip, Objects.toString(path.getFileName(), null)) < 0;
110+
return Arrays.binarySearch(skip, PathUtils.getFileNameString(path)) < 0;
111111
}
112112

113113
@Override

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import java.util.List;
6464
import java.util.Objects;
6565
import java.util.Set;
66+
import java.util.function.Function;
6667
import java.util.stream.Collector;
6768
import java.util.stream.Collectors;
6869
import java.util.stream.Stream;
@@ -866,6 +867,33 @@ public static DosFileAttributeView getDosFileAttributeView(final Path path, fina
866867
return Files.getFileAttributeView(path, DosFileAttributeView.class, options);
867868
}
868869

870+
/**
871+
* Gets the Path's file name and apply the given function if the file name is non-null.
872+
*
873+
* @param <R> The function's result type.
874+
* @param path the path to query.
875+
* @param function function to apply to the file name.
876+
* @return the Path's file name as a string or null.
877+
* @see Path#getFileName()
878+
* @since 2.16.0
879+
*/
880+
public static <R> R getFileName(final Path path, Function<Path, R> function) {
881+
final Path fileName = path.getFileName();
882+
return fileName != null ? function.apply(fileName) : null;
883+
}
884+
885+
/**
886+
* Gets the Path's file name as a string.
887+
*
888+
* @param path the path to query.
889+
* @return the Path's file name as a string or null.
890+
* @see Path#getFileName()
891+
* @since 2.16.0
892+
*/
893+
public static String getFileNameString(final Path path) {
894+
return getFileName(path, Path::toString);
895+
}
896+
869897
/**
870898
* Gets the file's last modified time or null if the file does not exist.
871899
* <p>

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.stream.Stream;
2727

2828
import org.apache.commons.io.IOCase;
29+
import org.apache.commons.io.file.PathUtils;
2930

3031
/**
3132
* Filters file names for a certain name.
@@ -179,14 +180,14 @@ public boolean accept(final File dir, final String name) {
179180

180181
/**
181182
* Checks to see if the file name matches.
182-
* @param file the File to check
183+
* @param path the File to check
183184
*
184185
* @return true if the file name matches
185186
* @since 2.9.0
186187
*/
187188
@Override
188-
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
189-
return toFileVisitResult(acceptBaseName(Objects.toString(file.getFileName(), null)));
189+
public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
190+
return toFileVisitResult(acceptBaseName(PathUtils.getFileNameString(path)));
190191
}
191192

192193
private boolean acceptBaseName(final String baseName) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.stream.Stream;
2727

2828
import org.apache.commons.io.IOCase;
29+
import org.apache.commons.io.file.PathUtils;
2930

3031
/**
3132
* Filters file names for a certain prefix.
@@ -191,8 +192,7 @@ public boolean accept(final File file, final String name) {
191192
*/
192193
@Override
193194
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
194-
final Path fileName = file.getFileName();
195-
return toFileVisitResult(accept(fileName == null ? null : fileName.toFile()));
195+
return toFileVisitResult(accept(PathUtils.getFileName(file, Path::toFile)));
196196
}
197197

198198
private boolean accept(final String name) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.regex.Pattern;
2727

2828
import org.apache.commons.io.IOCase;
29+
import org.apache.commons.io.file.PathUtils;
2930

3031
/**
3132
* Filters files using supplied regular expression(s).
@@ -111,7 +112,7 @@ private static int toFlags(final IOCase ioCase) {
111112
*/
112113
@SuppressWarnings("unchecked")
113114
public RegexFileFilter(final Pattern pattern) {
114-
this(pattern, (Function<Path, String> & Serializable) p -> Objects.toString(p.getFileName(), null));
115+
this(pattern, (Function<Path, String> & Serializable) PathUtils::getFileNameString);
115116
}
116117

117118
/**

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.stream.Stream;
2727

2828
import org.apache.commons.io.IOCase;
29+
import org.apache.commons.io.file.PathUtils;
2930

3031
/**
3132
* Filters files based on the suffix (what the file name ends with).
@@ -185,14 +186,14 @@ public boolean accept(final File file, final String name) {
185186

186187
/**
187188
* Checks to see if the file name ends with the suffix.
188-
* @param file the File to check
189+
* @param path the File to check
189190
*
190191
* @return true if the file name ends with one of our suffixes
191192
* @since 2.9.0
192193
*/
193194
@Override
194-
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
195-
return toFileVisitResult(accept(Objects.toString(file.getFileName(), null)));
195+
public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
196+
return toFileVisitResult(accept(PathUtils.getFileNameString(path)));
196197
}
197198

198199
private boolean accept(final String name) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.commons.io.FilenameUtils;
2929
import org.apache.commons.io.IOCase;
3030
import org.apache.commons.io.build.AbstractSupplier;
31+
import org.apache.commons.io.file.PathUtils;
3132

3233
/**
3334
* Filters files using the supplied wildcards.
@@ -274,14 +275,14 @@ public boolean accept(final File dir, final String name) {
274275
/**
275276
* Checks to see if the file name matches one of the wildcards.
276277
*
277-
* @param file the file to check
278+
* @param path the file to check
278279
*
279280
* @return true if the file name matches one of the wildcards.
280281
* @since 2.9.0
281282
*/
282283
@Override
283-
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
284-
return toFileVisitResult(accept(Objects.toString(file.getFileName(), null)));
284+
public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
285+
return toFileVisitResult(accept(PathUtils.getFileNameString(path)));
285286
}
286287

287288
private boolean accept(final String name) {

0 commit comments

Comments
 (0)