Skip to content

Commit 177911e

Browse files
committed
Add ObjectUtils.getIfNull(Object, Object) and deprecate
defaultIfNull(Object, Object) A adaptation of PR #1355 by Pankraz76
1 parent 5a15ccb commit 177911e

File tree

8 files changed

+64
-8
lines changed

8 files changed

+64
-8
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ The <action> type attribute can be add,update,fix,remove.
123123
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.JAVA_SECURITY_KERBEROS_KDC.</action>
124124
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemProperties.JAVA_SECURITY_KERBEROS_REAL.</action>
125125
<action type="add" dev="ggregory" due-to="kommalapatiraviteja">Add ArrayFill.fill(boolean[], boolean) #1386.</action>
126+
<action type="add" dev="ggregory" due-to="Pankraz76, Gary Gregory">Add ObjectUtils.getIfNull(Object, Object) and deprecate defaultIfNull(Object, Object).</action>
126127
<!-- UPDATE -->
127128
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 84 #1267, #1277, #1283, #1288, #1302, #1377.</action>
128129
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action>

src/main/java/org/apache/commons/lang3/ObjectUtils.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,13 @@ public static short CONST_SHORT(final int v) {
580580
* @param object the {@link Object} to test, may be {@code null}
581581
* @param defaultValue the default value to return, may be {@code null}
582582
* @return {@code object} if it is not {@code null}, defaultValue otherwise
583-
* TODO Rename to getIfNull in 4.0
583+
* @see #getIfNull(Object, Object)
584+
* @see #getIfNull(Object, Supplier)
585+
* @deprecated Use {@link #getIfNull(Object, Object)}.
584586
*/
587+
@Deprecated
585588
public static <T> T defaultIfNull(final T object, final T defaultValue) {
586-
return object != null ? object : defaultValue;
589+
return getIfNull(object, defaultValue);
587590
}
588591

589592
// Null-safe equals/hashCode
@@ -703,12 +706,35 @@ public static <T> T getFirstNonNull(final Supplier<T>... suppliers) {
703706
* @param object the {@link Object} to test, may be {@code null}
704707
* @param defaultSupplier the default value to return, may be {@code null}
705708
* @return {@code object} if it is not {@code null}, {@code defaultValueSupplier.get()} otherwise
709+
* @see #getIfNull(Object, Object)
706710
* @since 3.10
707711
*/
708712
public static <T> T getIfNull(final T object, final Supplier<T> defaultSupplier) {
709713
return object != null ? object : Suppliers.get(defaultSupplier);
710714
}
711715

716+
/**
717+
* Returns a default value if the object passed is {@code null}.
718+
*
719+
* <pre>
720+
* ObjectUtils.getIfNull(null, null) = null
721+
* ObjectUtils.getIfNull(null, "") = ""
722+
* ObjectUtils.getIfNull(null, "zz") = "zz"
723+
* ObjectUtils.getIfNull("abc", *) = "abc"
724+
* ObjectUtils.getIfNull(Boolean.TRUE, *) = Boolean.TRUE
725+
* </pre>
726+
*
727+
* @param <T> the type of the object
728+
* @param object the {@link Object} to test, may be {@code null}
729+
* @param defaultValue the default value to return, may be {@code null}
730+
* @return {@code object} if it is not {@code null}, defaultValue otherwise
731+
* @see #getIfNull(Object, Supplier)
732+
* @since 3.18.0
733+
*/
734+
public static <T> T getIfNull(final T object, final T defaultValue) {
735+
return object != null ? object : defaultValue;
736+
}
737+
712738
/**
713739
* Gets the hash code of an object returning zero when the
714740
* object is {@code null}.

src/main/java/org/apache/commons/lang3/builder/Diff.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class Diff<T> extends Pair<T, T> {
5656
*/
5757
protected Diff(final String fieldName) {
5858
this.fieldName = Objects.requireNonNull(fieldName);
59-
this.type = ObjectUtils.defaultIfNull(TypeUtils.getTypeArguments(getClass(), Diff.class).get(Diff.class.getTypeParameters()[0]), Object.class);
59+
this.type = ObjectUtils.getIfNull(TypeUtils.getTypeArguments(getClass(), Diff.class).get(Diff.class.getTypeParameters()[0]), Object.class);
6060
}
6161

6262
Diff(final String fieldName, final Type type) {

src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ private static final class WildcardTypeImpl implements WildcardType {
235235
* @param lowerBounds of this type
236236
*/
237237
private WildcardTypeImpl(final Type[] upperBounds, final Type[] lowerBounds) {
238-
this.upperBounds = ObjectUtils.defaultIfNull(upperBounds, ArrayUtils.EMPTY_TYPE_ARRAY);
239-
this.lowerBounds = ObjectUtils.defaultIfNull(lowerBounds, ArrayUtils.EMPTY_TYPE_ARRAY);
238+
this.upperBounds = ObjectUtils.getIfNull(upperBounds, ArrayUtils.EMPTY_TYPE_ARRAY);
239+
this.lowerBounds = ObjectUtils.getIfNull(lowerBounds, ArrayUtils.EMPTY_TYPE_ARRAY);
240240
}
241241

242242
/**

src/main/java/org/apache/commons/lang3/text/FormattableUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static Formatter append(final CharSequence seq, final Formatter formatter
9797
"Specified ellipsis '%1$s' exceeds precision of %2$s", ellipsis, Integer.valueOf(precision));
9898
final StringBuilder buf = new StringBuilder(seq);
9999
if (precision >= 0 && precision < seq.length()) {
100-
final CharSequence actualEllipsis = ObjectUtils.defaultIfNull(ellipsis, StringUtils.EMPTY);
100+
final CharSequence actualEllipsis = ObjectUtils.getIfNull(ellipsis, StringUtils.EMPTY);
101101
buf.replace(precision - actualEllipsis.length(), seq.length(), actualEllipsis.toString());
102102
}
103103
final boolean leftJustify = (flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags.LEFT_JUSTIFY;

src/main/java/org/apache/commons/lang3/time/DurationUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public static int toMillisInt(final Duration duration) {
219219
* @return The given duration or {@link Duration#ZERO}.
220220
*/
221221
public static Duration zeroIfNull(final Duration duration) {
222-
return ObjectUtils.defaultIfNull(duration, Duration.ZERO);
222+
return ObjectUtils.getIfNull(duration, Duration.ZERO);
223223
}
224224

225225
/**

src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,35 @@ public void testGetFirstNonNull() {
503503
assertEquals(Boolean.TRUE, ObjectUtils.getFirstNonNull(Suppliers.nul(), () -> Boolean.TRUE));
504504
}
505505

506+
@Test
507+
public void testGetIfNullObject() {
508+
final Object o = FOO;
509+
final Object defaultObject = BAR;
510+
assertNull(ObjectUtils.getIfNull(null, (Object) null));
511+
assertSame(defaultObject, ObjectUtils.getIfNull(null, defaultObject), "dflt was not returned when o was null");
512+
assertSame(o, ObjectUtils.getIfNull(o, defaultObject), "dflt was returned when o was not null");
513+
}
514+
515+
@Test
516+
public void testGetIfNullSupplier() {
517+
final Object o = FOO;
518+
final Object defaultObject = BAR;
519+
assertNull(ObjectUtils.getIfNull(null, (Supplier<Object>) null));
520+
assertSame(defaultObject, ObjectUtils.getIfNull(null, () -> defaultObject), "dflt was not returned when o was null");
521+
assertSame(o, ObjectUtils.getIfNull(o, () -> defaultObject), "dflt was returned when o was not null");
522+
assertSame(o, ObjectUtils.getIfNull(FOO, () -> defaultObject), "dflt was returned when o was not null");
523+
assertSame(o, ObjectUtils.getIfNull("foo", () -> defaultObject), "dflt was returned when o was not null");
524+
final MutableInt callsCounter = new MutableInt(0);
525+
final Supplier<Object> countingDefaultSupplier = () -> {
526+
callsCounter.increment();
527+
return defaultObject;
528+
};
529+
ObjectUtils.getIfNull(o, countingDefaultSupplier);
530+
assertEquals(0, callsCounter.getValue());
531+
ObjectUtils.getIfNull(null, countingDefaultSupplier);
532+
assertEquals(1, callsCounter.getValue());
533+
}
534+
506535
@Test
507536
public void testHashCode() {
508537
assertEquals(0, ObjectUtils.hashCode(null));

src/test/java/org/apache/commons/lang3/function/Objects.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
*
5353
* This class is somewhat redundant with regards to {@link ObjectUtils}.
5454
* For example, {@link #requireNonNull(Object, Object)} is almost equivalent
55-
* with {@link ObjectUtils#defaultIfNull(Object, Object)}. However, it isn't
55+
* with {@link ObjectUtils#getIfNull(Object, Object)}. However, it isn't
5656
* quite the same, because the latter can, in fact, return null. The former
5757
* can't, and the Java compiler confirms this.(An alternative to redundancy
5858
* would have been to change the {@code ObjectUtils} class. However, that

0 commit comments

Comments
 (0)