Skip to content

Commit

Permalink
Changes for review comments on PR #684
Browse files Browse the repository at this point in the history
- Changed method names from applyIfNonNull -> acceptIfNonNull; and
applyFirstNonNull -> acceptFirstNonNull
- Changed parameter order
  • Loading branch information
bindul committed Dec 30, 2020
1 parent 4450f1c commit 03165ee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
31 changes: 16 additions & 15 deletions src/main/java/org/apache/commons/lang3/ObjectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,55 +229,56 @@ public static boolean anyNotNull(final Object... values) {

/**
* <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.
* 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)}
* </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"
* ObjectUtils.acceptFirstNonNull(bean::setValue, null) - setValue not invoked
* ObjectUtils.acceptFirstNonNull(bean::setValue, null, "abc", "def") - setValue invoked with "abc"
* ObjectUtils.acceptFirstNonNull(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)
* @see #acceptIfNonNull(Object, Consumer)
* @since 3.12
*/
@SafeVarargs
public static <T> void applyFirstNonNull(final Consumer<T> consumer, final T... objects) {
applyIfNonNull(consumer, firstNonNull(objects));
public static <T> void acceptFirstNonNull(final Consumer<T> consumer, final T... objects) {
acceptIfNonNull(firstNonNull(objects), consumer);
}

/**
* <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.
* Calls the given {@code consumer's} {@link Consumer#accept(Object)} method with the {@code object} if it is
* {@code non-null}.
* </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"
* ObjectUtils.acceptIfNonNull(null, bean::setValue) - setValue not invoked
* ObjectUtils.acceptIfNonNull("abc", bean::setValue) - setValue invoked with "abc"
* ObjectUtils.acceptIfNonNull("abc", v -&gt; bean.setValue(v)) - 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...)
* @see #acceptFirstNonNull(Consumer, Object...)
* @since 3.12
*/
public static <T> void applyIfNonNull(final Consumer<T> consumer, final T object) {
public static <T> void acceptIfNonNull(final T object, final Consumer<T> consumer) {
if (object != null) {
consumer.accept(object);
}
Expand Down
21 changes: 11 additions & 10 deletions src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -797,7 +798,7 @@ public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
this.value = Objects.requireNonNull(value, "value");
}
}
}

0 comments on commit 03165ee

Please sign in to comment.