You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2023/02/20 23:29:50 UTC

[groovy] 03/03: GROOVY-10942 Add getName extension method for Path

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 22a01ded99fa6017e3b484cd356d27d454a432bf
Author: Goooler <wa...@gmail.com>
AuthorDate: Mon Feb 20 11:01:19 2023 +0800

    GROOVY-10942 Add getName extension method for Path
---
 .../org/apache/groovy/nio/extensions/NioExtensions.java | 17 +++++++++++++----
 .../groovy/nio/extensions/NioExtensionsTest.groovy      | 10 ++++++++++
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java b/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
index 6720d495ef..c2525750b9 100644
--- a/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
+++ b/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
@@ -101,6 +101,17 @@ public class NioExtensions extends DefaultGroovyMethodsSupport {
         return Files.size(self);
     }
 
+    /**
+     * Provide the name of the file or directory.
+     * @param self a path object
+     * @return the name of the file or directory denoted by this path as a string, or an empty string if this path has zero path elements.
+     * @since 5.0.0
+     */
+    public static String getName(Path self) {
+        final Path fileName = self.getFileName();
+        return fileName == null ? "" : fileName.toString();
+    }
+
     /**
      * Provide the extension of this path.
      *
@@ -109,8 +120,7 @@ public class NioExtensions extends DefaultGroovyMethodsSupport {
      * @since 5.0.0
      */
     public static String getExtension(Path self) {
-        final Path fileName = self.getFileName();
-        final String name = fileName == null ? "" : fileName.toString();
+        final String name = getName(self);
         final int index = name.lastIndexOf('.');
         if (index == -1) return "";
         return name.substring(index + 1);
@@ -124,8 +134,7 @@ public class NioExtensions extends DefaultGroovyMethodsSupport {
      * @since 5.0.0
      */
     public static String getBaseName(Path self) {
-        final Path fileName = self.getFileName();
-        final String name = fileName == null ? "" : fileName.toString();
+        final String name = getName(self);
         final int index = name.lastIndexOf('.');
         if (index == -1) return name;
         return name.substring(0, index);
diff --git a/subprojects/groovy-nio/src/test/groovy/org/apache/groovy/nio/extensions/NioExtensionsTest.groovy b/subprojects/groovy-nio/src/test/groovy/org/apache/groovy/nio/extensions/NioExtensionsTest.groovy
index fd0df0ec48..607492c27f 100644
--- a/subprojects/groovy-nio/src/test/groovy/org/apache/groovy/nio/extensions/NioExtensionsTest.groovy
+++ b/subprojects/groovy-nio/src/test/groovy/org/apache/groovy/nio/extensions/NioExtensionsTest.groovy
@@ -180,6 +180,16 @@ class NioExtensionsTest extends Specification {
         path.size() == str.size()
     }
 
+    def testPathName() {
+        when:
+        def file = new File(tempFile, 'gradle.properties')
+        def dir = new File(tempFile, 'properties')
+
+        then:
+        file.name == file.toPath().name
+        dir.name == dir.toPath().name
+    }
+
     def testPathExtension() {
         when:
         def filePath = new File(tempFile, 'gradle.properties').toPath()