RFR: 8298318: (fs) APIs for handling filename extensions [v4]

Roger Riggs rriggs at openjdk.org
Thu Mar 7 17:02:58 UTC 2024


On Wed, 6 Mar 2024 23:41:05 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Add to `java.nio.file.Path` a method `getExtension` to retrieve the `Path`'s extension, and companion methods `removeExtension` and `addExtension`.
>
> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
> 
>   8298318: Remove vestigial trim() call and commented out methods

src/java.base/share/classes/java/nio/file/Path.java line 53:

> 51:  * {@link #getParent getParent}, {@link #getRoot getRoot}, and {@link #subpath
> 52:  * subpath} methods to access the path components or a subsequence of its name
> 53:  * elements, and {@link #getExtension() getExtension} to obtain its extension.

Could also mention the other new methods.

src/java.base/share/classes/java/nio/file/Path.java line 308:

> 306: 
> 307:     /**
> 308:      * Returns a copy of this {@code Path} with the file name extension removed.

Perhaps just `Returns a Path for this Path with the file  name extension removed`.
That preserves the ability to return `this` if there is no extension.

src/java.base/share/classes/java/nio/file/Path.java line 311:

> 309:      * If this path has no extension, then the path is returned unchanged.
> 310:      *
> 311:      * <p> A compound extension may be replaced by invoking this method and

This jumps into compound extension without further describing the single extension case.
Perhaps include a simple example. And possibly describe the compound extension case in getExtension().

src/java.base/share/classes/java/nio/file/Path.java line 326:

> 324:      *     assert equals(Path.of(withoutExtension().toString()
> 325:      *         + (getExtension().isEmpty() ? "" : ("." + getExtension())));
> 326:      * }

This seems more like implSpec text and could be combined with the implSpec.

src/java.base/share/classes/java/nio/file/Path.java line 362:

> 360:      * non-{@linkplain String#isEmpty empty}, and not
> 361:      * {@linkplain String#isBlank blank}, then a {@code '.'} and then
> 362:      * {@code extension} are appended to the path returned by

Drop "copy" as above.
I'd remove the behavior specific to blanks. They are not a good idea but whether they are restricted is the responsibility of the file system, not this APIs.

test/jdk/java/nio/file/Path/Extensions.java line 81:

> 79:             {"compress.gzip",  "gzip"},
> 80:             {"waitwhat.&$!#%", "&$!#%"},
> 81:             {"6.283185307",    "283185307"}

The behavior of these test cases is good, these are the expected results of getExtension().

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

PR Review Comment: https://git.openjdk.org/jdk/pull/16226#discussion_r1516519538
PR Review Comment: https://git.openjdk.org/jdk/pull/16226#discussion_r1516498847
PR Review Comment: https://git.openjdk.org/jdk/pull/16226#discussion_r1516508594
PR Review Comment: https://git.openjdk.org/jdk/pull/16226#discussion_r1516511591
PR Review Comment: https://git.openjdk.org/jdk/pull/16226#discussion_r1516517693
PR Review Comment: https://git.openjdk.org/jdk/pull/16226#discussion_r1516518929


More information about the nio-dev mailing list