Skip to content

Commit d624371

Browse files
Vincent PotucekVincent Potucek
Vincent Potucek
authored and
Vincent Potucek
committed
add ObjectUtils.defaultIfNull
Signed-off-by: Vincent Potucek <[email protected]>
1 parent 68fce29 commit d624371

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

Diff for: spring-core/src/main/java/org/springframework/util/ObjectUtils.java

+42
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,48 @@ public static boolean isEmpty(@Nullable Object @Nullable [] array) {
118118
return (array == null || array.length == 0);
119119
}
120120

121+
/**
122+
* Returns the object if it is not {@code null}; otherwise, returns the default value.
123+
*
124+
* <pre>
125+
* ObjectUtils.getIfNull(null, null) = null
126+
* ObjectUtils.getIfNull(null, "") = ""
127+
* ObjectUtils.getIfNull(null, "zz") = "zz"
128+
* ObjectUtils.getIfNull("abc", *) = "abc"
129+
* ObjectUtils.getIfNull(Boolean.TRUE, *) = Boolean.TRUE
130+
* </pre>
131+
*
132+
* @param <T> the type of the object
133+
* @param object the object to test, may be {@code null}
134+
* @param defaultValue the default value to return if the object is {@code null}, may be {@code null}
135+
* @return {@code object} if it is not {@code null}; otherwise, {@code defaultValue}
136+
*/
137+
@Nullable
138+
public static <T> T getIfNull(@Nullable final T object, @Nullable final T defaultValue) {
139+
return Objects.nonNull(object) ? object : defaultValue;
140+
}
141+
142+
/**
143+
* Returns the collection if it is not {@code null} and not empty; otherwise, returns the default value.
144+
*
145+
* <pre>
146+
* ObjectUtils.getIfEmpty(List.of("a"), List.of("b")) = List.of("a")
147+
* ObjectUtils.getIfEmpty(null, List.of("b")) = List.of("b")
148+
* ObjectUtils.getIfEmpty(Collections.emptyList(), List.of("b")) = List.of("b")
149+
* ObjectUtils.getIfEmpty(null, null) = null
150+
* </pre>
151+
*
152+
* @param <T> the type of elements in the collection
153+
* @param object the collection to test, may be {@code null}
154+
* @param defaultValue the default value to return if the collection is {@code null} or empty, may be {@code null}
155+
* @return {@code object} if it is not {@code null} and not empty; otherwise, {@code defaultValue}
156+
*/
157+
@Nullable
158+
public static <T> Collection<T> getIfEmpty(@Nullable final Collection<T> object,
159+
@Nullable final Collection<T> defaultValue) {
160+
return Objects.nonNull(object) && !object.isEmpty() ? object : defaultValue;
161+
}
162+
121163
/**
122164
* Determine whether the given object is empty.
123165
* <p>This method supports the following object types.

Diff for: spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.time.LocalDate;
3232
import java.time.ZoneId;
3333
import java.util.Arrays;
34+
import java.util.Collection;
3435
import java.util.Collections;
3536
import java.util.Currency;
3637
import java.util.Date;
@@ -52,6 +53,8 @@
5253
import static org.assertj.core.api.Assertions.assertThat;
5354
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5455
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
56+
import static org.springframework.util.ObjectUtils.getIfEmpty;
57+
import static org.springframework.util.ObjectUtils.getIfNull;
5558
import static org.springframework.util.ObjectUtils.isEmpty;
5659

5760
/**
@@ -111,6 +114,35 @@ void isCompatibleWithThrowsClause() {
111114
assertThat(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), throwable)).isTrue();
112115
}
113116

117+
@Test
118+
void testGetIfNull() {
119+
String value = UUID.randomUUID().toString();
120+
String backup = UUID.randomUUID().toString();
121+
assertThat(getIfNull(value, backup)).isEqualTo(value);
122+
assertThat(getIfNull(value, value)).isEqualTo(value);
123+
assertThat(getIfNull(backup, backup)).isEqualTo(backup);
124+
assertThat(getIfNull(null, value)).isEqualTo(value);
125+
assertThat(getIfNull(null, backup)).isEqualTo(backup);
126+
assertThat(getIfNull("null", backup)).isEqualTo("null");
127+
assertThat(Optional.ofNullable(getIfNull(null, null))).isEmpty();
128+
}
129+
130+
@Test
131+
void testGetIfEmpty() {
132+
Collection<String> empty = Collections.emptyList();
133+
Collection<String> value = List.of(UUID.randomUUID().toString());
134+
Collection<String> backup = List.of(UUID.randomUUID().toString());
135+
assertThat(getIfEmpty(value, backup)).isEqualTo(value);
136+
assertThat(getIfEmpty(null, backup)).isEqualTo(backup);
137+
assertThat(getIfEmpty(empty, backup)).isEqualTo(backup);
138+
assertThat(getIfEmpty(value, empty)).isEqualTo(value);
139+
assertThat(getIfEmpty(empty, empty)).isEqualTo(empty);
140+
assertThat(getIfEmpty(backup, backup)).isEqualTo(backup);
141+
assertThat(getIfEmpty(null, empty)).isEqualTo(empty);
142+
assertThat(getIfEmpty(null, backup)).isEqualTo(backup);
143+
assertThat(Optional.ofNullable(getIfEmpty(null, null))).isEmpty();
144+
}
145+
114146
@Test
115147
void isEmptyNull() {
116148
assertThat(isEmpty(null)).isTrue();

0 commit comments

Comments
 (0)