[ 
https://issues.apache.org/jira/browse/LANG-1633?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18136030#comment-18136030
 ] 

Nyasa Roy commented on LANG-1633:
---------------------------------

Hi team,

We've opened a pull request for this issue: 
https://github.com/apache/commons-lang/pull/1852

It adds applyIfNonNull and applyFirstNonNull to ObjectUtils, allowing Consumers 
to be applied only to non-null values. It also adds tests covering null values, 
first non-null selection, Consumer invocation, and null Consumers.

> ObjectUtils - apply Consumer with non-null value
> ------------------------------------------------
>
>                 Key: LANG-1633
>                 URL: https://issues.apache.org/jira/browse/LANG-1633
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.*
>            Reporter: Bindul Bhowmik
>            Priority: Minor
>
> 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
(v10.3.26#10030026)

Reply via email to