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

Roger Riggs rriggs at openjdk.java.net
Tue May 3 14:52:23 UTC 2022


On Tue, 3 May 2022 00:37:34 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Resurrection of the proposal to add a method to obtain the filename extension originated in PR [2319](https://github.com/openjdk/jdk/pull/2319).
>
> Brian Burkhalter has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains eight additional commits since the last revision:
> 
>  - 8057113: trailing dot yields empty string extension; add test
>  - Merge
>  - 8057113: String getExtension(String defaultExtension) to Optional<String> getExtension() and other changes
>  - 8057113: Clean up trailing whitespace
>  - 8057113: Implement reviewer suggestions for replaceExtension()
>  - 8057113: Add hasExtension() and replaceExtension()
>  - 8057113: Fix variable name in @return verbiage
>  - 8057113: (fs) Path should have a method to obtain the filename extension

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

> 274:      *     else
> 275:      *         Optional.empty();
> 276:      * }</pre>

Maybe clearer as:

    if (lastDot <= 0)
        return Option.empty();
    return lastDot == name.length() -1 ?
        optional.of("") : 
        optional.of(name.substring(lastDot + 1));


I think the update for a file name with a final dot having an empty extension works well.

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

> 278:      * @return  an {@code Optional} which either contains the file name of
> 279:      *          this path, or is {@link Optional#empty empty} if the extension
> 280:     *           is indeterminate

Can it say "not found";  indeterminate sounds non-deterministic but the algorithm is.

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

> 326:         String thisExtension = getExtension().orElse(null);
> 327:         if (thisExtension == null)
> 328:             return Optional.empty();

Maybe keep around the Optional holding the extension to return if it is empty or matches; saving creating a new one.

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

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


More information about the nio-dev mailing list