You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2020/12/29 15:56:44 UTC

[GitHub] [commons-lang] garydgregory commented on a change in pull request #684: [LANG-1634] Add ObjectUtils #applyIfNonNull and #applyFirstNonNull

garydgregory commented on a change in pull request #684:
URL: https://github.com/apache/commons-lang/pull/684#discussion_r549756117



##########
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:
       You can add a call to `Objects.requireNonNull()` here instead of the blind assignment:
   ```
   this.value = Objects.requireNonNull(value, "value");
   ```
   

##########
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:
       See "accept" comment above and other comment in the Conversation stream of this PR.
   

##########
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:
       IMO, you're not "invoking", you're "accepting", so the Javadoc should say "Accepts...". Actually, the method is misnamed since the underlying method is `Consumer#accept()`, this method should be use "accept", not "apply".
   




----------------------------------------------------------------
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:
users@infra.apache.org