You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/04/18 22:37:21 UTC

[commons-lang] branch master updated: Add and use Suppliers.

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 4e18561  Add and use Suppliers.
4e18561 is described below

commit 4e18561e49881d32cfbb85dc25640961a058dcf6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Apr 18 18:37:17 2021 -0400

    Add and use Suppliers.
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/lang3/ObjectUtils.java |  5 +--
 .../java/org/apache/commons/lang3/StringUtils.java |  5 +--
 .../apache/commons/lang3/function/Suppliers.java   | 40 +++++++++++++++++++++
 .../commons/lang3/function/SuppliersTest.java      | 41 ++++++++++++++++++++++
 5 files changed, 88 insertions(+), 4 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a7fbe83..b6ec5ba 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -53,6 +53,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getEnumSystemProperty(...).</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add TriConsumer.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add and use EnumUtils.getFirstEnumIgnoreCase(Class, String, Function, E).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add and use Suppliers.</action>
     <!-- UPDATE -->
     <action                   type="add" dev="ggregory" due-to="Dependabot">Bump spotbugs-maven-plugin from 4.2.0 to 4.2.2 #735.</action>
   </release>
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index 7eb227f..4bfac7e 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -32,6 +32,7 @@ import java.util.TreeSet;
 import java.util.function.Supplier;
 
 import org.apache.commons.lang3.exception.CloneFailedException;
+import org.apache.commons.lang3.function.Suppliers;
 import org.apache.commons.lang3.mutable.MutableInt;
 import org.apache.commons.lang3.text.StrBuilder;
 import org.apache.commons.lang3.time.DurationUtils;
@@ -737,7 +738,7 @@ public class ObjectUtils {
      * @since 3.10
      */
     public static <T> T getIfNull(final T object, final Supplier<T> defaultSupplier) {
-        return object != null ? object : defaultSupplier == null ? null : defaultSupplier.get();
+        return object != null ? object : Suppliers.get(defaultSupplier);
     }
 
     /**
@@ -1318,7 +1319,7 @@ public class ObjectUtils {
      * @since 3.11
      */
     public static String toString(final Object obj, final Supplier<String> supplier) {
-        return obj == null ? supplier == null ? null : supplier.get() : obj.toString();
+        return obj == null ? Suppliers.get(supplier) : obj.toString();
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index d702e10..5f22048 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -31,6 +31,7 @@ import java.util.StringJoiner;
 import java.util.function.Supplier;
 import java.util.regex.Pattern;
 
+import org.apache.commons.lang3.function.Suppliers;
 import org.apache.commons.lang3.function.ToBooleanBiFunction;
 
 /**
@@ -2210,7 +2211,7 @@ public class StringUtils {
      * @since 3.10
      */
     public static <T extends CharSequence> T getIfBlank(final T str, final Supplier<T> defaultSupplier) {
-        return isBlank(str) ? defaultSupplier == null ? null : defaultSupplier.get() : str;
+        return isBlank(str) ? Suppliers.get(defaultSupplier) : str;
     }
 
     /**
@@ -2238,7 +2239,7 @@ public class StringUtils {
      * @since 3.10
      */
     public static <T extends CharSequence> T getIfEmpty(final T str, final Supplier<T> defaultSupplier) {
-        return isEmpty(str) ? defaultSupplier == null ? null : defaultSupplier.get() : str;
+        return isEmpty(str) ? Suppliers.get(defaultSupplier) : str;
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/lang3/function/Suppliers.java b/src/main/java/org/apache/commons/lang3/function/Suppliers.java
new file mode 100644
index 0000000..28d0013
--- /dev/null
+++ b/src/main/java/org/apache/commons/lang3/function/Suppliers.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3.function;
+
+import java.util.function.Supplier;
+
+/**
+ * Helps use {@link Supplier}.
+ *
+ * @since 3.13.0
+ */
+public class Suppliers {
+
+    /**
+     * Null-safe call to {@link Supplier#get()}.
+     * 
+     * @param <T> the type of results supplied by this supplier.
+     * @param supplier the supplier or null.
+     * @return Result of {@link Supplier#get()} or null.
+     */
+    public static <T> T get(final Supplier<T> supplier) {
+        return supplier == null ? null : supplier.get();
+    }
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java b/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java
new file mode 100644
index 0000000..d4e2109
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/function/SuppliersTest.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.Supplier;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link Suppliers}.
+ */
+public class SuppliersTest {
+
+    /**
+     * Tests {@link Suppliers#get(Supplier)}.
+     */
+    @Test
+    public void testGet() {
+        assertNull(Suppliers.get(null));
+        assertNull(Suppliers.get(() -> null));
+        assertEquals("foo", Suppliers.get(() -> "foo"));
+    }
+}