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

Alan Bateman alanb at openjdk.java.net
Wed Feb 3 15:32:43 UTC 2021


On Tue, 2 Feb 2021 23:42:48 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Marked as reviewed by rriggs (Reviewer).
>
> **Alternate Specifications**
> 
> Alternate specifications are possible. Ignoring more precise naming, e.g., `getExtensionOrElseEmptyString()` for the original version above, some possible options are:
> 
> 1. `String getExtension(`) returns the extension or the empty string if no extension found
> 2. `String getExtension()` returns the extension or `null` if no extension found
> 3. `String getExtension(String default)` returns the extension or `default` if no extension found (may be `null`)
> 4. `Optional<String> getExtension()` returns the extension, if any, contained in an `Optional`
> 
> Option 1 is that originally proposed. Option 2 is identical but returns null instead of the empty string if there is no extension. This is consistent with other methods in `Path` such as `getRoot()`, `getParent()`, and `getFileName()`. Option 3 allows the possibility to supply the value to be returned if no extension is found. Option 4 wraps the extension string or `null` in an `Optional`.
> 
> If one wanted to be able to retain the internal structure of the extension as a `Path`, then one could instead define two methods:
> 
> `Path Path.getExtension()`
> `String Path.getExtensionString()`
> 
> as part of the point is the user not being obliged to call `toString()`. Here `getExtension()` would be implemented in the platform-specific `Path` classes, and `getExtensionString()` would be a default method in the `Path` interface.
> 
> In all cases, when the `Path` represents a root component, then the extension is indeterminate (empty or `null`, depending).

Thanks for listing down the options. We'll need to spend more time working through potential use cases and narrow down the possible APIs. Having a default value or Optional.orElse has some appeal to cover the cases where the Path does not have a file name or extension.

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

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


More information about the nio-dev mailing list