RFR: 8298318: (fs) APIs for handling filename extensions

Pavel Rappo prappo at openjdk.org
Wed Oct 18 16:24:00 UTC 2023


On Wed, 18 Oct 2023 14:21:32 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

> There are a few builder style APIs for immutable objects that have `withXXX` methods that return an instance with the change requested. A `withExtension(String ext)` could handle the common cases for removing, replacing, and adding an extension.
> 
> ```
> Path p = Path.of("photo.png");
> Path base = p.withExtension("") -> "photo" // replaces any extension with the empty extension (and no dot)
> p.withExtension("jpg") -> "photo.jpg"      // replaces the extension
> base.withExtension("pdf") -> "photo.pdf"   // adds the extension
> ```
> 
> Removing an extension with `""` may be a bit unusual, perhaps an additional method would aid discoverablity`basepath()` or `dropExtension`, or `trimExtension`.

Right. Depending on how frequent an operation of removing extension is, one could also imagine a name such as `withoutExtension()`. We have methods like that in the JDK.

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

PR Comment: https://git.openjdk.org/jdk/pull/16226#issuecomment-1768893893


More information about the nio-dev mailing list