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 ff6c3ec5 Add PathUtils.getBaseName(Path)
ff6c3ec5 is described below
commit ff6c3ec5ea4a08c590fa2135430e6f5265790b5f
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Jan 13 15:17:51 2024 -0500
Add PathUtils.getBaseName(Path)
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/io/file/PathUtils.java | 21 +++++++++++++++++++--
.../org/apache/commons/io/file/PathUtilsTest.java | 21 +++++++++++++++++++++
3 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6451d0dc..cd8fdcd0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -107,6 +107,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add FileTimes.toUnixTime(FileTime).</action>
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add BrokenInputStream.Builder.</action>
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add PathUtils.getExtension(Path).</action>
+ <action dev="ggregory" type="add" due-to="Gary
Gregory">Add PathUtils.getBaseName(Path).</action>
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add ThrottledInputStream.</action>
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add IORunnable.noop().</action>
<action dev="ggregory" type="add" due-to="Gary
Gregory">Add ChecksumInputStream and test #548.</action>
diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java
b/src/main/java/org/apache/commons/io/file/PathUtils.java
index 30d0add1..5812cc7a 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -883,8 +883,7 @@ public final class PathUtils {
* </p>
*
* @param path the path to query.
- * @return the extension of the file or an empty string if none exists or
{@code null}
- * if the fileName is {@code null}.
+ * @return the extension of the file or an empty string if none exists or
{@code null} if the fileName is {@code null}.
* @since 2.16.0
*/
public static String getExtension(final Path path) {
@@ -1856,6 +1855,24 @@ public final class PathUtils {
return path;
}
+ /**
+ * Gets the base name (the part up to and not including the last ".") of
the last path segment of a file name.
+ * <p>
+ * Will return the file name itself if it doesn't contain any dots. All
leading directories of the {@code file name} parameter are skipped.
+ * </p>
+ *
+ * @return the base name of file name
+ * @param path the path of the file to obtain the base name of.
+ * @since 2.16.0
+ */
+ public static String getBaseName(final Path path) {
+ if (path == null) {
+ return null;
+ }
+ final Path fileName = path.getFileName();
+ return fileName != null ?
FilenameUtils.removeExtension(fileName.toString()) : null;
+ }
+
/**
* Prevents instantiation.
*/
diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
index 22514451..74c45400 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
@@ -246,6 +246,27 @@ public class PathUtilsTest extends AbstractTempDirTest {
assertEquals(symlinkedDir,
PathUtils.createParentDirectories(symlinkedDir.resolve("child")));
}
+ @Test
+ public void testGetBaseNamePathBaseCases() {
+ assertEquals("bar", PathUtils.getBaseName(Paths.get("a/b/c/bar.foo")));
+ assertEquals("foo", PathUtils.getBaseName(Paths.get("foo")));
+ assertEquals("", PathUtils.getBaseName(Paths.get("")));
+ assertEquals("", PathUtils.getBaseName(Paths.get(".")));
+ for (final File f : File.listRoots()) {
+ assertNull(PathUtils.getBaseName(f.toPath()));
+ }
+ if (SystemUtils.IS_OS_WINDOWS) {
+ assertNull(PathUtils.getBaseName(Paths.get("C:\\")));
+ }
+ }
+
+ @Test
+ public void testGetBaseNamePathCornerCases() {
+ assertNull(PathUtils.getBaseName((Path) null));
+ assertEquals("foo", PathUtils.getBaseName(Paths.get("foo.")));
+ assertEquals("", PathUtils.getBaseName(Paths.get("bar/.foo")));
+ }
+
@Test
public void testGetExtension() {
assertNull(PathUtils.getExtension(null));