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

Brian Burkhalter bpb at openjdk.java.net
Thu Apr 7 20:33:39 UTC 2022


On Thu, 7 Apr 2022 16:03:40 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

>> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   8057113: Fix variable name in @return verbiage
>
> I would include:
> 
> 2. `default String getExtension(String default)` returns the extension of the string representation of the path
> 
> 4. `default boolean hasExtension(String extension, boolean ignoreCase)` returns whether the string representation of the path has the specified extension, optionally ignoring case
> 
>    An alternative to case insensitive is  `Optional<String> hasExtension(String ext, String... extensions);`
>    It allows at least one extension and a list of possible other extensions and returns the first one found.
>    
> 5ish. `Path replaceExtension(String extension)` replaces the path's extension with the specified extension
>    No particular bias against `removeExtension` but if total method count is an issue
>    `replaceExtension` can be used to remove an extension with an empty string argument
> 
> These can avoid needing to convert Paths fully to strings and back; retaining the implementation
> specific representation and giving the developer a familiar string based API.

@RogerRiggs Thanks for the comments.

I tend to agree with you about using `String` and not `Path` to represent an extension. I do not see what value it would add here to use `Path`. I think the subset of methods you selected is a good one. I also had thought about having `replaceExtension(“”)` be equivalent to `removeExtension()` but I failed to document it.

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

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


More information about the nio-dev mailing list