This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 234c6a9b8 LANG-1733: Add null handling features working with Java 8 
specs (#1215)
234c6a9b8 is described below

commit 234c6a9b84cb4bfb03e54c717df3ccd940b9884a
Author: Jongjin Bae <47495187+i...@users.noreply.github.com>
AuthorDate: Thu May 30 03:18:19 2024 +0900

    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....@navercorp.com>
---
 .../apache/commons/lang3/function/Consumers.java   | 16 +++++++++++-
 .../apache/commons/lang3/function/Functions.java   | 15 +++++++++++
 .../commons/lang3/function/ConsumersTest.java      | 29 ++++++++++++++++++++++
 .../commons/lang3/function/FunctionsTest.java      | 19 ++++++++++++++
 4 files changed, 78 insertions(+), 1 deletion(-)

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 <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);
+        }
+    }
 }
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 <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;
+    }
 }
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<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;
+        }
+    }
 }
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));
+    }
 }

Reply via email to