LANG-1733: Add null handling features working with Java 8 specs (#1215)
* LANG-1733: Add null handling features working with Java 8 specs * LANG-1733: Move null handling features to Consumers and Functions * LANG-1733: Update Javadoc * LANG-1733: Add since javadoc tag * LANG-1733: Add null check for consumer and function * LANG-1733: Revert Functions and Consumers class javadoc * LANG-1733: Rename methods * LANG-1733: Update null handling features to handle an object if it is null * LANG-1733: Update ConsumersTest to not use mock * LANG-1733: Update Comments * LANG-1733: Update codes to pass style check * LANG-1733: Modify the comment on apply method in Functions class --------- Co-authored-by: 배종진 <frog.bae@navercorp.com>
This commit is contained in:
parent
9babe58d89
commit
234c6a9b84
|
@ -35,7 +35,7 @@ public class Consumers {
|
|||
* Gets the NOP Consumer singleton.
|
||||
*
|
||||
* @param <T> type type to consume.
|
||||
* @return the NOP Consumer singleton..
|
||||
* @return the NOP Consumer singleton.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Consumer<T> nop() {
|
||||
|
@ -46,4 +46,18 @@ public class Consumers {
|
|||
// No instances.
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the given {@link Consumer} action to the object if the consumer is not {@code null}. Otherwise, does
|
||||
* nothing.
|
||||
*
|
||||
* @param object the object to be consumed.
|
||||
* @param consumer the consumer to consume.
|
||||
* @param <T> the type of the argument the consumer accepts.
|
||||
* @since 3.15.0
|
||||
*/
|
||||
public static <T> void accept(final T object, final Consumer<T> consumer) {
|
||||
if (consumer != null) {
|
||||
consumer.accept(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,4 +41,19 @@ public final class Functions {
|
|||
private Functions() {
|
||||
// no instances needed.
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the {@link Function} on the object if the function is not {@code null}. Otherwise, does nothing and
|
||||
* returns {@code null}.
|
||||
*
|
||||
* @param object the object to apply the function.
|
||||
* @param function the function to apply.
|
||||
* @param <T> the type of the argument the function applies.
|
||||
* @param <R> the type of the result the function returns.
|
||||
* @return the value the function returns if the function is not {@code null}; {@code null} otherwise.
|
||||
* @since 3.15.0
|
||||
*/
|
||||
public static <T, R> R apply(final T object, final Function<T, R> function) {
|
||||
return function != null ? function.apply(object) : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,9 @@
|
|||
|
||||
package org.apache.commons.lang3.function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
@ -46,4 +49,30 @@ public class ConsumersTest extends AbstractLangTest {
|
|||
Consumers.nop().accept("");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Consumers#accept(Object, Consumer)}.
|
||||
*/
|
||||
@Test
|
||||
public void testAccept() {
|
||||
final StringBuilder builder = new StringBuilder("foo");
|
||||
Consumers.accept(builder, sb -> sb.append("-bar"));
|
||||
assertEquals("foo-bar", builder.toString());
|
||||
|
||||
final TestConsumer<String> consumer = new TestConsumer<>();
|
||||
Consumers.accept(null, consumer);
|
||||
assertTrue(consumer.isCalled);
|
||||
|
||||
final StringBuilder builder2 = new StringBuilder("foo");
|
||||
Consumers.accept(builder2, null);
|
||||
assertEquals("foo", builder2.toString());
|
||||
}
|
||||
|
||||
private static final class TestConsumer<T> implements Consumer<T> {
|
||||
private boolean isCalled;
|
||||
|
||||
@Override
|
||||
public void accept(T t) {
|
||||
isCalled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,13 +18,32 @@
|
|||
package org.apache.commons.lang3.function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link Functions}.
|
||||
*/
|
||||
public class FunctionsTest {
|
||||
|
||||
/**
|
||||
* Tests {@link Functions#function(Function)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFunction() {
|
||||
assertEquals("foo", Functions.function(String::valueOf).andThen(String::toString).apply("foo"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Functions#apply(Object, Function)}.
|
||||
*/
|
||||
@Test
|
||||
public void testApply() {
|
||||
assertEquals("foo-bar", Functions.apply("foo", string -> string.concat("-bar")));
|
||||
assertEquals("foo-bar", Functions.apply(null, object -> "foo-bar"));
|
||||
assertNull(Functions.apply("foo", null));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue