[ 
https://issues.apache.org/jira/browse/LANG-1634?focusedWorklogId=529256&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-529256
 ]

ASF GitHub Bot logged work on LANG-1634:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 29/Dec/20 20:32
            Start Date: 29/Dec/20 20:32
    Worklog Time Spent: 10m 
      Work Description: bindul commented on a change in pull request #684:
URL: https://github.com/apache/commons-lang/pull/684#discussion_r549838166



##########
File path: src/main/java/org/apache/commons/lang3/ObjectUtils.java
##########
@@ -226,6 +227,62 @@ public static boolean anyNotNull(final Object... values) {
         return firstNonNull(values) != null;
     }
 
+    /**
+     * <p>
+     * Invokes the given {@code consumer's} {@link Consumer#accept(Object)} 
with the first {@code non-null} value from

Review comment:
       I have changed the method name to 'accept...' and updated the Javadoc 
comment accordingly.

##########
File path: src/main/java/org/apache/commons/lang3/ObjectUtils.java
##########
@@ -226,6 +227,62 @@ public static boolean anyNotNull(final Object... values) {
         return firstNonNull(values) != null;
     }
 
+    /**
+     * <p>
+     * Invokes the given {@code consumer's} {@link Consumer#accept(Object)} 
with the first {@code non-null} value from
+     * {@code objects}. If all the values are null, the consumer is not 
invoked.
+     * </p>
+     *
+     * <p>
+     * The caller is responsible for thread-safety and exception handling of 
consumer.
+     * </p>
+     *
+     * <pre>
+     * ObjectUtils.applyFirstNonNull(bean::setValue, null)                 - 
setValue not invoked
+     * ObjectUtils.applyFirstNonNull(bean::setValue, null, "abc", "def")   - 
setValue invoked with "abc"
+     * ObjectUtils.applyFirstNonNull(v -&gt; bean.setValue(v), "abc")      - 
setValue invoked with "abc"
+     * </pre>
+     *
+     * @param <T> the type of the object
+     * @param objects  the values to test, may be {@code null} or empty
+     * @param consumer the consumer operation to invoke with the first 
non-null {@code objects}.
+     * @see #firstNonNull(Object...)
+     * @see #applyIfNonNull(Consumer, Object)
+     * @since 3.12
+     */
+    @SafeVarargs
+    public static <T> void applyFirstNonNull(final Consumer<T> consumer, final 
T... objects) {
+        applyIfNonNull(consumer, firstNonNull(objects));
+    }
+
+    /**
+     * <p>
+     * Invokes the given {@code consumer's} {@link Consumer#accept(Object)} 
with the {@code object} if it is
+     * {@code non-null}, otherwise the consumer is not invoked.
+     * </p>
+     *
+     * <p>
+     * The caller is responsible for thread-safety and exception handling of 
consumer.
+     * </p>
+     *
+     * <pre>
+     * ObjectUtils.applyIfNonNull(bean::setValue, null)             - setValue 
not invoked
+     * ObjectUtils.applyIfNonNull(bean::setValue, "abc")            - setValue 
invoked with "abc"
+     * ObjectUtils.applyIfNonNull(v -&gt; bean.setValue(v), "abc")  - setValue 
invoked with "abc"
+     * </pre>
+     *
+     * @param <T> the type of the object
+     * @param object the {@code Object} to test, may be {@code null}
+     * @param consumer the consumer operation to invoke with {@code object} if 
it is {@code non-null}
+     * @see #applyFirstNonNull(Consumer, Object...)
+     * @since 3.12
+     */
+    public static <T> void applyIfNonNull(final Consumer<T> consumer, final T 
object) {

Review comment:
       I have changed the method name to 'accept...', switched the parameter 
order and updated the Javadoc comment accordingly.

##########
File path: src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
##########
@@ -764,4 +791,13 @@ public int compare(final CharSequence o1, final 
CharSequence o2) {
 
     }
 
+    static final class ApplyIfNonNullBean {
+        private String value;
+        public String getValue() {
+            return value;
+        }
+        public void setValue(String value) {
+            this.value = value;

Review comment:
       This inner class was meant as a simple Java bean with a setter and 
getter, hence did not have any checks in it. I have changed it now.




----------------------------------------------------------------
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: 529256)
    Time Spent: 1h 40m  (was: 1.5h)

> 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 40m
>  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)

Reply via email to