diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index c6c6a8e8e11..6411297dbba 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
+import java.util.function.Consumer;
import java.util.function.Supplier;
import org.apache.commons.lang3.exception.CloneFailedException;
@@ -226,6 +227,62 @@ public static boolean anyNotNull(final Object... values) {
return firstNonNull(values) != null;
}
+ /**
+ *
+ * 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.
+ *
+ *
+ *
+ * The caller is responsible for thread-safety and exception handling of consumer.
+ *
+ *
+ *
+ * ObjectUtils.applyIfNonNull(bean::setValue, null) - setValue not invoked
+ * ObjectUtils.applyIfNonNull(bean::setValue, null, "abc", "def") - setValue invoked with "abc"
+ * ObjectUtils.applyIfNonNull(v -> bean.setValue(v), "abc") - setValue invoked with "abc"
+ *
+ *
+ * @param 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 void applyFirstNonNull(final Consumer consumer, final T... objects) {
+ applyIfNonNull(consumer, firstNonNull(objects));
+ }
+
+ /**
+ *
+ * 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.
+ *
+ *
+ *
+ * The caller is responsible for thread-safety and exception handling of consumer.
+ *
+ *
+ *
+ * ObjectUtils.applyIfNonNull(bean::setValue, null) - setValue not invoked
+ * ObjectUtils.applyIfNonNull(bean::setValue, "abc") - setValue invoked with "abc"
+ * ObjectUtils.applyIfNonNull(v -> bean.setValue(v), "abc") - setValue invoked with "abc"
+ *
+ *
+ * @param 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 void applyIfNonNull(final Consumer consumer, final T object) {
+ if (object != null) {
+ consumer.accept(object);
+ }
+ }
+
// cloning
//-----------------------------------------------------------------------
/**
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index 3efafe70ab6..7a997917dfc 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -245,6 +245,33 @@ public void testEquals() {
assertTrue(ObjectUtils.equals(FOO, FOO), "ObjectUtils.equals(\"foo\", \"foo\") returned false");
}
+ @Test
+ public void testApplyIfNonNull() {
+ final ApplyIfNonNullBean bean = new ApplyIfNonNullBean();
+ bean.setValue(FOO);
+
+ ObjectUtils.applyIfNonNull(bean::setValue, null);
+ assertEquals(FOO, bean.getValue());
+
+ ObjectUtils.applyIfNonNull(bean::setValue, BAR);
+ assertEquals(BAR, bean.getValue());
+
+ ObjectUtils.applyIfNonNull(v -> bean.setValue(v), FOO);
+ assertEquals(FOO, bean.getValue());
+ }
+
+ @Test
+ public void testApplyFirstNonNull() {
+ final ApplyIfNonNullBean bean = new ApplyIfNonNullBean();
+ bean.setValue(FOO);
+
+ ObjectUtils.applyFirstNonNull(bean::setValue, null, null, null);
+ assertEquals(FOO, bean.getValue());
+
+ ObjectUtils.applyFirstNonNull(bean::setValue, null, null, BAR, FOO, null);
+ assertEquals(BAR, bean.getValue());
+ }
+
@Test
public void testNotEqual() {
assertFalse(ObjectUtils.notEqual(null, null), "ObjectUtils.notEqual(null, null) returned false");
@@ -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;
+ }
+ }
}