Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8215402

Add isEmpty default method to CharSequence

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: 15
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      source
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      Hide
      Existing CharSequence implementations that define an isEmpty() method are highly likely to be source, binary and behaviorally compatible.

      A warning could manifest when compiling with -Xlint:overrides if the overriding implementation does not add an @Override
      Show
      Existing CharSequence implementations that define an isEmpty() method are highly likely to be source, binary and behaviorally compatible. A warning could manifest when compiling with -Xlint:overrides if the overriding implementation does not add an @Override
    • Interface Kind:
      Java API
    • Scope:
      SE

      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;
      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                redestad Claes Redestad
                Reporter:
                redestad Claes Redestad
                Reviewed By:
                Chris Hegarty, Jim Laskey, Pavel Rappo, Roger Riggs, Stuart Marks
              • Votes:
                1 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: