diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index 17d461c295d..0c3084f855a 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -229,8 +229,9 @@ public static boolean anyNotNull(final Object... values) {
/**
*
- * 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.
+ * Calls the given {@code consumer's} {@link Consumer#accept(Object)} method with the first {@code non-null} value
+ * from {@code objects}. If all the values are null, the consumer is not invoked. This is equivalent to the call
+ * {@code ObjectUtils.acceptIfNonNull(ObjectUtils.firstNonNull(objects), consumer)}
*
*
*
@@ -238,27 +239,27 @@ public static boolean anyNotNull(final Object... values) {
*
*
*
- * ObjectUtils.applyFirstNonNull(bean::setValue, null) - setValue not invoked
- * ObjectUtils.applyFirstNonNull(bean::setValue, null, "abc", "def") - setValue invoked with "abc"
- * ObjectUtils.applyFirstNonNull(v -> bean.setValue(v), "abc") - setValue invoked with "abc"
+ * ObjectUtils.acceptFirstNonNull(bean::setValue, null) - setValue not invoked
+ * ObjectUtils.acceptFirstNonNull(bean::setValue, null, "abc", "def") - setValue invoked with "abc"
+ * ObjectUtils.acceptFirstNonNull(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)
+ * @see #acceptIfNonNull(Object, Consumer)
* @since 3.12
*/
@SafeVarargs
- public static void applyFirstNonNull(final Consumer consumer, final T... objects) {
- applyIfNonNull(consumer, firstNonNull(objects));
+ public static void acceptFirstNonNull(final Consumer consumer, final T... objects) {
+ acceptIfNonNull(firstNonNull(objects), consumer);
}
/**
*
- * 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.
+ * Calls the given {@code consumer's} {@link Consumer#accept(Object)} method with the {@code object} if it is
+ * {@code non-null}.
*
*
*
@@ -266,18 +267,18 @@ public static void applyFirstNonNull(final Consumer consumer, final T...
*
*
*
- * 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"
+ * ObjectUtils.acceptIfNonNull(null, bean::setValue) - setValue not invoked
+ * ObjectUtils.acceptIfNonNull("abc", bean::setValue) - setValue invoked with "abc"
+ * ObjectUtils.acceptIfNonNull("abc", v -> bean.setValue(v)) - 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...)
+ * @see #acceptFirstNonNull(Consumer, Object...)
* @since 3.12
*/
- public static void applyIfNonNull(final Consumer consumer, final T object) {
+ public static void acceptIfNonNull(final T object, final Consumer consumer) {
if (object != null) {
consumer.accept(object);
}
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index 7a997917dfc..11eb563dcf7 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -40,6 +40,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;
@@ -246,30 +247,30 @@ public void testEquals() {
}
@Test
- public void testApplyIfNonNull() {
+ public void testAcceptFirstNonNull() {
final ApplyIfNonNullBean bean = new ApplyIfNonNullBean();
bean.setValue(FOO);
- ObjectUtils.applyIfNonNull(bean::setValue, null);
+ ObjectUtils.acceptFirstNonNull(bean::setValue, null, null, null);
assertEquals(FOO, bean.getValue());
- ObjectUtils.applyIfNonNull(bean::setValue, BAR);
+ ObjectUtils.acceptFirstNonNull(bean::setValue, null, null, BAR, FOO, null);
assertEquals(BAR, bean.getValue());
-
- ObjectUtils.applyIfNonNull(v -> bean.setValue(v), FOO);
- assertEquals(FOO, bean.getValue());
}
@Test
- public void testApplyFirstNonNull() {
+ public void testAcceptIfNonNull() {
final ApplyIfNonNullBean bean = new ApplyIfNonNullBean();
bean.setValue(FOO);
- ObjectUtils.applyFirstNonNull(bean::setValue, null, null, null);
+ ObjectUtils.acceptIfNonNull(null, bean::setValue);
assertEquals(FOO, bean.getValue());
- ObjectUtils.applyFirstNonNull(bean::setValue, null, null, BAR, FOO, null);
+ ObjectUtils.acceptIfNonNull(BAR, bean::setValue);
assertEquals(BAR, bean.getValue());
+
+ ObjectUtils.acceptIfNonNull(FOO, v -> bean.setValue(v));
+ assertEquals(FOO, bean.getValue());
}
@Test
@@ -797,7 +798,7 @@ public String getValue() {
return value;
}
public void setValue(String value) {
- this.value = value;
+ this.value = Objects.requireNonNull(value, "value");
}
}
}