[
https://issues.apache.org/jira/browse/LANG-1634?focusedWorklogId=529257&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-529257
]
ASF GitHub Bot logged work on LANG-1634:
----------------------------------------
Author: ASF GitHub Bot
Created on: 29/Dec/20 20:42
Start Date: 29/Dec/20 20:42
Worklog Time Spent: 10m
Work Description: bindul commented on pull request #684:
URL: https://github.com/apache/commons-lang/pull/684#issuecomment-752234673
@garydgregory
I have renamed the methods to accept from apply and switched the parameter
order where possible.
`acceptFirstNonNull` is the analogous method to existing
`ObjectUtils.firstNonNull` or `StringUtils.firstNonBlank`. If I remember
correctly, I have had one use for this method in my real life code, where a
configuration parameter value could be supplied at multiple levels (specific
instance, application default or global default; the first two could be null)
and I have used a similar method to handle this. If you don't want to include
it, I am happy to remove this.
In a real life scenario, the use case for the `acceptIfNonNull` method is
usually when setting configuration values. The driver (say for something like
redis, solr, etc.) or library (like a HTTP client library) accepts a
configuration bean with some default values set. Your application accepts
configuration values, and you only want to set those in the configuration bean
if they are not null (or you lose the default). Anyways, I have updated 3
existing commons-lang classes to show how it can reduce 3 lines of code to 1. I
did not go and update every possible occurrence, as that would make this PR
large and lose focus on the primary change in the PR.
Please review and let me know if I should make further modifications.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 529257)
Time Spent: 1h 50m (was: 1h 40m)
> ObjectUtils - apply Consumer with non-null value
> ------------------------------------------------
>
> Key: LANG-1634
> URL: https://issues.apache.org/jira/browse/LANG-1634
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.*
> Reporter: Bindul Bhowmik
> Priority: Minor
> Labels: pull-request-available
> Time Spent: 1h 50m
> Remaining Estimate: 0h
>
> There are multiple places in code where we have to check if a value is
> {{null}} before using it in a setter or other method, like:
> {code:java}
> if (valueX != null) {
> bean.setValue(valueX);
> someObject.compute(valueX, "bar");
> }
> {code}
> This enhancement request is to add a couple of methods in {{ObjectUtils}} to
> wrap this logic, like the following:
> {code:java}
> public static <T> void applyIfNonNull(final Consumer<T> consumer, final T
> object)
> public static <T> void applyFirstNonNull(final Consumer<T> consumer, final
> T... objects)
> {code}
> With this the two statements above could be used as:
> {code:java}
> ObjectUtils.applyIfNonNull(bean::setValue, valueX);
> ObjectUtils.appyIfNonNull(v -> someObject.compute(v, "bar"), valueX);
> {code}
> The benefit of this should increase with more such null checks we need in the
> code that can be replaced by single statements.
> Pull request forthcoming.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)