diff --git a/src/main/java/org/apache/commons/lang3/function/Consumers.java b/src/main/java/org/apache/commons/lang3/function/Consumers.java index 1d505d636..43f161d07 100644 --- a/src/main/java/org/apache/commons/lang3/function/Consumers.java +++ b/src/main/java/org/apache/commons/lang3/function/Consumers.java @@ -35,7 +35,7 @@ public class Consumers { * Gets the NOP Consumer singleton. * * @param type type to consume. - * @return the NOP Consumer singleton.. + * @return the NOP Consumer singleton. */ @SuppressWarnings("unchecked") public static Consumer 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 the type of the argument the consumer accepts. + * @since 3.15.0 + */ + public static void accept(final T object, final Consumer consumer) { + if (consumer != null) { + consumer.accept(object); + } + } } diff --git a/src/main/java/org/apache/commons/lang3/function/Functions.java b/src/main/java/org/apache/commons/lang3/function/Functions.java index d33cc34ba..cf8309f8f 100644 --- a/src/main/java/org/apache/commons/lang3/function/Functions.java +++ b/src/main/java/org/apache/commons/lang3/function/Functions.java @@ -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 the type of the argument the function applies. + * @param 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 R apply(final T object, final Function function) { + return function != null ? function.apply(object) : null; + } } diff --git a/src/test/java/org/apache/commons/lang3/function/ConsumersTest.java b/src/test/java/org/apache/commons/lang3/function/ConsumersTest.java index 35c7dda7b..47828debf 100644 --- a/src/test/java/org/apache/commons/lang3/function/ConsumersTest.java +++ b/src/test/java/org/apache/commons/lang3/function/ConsumersTest.java @@ -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 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 implements Consumer { + private boolean isCalled; + + @Override + public void accept(T t) { + isCalled = true; + } + } } diff --git a/src/test/java/org/apache/commons/lang3/function/FunctionsTest.java b/src/test/java/org/apache/commons/lang3/function/FunctionsTest.java index f97cb0c45..4693eb00a 100644 --- a/src/test/java/org/apache/commons/lang3/function/FunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/function/FunctionsTest.java @@ -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)); + } }