From c26c72f8a8ee9996bff2bf139eb42fc2c56e62d8 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Tue, 23 Jun 2020 15:15:38 -0400 Subject: [PATCH] Add ObjectUtils.toString(Object, Supplier). --- src/changes/changes.xml | 3 ++- .../org/apache/commons/lang3/ObjectUtils.java | 26 ++++++++++++++++++- .../apache/commons/lang3/ObjectUtilsTest.java | 9 +++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 03989d564..4694dcddb 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -79,7 +79,8 @@ The type attribute can be add,update,fix,remove. Use Java 8 lambdas and Map operations. Change removeLastFieldSeparator to use endsWith #550. Change a Pattern to a static final field, for not letting it compile each time the function invoked. #542. - Added ImmutablePair factory methods left() and right(). + Add ImmutablePair factory methods left() and right(). + Add ObjectUtils.toString(Object, Supplier<String>). diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index f1e5fa2a9..95e886228 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -789,7 +789,7 @@ public class ObjectUtils { builder.append(name) .append(AT_SIGN) .append(hexString); - // @formatter:off + // @formatter:on return builder.toString(); } @@ -1182,6 +1182,30 @@ public class ObjectUtils { return obj == null ? nullStr : obj.toString(); } + /** + *

Gets the {@code toString} of an {@code Object} returning + * a specified text if {@code null} input.

+ * + *
+     * ObjectUtils.toString(obj, () -> expensive())
+     * 
+ *
+     * ObjectUtils.toString(null, () -> expensive())         = result of expensive()
+     * ObjectUtils.toString(null, () -> expensive())         = result of expensive()
+     * ObjectUtils.toString("", () -> expensive())           = ""
+     * ObjectUtils.toString("bat", () -> expensive())        = "bat"
+     * ObjectUtils.toString(Boolean.TRUE, () -> expensive()) = "true"
+     * 
+ * + * @param obj the Object to {@code toString}, may be null + * @param supplier the Supplier of String used on {@code null} input, may be null + * @return the passed in Object's toString, or {@code nullStr} if {@code null} input + * @since 3.11 + */ + public static String toString(final Object obj, final Supplier supplier) { + return obj == null ? supplier == null ? null : supplier.get() : obj.toString(); + } + /** *

{@code ObjectUtils} instances should NOT be constructed in * standard programming. Instead, the static methods on the class should diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index aab3a4e77..b1da3b69c 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -361,6 +361,15 @@ public class ObjectUtilsTest { assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) ); } + @Test + public void testToString_SupplierString() { + assertEquals(null, ObjectUtils.toString(null, (Supplier) null)); + assertEquals(null, ObjectUtils.toString(null, () -> null)); + // Pretend computing BAR is expensive. + assertEquals(BAR, ObjectUtils.toString(null, () -> BAR)); + assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, () -> BAR)); + } + @SuppressWarnings("cast") // 1 OK, because we are checking for code change @Test public void testNull() {