Skip to content

Commit 4c2a4f8

Browse files
author
Vincent Potucek
committed
add ObjectUtils.getNonNull/getIfNull/getIfEmpty
Signed-off-by: Vincent Potucek <vpotucek@me.com>
1 parent 11c1c8e commit 4c2a4f8

File tree

2 files changed

+65
-43
lines changed

2 files changed

+65
-43
lines changed

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,46 +118,47 @@ public static boolean isEmpty(@Nullable Object @Nullable [] array) {
118118
return (array == null || array.length == 0);
119119
}
120120

121+
121122
/**
122-
* Returns the object if it is not {@code null}; otherwise, returns the default value.
123+
* Returns the first non-null element from the provided varargs.
123124
*
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>
125+
* @param objects The objects to check for non-null values.
126+
* @param <T> The type of the objects.
127+
* @return An Optional containing the first non-null object, or an empty Optional if all objects are null.
128+
*/
129+
@SafeVarargs
130+
public static <T> Optional<T> getNonNull(@Nullable final T... objects) {
131+
return Optional.ofNullable(objects)
132+
.map(arr -> Arrays.stream(arr).filter(Objects::nonNull).findFirst())
133+
.orElse(Optional.empty());
134+
}
135+
136+
/**
137+
* Returns the object if it is not {@code null}; otherwise, returns the default value.
131138
*
132-
* @param <T> the type of the object
133139
* @param object the object to test, may be {@code null}
134140
* @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}
141+
* @param <T> the type of the object
142+
* @return An Optional containing either the object if not null, or the default value wrapped in an Optional.
136143
*/
137-
@Nullable
138-
public static <T> T getIfNull(@Nullable final T object, @Nullable final T defaultValue) {
139-
return Objects.nonNull(object) ? object : defaultValue;
144+
public static <T> Optional<T> getIfNull(@Nullable final T object, @Nullable final T defaultValue) {
145+
return Optional.ofNullable(object).or(() -> Optional.ofNullable(defaultValue));
140146
}
141147

142148
/**
143149
* Returns the collection if it is not {@code null} and not empty; otherwise, returns the default value.
144150
*
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
153151
* @param object the collection to test, may be {@code null}
154152
* @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;
153+
* @param <T> the type of elements in the collection
154+
* @return An Optional containing the collection if not null and not empty, otherwise an Optional of the default
155+
* value.
156+
*/
157+
public static <T> Optional<Collection<T>> getIfEmpty(@Nullable final Collection<T> object,
158+
@Nullable final Collection<T> defaultValue) {
159+
return Optional.ofNullable(object)
160+
.filter(collection -> !collection.isEmpty())
161+
.or(() -> Optional.ofNullable(defaultValue));
161162
}
162163

163164
/**

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

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
5656
import static org.springframework.util.ObjectUtils.getIfEmpty;
5757
import static org.springframework.util.ObjectUtils.getIfNull;
58+
import static org.springframework.util.ObjectUtils.getNonNull;
5859
import static org.springframework.util.ObjectUtils.isEmpty;
5960

6061
/**
@@ -118,29 +119,49 @@ void isCompatibleWithThrowsClause() {
118119
void getIfNullObject() {
119120
String value = UUID.randomUUID().toString();
120121
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();
122+
123+
assertThat(getIfNull(value, backup)).contains(value);
124+
assertThat(getIfNull(value, backup)).contains(value);
125+
assertThat(getIfNull(value, value)).contains(value);
126+
assertThat(getIfNull(backup, backup)).contains(backup);
127+
assertThat(getIfNull(null, value)).contains(value);
128+
assertThat(getIfNull(null, backup)).contains(backup);
129+
assertThat(getIfNull("null", backup)).contains("null");
130+
assertThat(getIfNull(null, null)).isEmpty();
128131
}
129132

130133
@Test
131134
void getIfEmptyObject() {
132135
Collection<String> empty = Collections.emptyList();
133136
Collection<String> value = List.of(UUID.randomUUID().toString());
134137
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();
138+
139+
assertThat(getIfEmpty(value, backup)).contains(value);
140+
assertThat(getIfEmpty(null, backup)).contains(backup);
141+
assertThat(getIfEmpty(empty, backup)).contains(backup);
142+
assertThat(getIfEmpty(value, empty)).contains(value);
143+
assertThat(getIfEmpty(empty, empty)).contains(empty);
144+
assertThat(getIfEmpty(backup, backup)).contains(backup);
145+
assertThat(getIfEmpty(null, empty)).contains(empty);
146+
assertThat(getIfEmpty(null, backup)).contains(backup);
147+
assertThat(getIfEmpty(null, null)).isEmpty();
148+
}
149+
150+
@Test
151+
void getNonNullObject() {
152+
String value = UUID.randomUUID().toString();
153+
String backup = UUID.randomUUID().toString();
154+
155+
assertThat(getNonNull(backup, value, value)).contains(backup);
156+
assertThat(getNonNull(value, null, backup)).contains(value);
157+
assertThat(getNonNull(backup, value, null)).contains(backup);
158+
assertThat(getNonNull(value, backup)).contains(value);
159+
assertThat(getNonNull(null, null, value)).contains(value);
160+
161+
String _null = null;
162+
assertThat(getNonNull(null, null, _null)).isEmpty();
163+
assertThat(getNonNull(null, "null", backup)).contains("null");
164+
assertThat(getNonNull(null, null)).isEmpty();
144165
}
145166

146167
@Test

0 commit comments

Comments
 (0)