Skip to content

Commit 78feae4

Browse files
committed
[LANG-1634] Add ObjectUtils #applyIfNonNull and #applyFirstNonNull
Utility methods that take a java.util.function.Consumer and possibly null value(s). The consumer is invoked if the value is not null or with the first non-null value, respectively.
1 parent cd013c9 commit 78feae4

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/main/java/org/apache/commons/lang3/ObjectUtils.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.HashMap;
2828
import java.util.Map;
2929
import java.util.TreeSet;
30+
import java.util.function.Consumer;
3031
import java.util.function.Supplier;
3132

3233
import org.apache.commons.lang3.exception.CloneFailedException;
@@ -226,6 +227,62 @@ public static boolean anyNotNull(final Object... values) {
226227
return firstNonNull(values) != null;
227228
}
228229

230+
/**
231+
* <p>
232+
* Invokes the given {@code consumer's} {@link Consumer#accept(Object)} with the first {@code non-null} value from
233+
* {@code objects}. If all the values are null, the consumer is not invoked.
234+
* </p>
235+
*
236+
* <p>
237+
* The caller is responsible for thread-safety and exception handling of consumer.
238+
* </p>
239+
*
240+
* <pre>
241+
* ObjectUtils.applyIfNonNull(bean::setValue, null) - setValue not invoked
242+
* ObjectUtils.applyIfNonNull(bean::setValue, null, "abc", "def") - setValue invoked with "abc"
243+
* ObjectUtils.applyIfNonNull(v -&gt; bean.setValue(v), "abc") - setValue invoked with "abc"
244+
* </pre>
245+
*
246+
* @param <T> the type of the object
247+
* @param objects the values to test, may be {@code null} or empty
248+
* @param consumer the consumer operation to invoke with the first non-null {@code objects}.
249+
* @see #firstNonNull(Object...)
250+
* @see #applyIfNonNull(Consumer, Object)
251+
* @since 3.12
252+
*/
253+
@SafeVarargs
254+
public static <T> void applyFirstNonNull(final Consumer<T> consumer, final T... objects) {
255+
applyIfNonNull(consumer, firstNonNull(objects));
256+
}
257+
258+
/**
259+
* <p>
260+
* Invokes the given {@code consumer's} {@link Consumer#accept(Object)} with the {@code object} if it is
261+
* {@code non-null}, otherwise the consumer is not invoked.
262+
* </p>
263+
*
264+
* <p>
265+
* The caller is responsible for thread-safety and exception handling of consumer.
266+
* </p>
267+
*
268+
* <pre>
269+
* ObjectUtils.applyIfNonNull(bean::setValue, null) - setValue not invoked
270+
* ObjectUtils.applyIfNonNull(bean::setValue, "abc") - setValue invoked with "abc"
271+
* ObjectUtils.applyIfNonNull(v -&gt; bean.setValue(v), "abc") - setValue invoked with "abc"
272+
* </pre>
273+
*
274+
* @param <T> the type of the object
275+
* @param object the {@code Object} to test, may be {@code null}
276+
* @param consumer the consumer operation to invoke with {@code object} if it is {@code non-null}
277+
* @see #applyFirstNonNull(Consumer, Object...)
278+
* @since 3.12
279+
*/
280+
public static <T> void applyIfNonNull(final Consumer<T> consumer, final T object) {
281+
if (object != null) {
282+
consumer.accept(object);
283+
}
284+
}
285+
229286
// cloning
230287
//-----------------------------------------------------------------------
231288
/**

src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,33 @@ public void testEquals() {
245245
assertTrue(ObjectUtils.equals(FOO, FOO), "ObjectUtils.equals(\"foo\", \"foo\") returned false");
246246
}
247247

248+
@Test
249+
public void testApplyIfNonNull() {
250+
final ApplyIfNonNullBean bean = new ApplyIfNonNullBean();
251+
bean.setValue(FOO);
252+
253+
ObjectUtils.applyIfNonNull(bean::setValue, null);
254+
assertEquals(FOO, bean.getValue());
255+
256+
ObjectUtils.applyIfNonNull(bean::setValue, BAR);
257+
assertEquals(BAR, bean.getValue());
258+
259+
ObjectUtils.applyIfNonNull(v -> bean.setValue(v), FOO);
260+
assertEquals(FOO, bean.getValue());
261+
}
262+
263+
@Test
264+
public void testApplyFirstNonNull() {
265+
final ApplyIfNonNullBean bean = new ApplyIfNonNullBean();
266+
bean.setValue(FOO);
267+
268+
ObjectUtils.applyFirstNonNull(bean::setValue, null, null, null);
269+
assertEquals(FOO, bean.getValue());
270+
271+
ObjectUtils.applyFirstNonNull(bean::setValue, null, null, BAR, FOO, null);
272+
assertEquals(BAR, bean.getValue());
273+
}
274+
248275
@Test
249276
public void testNotEqual() {
250277
assertFalse(ObjectUtils.notEqual(null, null), "ObjectUtils.notEqual(null, null) returned false");
@@ -764,4 +791,13 @@ public int compare(final CharSequence o1, final CharSequence o2) {
764791

765792
}
766793

794+
static final class ApplyIfNonNullBean {
795+
private String value;
796+
public String getValue() {
797+
return value;
798+
}
799+
public void setValue(String value) {
800+
this.value = value;
801+
}
802+
}
767803
}

0 commit comments

Comments
 (0)