aidanow-uni opened a new pull request, #1469:
URL: https://github.com/apache/commons-lang/pull/1469
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
Thanks for your contribution to [Apache
Commons](https://commons.apache.org/)! Your help is appreciated!
Before you push a pull request, review this list:
- [X] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
- [X] Read the [ASF Generative Tooling
Guidance](https://www.apache.org/legal/generative-tooling.html) if you use
Artificial Intelligence (AI).
- [X] Run a successful build using the default
[Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command
line by itself.
- [X] Write unit tests that match behavioral changes, where the tests fail
if the changes to the runtime are not applied. This may not always be possible,
but it is a best-practice.
- [X] Write a pull request description that is detailed enough to understand
what the pull request does, how, and why.
- [X] Each commit in the pull request should have a meaningful subject line
and body. Note that a maintainer may squash commits during the merge process.
**Description of Issue**
The PR addresses the JIRA issue titled '[_Short-circuit operation of
firstNonBlank and
firstNonEmpty_](https://issues.apache.org/jira/browse/LANG-1656?jql=project%20%3D%20LANG%20ORDER%20BY%20created%20DESC)',
a minor feature request originally proposed by Zhengkai Wang.
The existing methods `firstNonBlank` and `firstNonEmpty` in StringUtils.java
evaluate all input strings immediately. This can lead to extra computational
overhead when some arguments are expensive to compute, such as those involving
remote API calls or database queries.
The feature request aims to introduce supplier-based variants of these
methods, which support lazy evaluation and short-circuit behaviour.
**Implementation**
To resolve this, two new methods were added to StringUtils:
1. firstNonBlankSupplier(Supplier<String>... suppliers)
- Iterates through suppliers in order
- Returns immediately once a non-blank supplier is found
2. firstNonEmptySupplier(Supplier<String>... suppliers)
- Similar to above, but checks for non-empty rather than non-blank
strings.
Introducing new methods instead of modifying existing ones ensures backward
compatibility with existing firstNonBlank and firstNonEmpty implementations. It
avoids method overloading ambiguity and also creates a clear separation between
eager and lazy variants.
**Validation and Testing**
1. Basic functionality tests
- Implemented in StringUtilsEmptyBlankTest.
- Methods `testFirstNonBlankSupplier()` and
`testFirstNonEmptySupplier()` validate:
- Correct return of the first non-blank/non-empty string.
- Handling of null inputs, empty arrays, and suppliers that return
blank or empty values.
2. Lazy evaluation tests
- `testFirstNonBlankSupplierLazyEvaluation()` and
`testFirstNonEmptySupplierLazyEvaluation()` verify short-circuit behavior using
AtomicBoolean flags.
- Confirms that once a valid result is found, subsequent suppliers are
not invoked, ensuring performance efficiency.
3. Safety guarantees
- All methods include null checks to prevent NullPointerException.
- Edge cases covered in original tests are preserved.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]