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

Add isEmpty default method to CharSequence

    XMLWordPrintable

Details

    • CSR
    • Status: Closed
    • P4
    • Resolution: Approved
    • 15
    • core-libs
    • None
    • source
    • minimal
    • 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
    • Java API
    • 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

              redestad Claes Redestad
              redestad Claes Redestad
              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: