Skip to content

Commit 7732f26

Browse files
committed
Changes for review comments on PR apache#684
- Changed method names from applyIfNonNull -> acceptIfNonNull; and applyFirstNonNull -> acceptFirstNonNull - Changed parameter order
1 parent 636dfba commit 7732f26

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,55 +229,56 @@ public static boolean anyNotNull(final Object... values) {
229229

230230
/**
231231
* <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.
232+
* Calls the given {@code consumer's} {@link Consumer#accept(Object)} method with the first {@code non-null} value
233+
* from {@code objects}. If all the values are null, the consumer is not invoked. This is equivalent to the call
234+
* {@code ObjectUtils.acceptIfNonNull(ObjectUtils.firstNonNull(objects), consumer)}
234235
* </p>
235236
*
236237
* <p>
237238
* The caller is responsible for thread-safety and exception handling of consumer.
238239
* </p>
239240
*
240241
* <pre>
241-
* ObjectUtils.applyFirstNonNull(bean::setValue, null) - setValue not invoked
242-
* ObjectUtils.applyFirstNonNull(bean::setValue, null, "abc", "def") - setValue invoked with "abc"
243-
* ObjectUtils.applyFirstNonNull(v -&gt; bean.setValue(v), "abc") - setValue invoked with "abc"
242+
* ObjectUtils.acceptFirstNonNull(bean::setValue, null) - setValue not invoked
243+
* ObjectUtils.acceptFirstNonNull(bean::setValue, null, "abc", "def") - setValue invoked with "abc"
244+
* ObjectUtils.acceptFirstNonNull(v -&gt; bean.setValue(v), "abc") - setValue invoked with "abc"
244245
* </pre>
245246
*
246247
* @param <T> the type of the object
247248
* @param objects the values to test, may be {@code null} or empty
248249
* @param consumer the consumer operation to invoke with the first non-null {@code objects}.
249250
* @see #firstNonNull(Object...)
250-
* @see #applyIfNonNull(Consumer, Object)
251+
* @see #acceptIfNonNull(Object, Consumer)
251252
* @since 3.12
252253
*/
253254
@SafeVarargs
254-
public static <T> void applyFirstNonNull(final Consumer<T> consumer, final T... objects) {
255-
applyIfNonNull(consumer, firstNonNull(objects));
255+
public static <T> void acceptFirstNonNull(final Consumer<T> consumer, final T... objects) {
256+
acceptIfNonNull(firstNonNull(objects), consumer);
256257
}
257258

258259
/**
259260
* <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.
261+
* Calls the given {@code consumer's} {@link Consumer#accept(Object)} method with the {@code object} if it is
262+
* {@code non-null}.
262263
* </p>
263264
*
264265
* <p>
265266
* The caller is responsible for thread-safety and exception handling of consumer.
266267
* </p>
267268
*
268269
* <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"
270+
* ObjectUtils.acceptIfNonNull(null, bean::setValue) - setValue not invoked
271+
* ObjectUtils.acceptIfNonNull("abc", bean::setValue) - setValue invoked with "abc"
272+
* ObjectUtils.acceptIfNonNull("abc", v -&gt; bean.setValue(v)) - setValue invoked with "abc"
272273
* </pre>
273274
*
274275
* @param <T> the type of the object
275276
* @param object the {@code Object} to test, may be {@code null}
276277
* @param consumer the consumer operation to invoke with {@code object} if it is {@code non-null}
277-
* @see #applyFirstNonNull(Consumer, Object...)
278+
* @see #acceptFirstNonNull(Consumer, Object...)
278279
* @since 3.12
279280
*/
280-
public static <T> void applyIfNonNull(final Consumer<T> consumer, final T object) {
281+
public static <T> void acceptIfNonNull(final T object, final Consumer<T> consumer) {
281282
if (object != null) {
282283
consumer.accept(object);
283284
}

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.HashSet;
4141
import java.util.List;
4242
import java.util.Map;
43+
import java.util.Objects;
4344
import java.util.Set;
4445
import java.util.function.Supplier;
4546

@@ -246,30 +247,30 @@ public void testEquals() {
246247
}
247248

248249
@Test
249-
public void testApplyIfNonNull() {
250+
public void testAcceptFirstNonNull() {
250251
final ApplyIfNonNullBean bean = new ApplyIfNonNullBean();
251252
bean.setValue(FOO);
252253

253-
ObjectUtils.applyIfNonNull(bean::setValue, null);
254+
ObjectUtils.acceptFirstNonNull(bean::setValue, null, null, null);
254255
assertEquals(FOO, bean.getValue());
255256

256-
ObjectUtils.applyIfNonNull(bean::setValue, BAR);
257+
ObjectUtils.acceptFirstNonNull(bean::setValue, null, null, BAR, FOO, null);
257258
assertEquals(BAR, bean.getValue());
258-
259-
ObjectUtils.applyIfNonNull(v -> bean.setValue(v), FOO);
260-
assertEquals(FOO, bean.getValue());
261259
}
262260

263261
@Test
264-
public void testApplyFirstNonNull() {
262+
public void testAcceptIfNonNull() {
265263
final ApplyIfNonNullBean bean = new ApplyIfNonNullBean();
266264
bean.setValue(FOO);
267265

268-
ObjectUtils.applyFirstNonNull(bean::setValue, null, null, null);
266+
ObjectUtils.acceptIfNonNull(null, bean::setValue);
269267
assertEquals(FOO, bean.getValue());
270268

271-
ObjectUtils.applyFirstNonNull(bean::setValue, null, null, BAR, FOO, null);
269+
ObjectUtils.acceptIfNonNull(BAR, bean::setValue);
272270
assertEquals(BAR, bean.getValue());
271+
272+
ObjectUtils.acceptIfNonNull(FOO, v -> bean.setValue(v));
273+
assertEquals(FOO, bean.getValue());
273274
}
274275

275276
@Test
@@ -797,7 +798,7 @@ public String getValue() {
797798
return value;
798799
}
799800
public void setValue(String value) {
800-
this.value = value;
801+
this.value = Objects.requireNonNull(value, "value");
801802
}
802803
}
803804
}

0 commit comments

Comments
 (0)