RFR: 8057113: (fs) Path should have a method to obtain the filename extension [v7]

Brian Burkhalter bpb at openjdk.java.net
Tue May 3 17:08:25 UTC 2022


On Tue, 3 May 2022 16:41:30 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

>>> Maybe keep around the Optional holding the extension to return if it is empty or matches; saving creating a new one.
>> 
>> I don't understand this comment. I think that this could be done in the implementation classes but I do not see how to do it here.
>
> Something like:
> 
>     default Optional<String> hasExtension(String ext, String... extensions) {
>         requireNonNull(ext);
>         Optional<String> extension = getExtension();
>         String thisExtension = null;
>         if (extension.isEmpty() || (thisExtension = extension.get()).equals(ext))
>             return extension;
>         ...

I see. That is good. This is what I had thought of:

--- a/src/java.base/unix/classes/sun/nio/fs/UnixPath.java
+++ b/src/java.base/unix/classes/sun/nio/fs/UnixPath.java
+    private volatile Optional<String> theExtension = null;
+
+    @Override
+    public Optional<String> getExtension() {
+        if (theExtension == null) {
+            theExtension = Path.super.getExtension();
+        }
+        return theExtension;
+    }

-------------

PR: https://git.openjdk.java.net/jdk/pull/8066


More information about the nio-dev mailing list