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;
}
- csr of
-
JDK-8215401 Add isEmpty default method to CharSequence
-
- Resolved
-