Skip to content

Add isNotEmpty Utility Methods to Improve Readability and Safety #35247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

iddeepak
Copy link

Summary
This PR introduces new utility methods to improve readability and reduce the risk of subtle bugs related to negated emptiness checks:

Adds CollectionUtils.isNotEmpty(Collection) Adds CollectionUtils.isNotEmpty(Map)

Motivation
As noted by @mdeinum in #35243 , negated expressions like !collection.isEmpty() can be error-prone:

public static boolean isNotEmpty(@Nullable Map<?, ? extends @Nullable Object> map) {
return (map != null && !map.isEmpty());
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdeinum
Added isNotEmpty methods to CollectionUtils as suggested to improve readability and avoid !isEmpty() usage

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 28, 2025
@@ -244,7 +244,7 @@ else if (ch == '"') {
nextIndex++;
}
String parameter = mimeType.substring(index + 1, nextIndex).trim();
if (parameter.length() > 0) {
if (StringUtils.hasLength(parameter)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use StringUtils already has a way to check
@mdeinum

@iddeepak
Copy link
Author

I see StringUtils#isEmpty is replaced with !StringUtils.hasLength everywhere, Can't we just use if string.isEmpty() ?
@mdeinum

@bclozel
Copy link
Member

bclozel commented Jul 28, 2025

Thanks for the proposal, but this has been declined multiple times, see #34519 and linked issues.

@bclozel bclozel closed this Jul 28, 2025
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants