Skip to content
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

Check isEmpty() instead of size == 0. #7144

Merged
merged 1 commit into from
Apr 9, 2024
Merged

Conversation

copybara-service[bot]
Copy link
Contributor

Check isEmpty() instead of size == 0.

That way is simpler.

It's also somewhat better for users who pass a filtered collection, but:

  • We may undo this change in the future (as we are considering approaches that would actually use the result of size for more than an emptiness check).
  • A nonempty filtered collection will still apply the filter at least once during the isEmpty call, so that can still cause trouble if the filter has side effects or "call the filter until it returns true" is slow enough to matter.
  • Other methods in Guava likely still trigger similar behavior when given a filtered collection, as do methods outside of Guava.

Fixes #7143 (for now)

RELNOTES=n/a

That way is simpler.

It's also somewhat better for users who pass a filtered collection, **but:**
- We may undo this change in the future (as we are considering approaches that would actually use the result of `size` for more than an emptiness check).
- A nonempty filtered collection will still apply the filter at least once during the `isEmpty` call, so that can still cause trouble if the filter has side effects or "call the filter until it returns `true`" is slow enough to matter.
- Other methods in Guava likely still trigger similar behavior when given a filtered collection, as do methods outside of Guava.

Fixes #7143 (for now)

RELNOTES=n/a
PiperOrigin-RevId: 623232817
@copybara-service copybara-service bot merged commit 911661a into master Apr 9, 2024
2 checks passed
@copybara-service copybara-service bot deleted the test_623223284 branch April 9, 2024 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Copying a filtered collection with ImmutableSet.copyOf() should require visiting each element only once
1 participant