JDK-8215402 : Add isEmpty default method to CharSequence
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 15
  • Submitted: 2018-12-14
  • Updated: 2020-07-13
  • Resolved: 2020-05-20
Related Reports
CSR :  
Description
Summary
-------

Adding an isEmpty default method to CharSequence would harmonize String with other CharSequence implementations (StringBuilder etc). 

Problem
-------

isEmpty is shorter and more concise than length() == 0. Checking for and filtering out empty Strings and other CharSequences is a common occurrence in code, and having an explicit method allows its use as a method reference. 

Solution
--------

Add a default method isEmpty() to CharSequence.

Specification
-------------

Add the following to java.lang.CharSequence:

    /**
     * Returns {@code true} if this character sequence is empty.
     *
     * @implSpec
     * The default implementation returns the result of calling {@code length() == 0}.
     *
     * @return {@code true} if {@link #length()} is {@code 0}, otherwise
     * {@code false}
     *
     * @since 15
     */
    default boolean isEmpty() {
        return this.length() == 0;
    }

Also add an override with clarifications to java.nio.CharBuffer:

    /**
     * Returns {@code true} if this character buffer is empty.
     *
     * @return {@code true} if there are {@code 0} remaining characters,
     *         otherwise {@code false}
     *
     * @since 15
     */
    public final boolean isEmpty() {
        return remaining() == 0;
    }
Comments
Re-approving update request.
20-05-2020

Translation error. Re-finalized.
20-05-2020

Wait, why did @implSpec change to @implNote? It should be @implSpec.
20-05-2020

True. The final modifier just keeps it consistent with other CharSequence overrides in CharBuffer.
20-05-2020

CharBuffer cannot be extended outside of the java.nio package so adding a final method to this class doesn't create a compatibility concern.
20-05-2020

Re-submitted with a few minor style changes that came up during review, as well as adding an override to CharBuffer.java
20-05-2020

Thanks to [~redestad] and [~asotona] for the additional analysis; moving to Approved.
18-05-2020

[~asotona] ran an extensive search on a large corpus of artifacts and projects. Analysis of the results shows that while implementing isEmpty on classes inheriting from CharSequence is somewhat popular, all instances we could find source for is behaviorally consistent with what's being suggested here.
18-05-2020

Rather than voting to Approve this request, I've instead voted to move it to the intermediate Provisional state. In my estimation, this is a reasonable addition, but due diligence requires more investigation be done and recorded in the CSR about possible effects on some of the many, many extent CharSequence implementations with respect to this change. A release note may be warranted.
18-12-2018

Should there be a @implSpec to indicate that the cost is gated by the implementation of length? For example, what if you have a fragmented sequence and have to sum the fragments lengths. Or if the sequence has to be scanned for length.
14-12-2018