RFR: 8057113: (fs) Path should have a method to obtain the filename extension [v3]
Rémi Forax
github.com+828220+forax at openjdk.java.net
Wed Feb 3 16:45:41 UTC 2021
On Wed, 3 Feb 2021 15:29:37 GMT, Alan Bateman <alanb at openjdk.org> wrote:
>> **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.
Using an Optional forces a user to read the javadoc (or worst guess), so i'm not sure it's a good idea to use it here because most people will just want the extension not a long speech about the filename being potentially null.
For me using "" when the extension doesn't exist whatever the reason is more pragmatic.
-------------
PR: https://git.openjdk.java.net/jdk/pull/2319
More information about the nio-dev
mailing list