Add ObjectUtils.toString(Object, Supplier<String>).

This commit is contained in:
Gary Gregory 2020-06-23 15:15:38 -04:00
parent 45b32d3a9c
commit c26c72f8a8
3 changed files with 36 additions and 2 deletions

View File

@ -79,7 +79,8 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1556" type="add" dev="ggregory" due-to="XenoAmess">Use Java 8 lambdas and Map operations.</action> <action issue="LANG-1556" type="add" dev="ggregory" due-to="XenoAmess">Use Java 8 lambdas and Map operations.</action>
<action issue="LANG-1565" type="add" dev="ggregory" due-to="XenoAmess">Change removeLastFieldSeparator to use endsWith #550.</action> <action issue="LANG-1565" type="add" dev="ggregory" due-to="XenoAmess">Change removeLastFieldSeparator to use endsWith #550.</action>
<action issue="LANG-1557" type="add" dev="ggregory" due-to="XenoAmess, Gary Gregory">Change a Pattern to a static final field, for not letting it compile each time the function invoked. #542.</action> <action issue="LANG-1557" type="add" dev="ggregory" due-to="XenoAmess, Gary Gregory">Change a Pattern to a static final field, for not letting it compile each time the function invoked. #542.</action>
<action type="add" dev="ggregory">Added ImmutablePair factory methods left() and right().</action> <action type="add" dev="ggregory">Add ImmutablePair factory methods left() and right().</action>
<action type="add" dev="ggregory">Add ObjectUtils.toString(Object, Supplier&lt;String&gt;).</action>
</release> </release>
<release version="3.10" date="2020-03-22" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11."> <release version="3.10" date="2020-03-22" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">

View File

@ -789,7 +789,7 @@ public class ObjectUtils {
builder.append(name) builder.append(name)
.append(AT_SIGN) .append(AT_SIGN)
.append(hexString); .append(hexString);
// @formatter:off // @formatter:on
return builder.toString(); return builder.toString();
} }
@ -1182,6 +1182,30 @@ public class ObjectUtils {
return obj == null ? nullStr : obj.toString(); return obj == null ? nullStr : obj.toString();
} }
/**
* <p>Gets the {@code toString} of an {@code Object} returning
* a specified text if {@code null} input.</p>
*
* <pre>
* ObjectUtils.toString(obj, () -&gt; expensive())
* </pre>
* <pre>
* ObjectUtils.toString(null, () -&gt; expensive()) = result of expensive()
* ObjectUtils.toString(null, () -&gt; expensive()) = result of expensive()
* ObjectUtils.toString("", () -&gt; expensive()) = ""
* ObjectUtils.toString("bat", () -&gt; expensive()) = "bat"
* ObjectUtils.toString(Boolean.TRUE, () -&gt; expensive()) = "true"
* </pre>
*
* @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<String> supplier) {
return obj == null ? supplier == null ? null : supplier.get() : obj.toString();
}
/** /**
* <p>{@code ObjectUtils} instances should NOT be constructed in * <p>{@code ObjectUtils} instances should NOT be constructed in
* standard programming. Instead, the static methods on the class should * standard programming. Instead, the static methods on the class should

View File

@ -361,6 +361,15 @@ public class ObjectUtilsTest {
assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) ); assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) );
} }
@Test
public void testToString_SupplierString() {
assertEquals(null, ObjectUtils.toString(null, (Supplier<String>) 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 @SuppressWarnings("cast") // 1 OK, because we are checking for code change
@Test @Test
public void testNull() { public void testNull() {