Skip to content

Commit 5a89550

Browse files
author
Vincent Potucek
committed
add ObjectUtils.firstNonNull/getIfNull/getIfEmpty
Signed-off-by: Vincent Potucek <[email protected]>
1 parent eef7c29 commit 5a89550

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ public static <T> Optional<Collection<T>> getIfEmpty(@Nullable final Collection<
161161
.or(() -> Optional.ofNullable(defaultValue));
162162
}
163163

164+
/**
165+
* Returns the array if it is not {@code null} and not empty; otherwise, returns the default value.
166+
*
167+
* @param object the array to test, may be {@code null}
168+
* @param defaultValue the default value to return if the array is {@code null} or empty, may be {@code null}
169+
* @param <T> the type of elements in the array
170+
* @return An Optional containing the array if not null and not empty, otherwise an Optional of the default
171+
* value.
172+
*/
173+
public static <T> Optional<T[]> getIfEmpty(@Nullable final T[] object,
174+
@Nullable final T[] defaultValue) {
175+
if (Objects.nonNull(object) && object.length > 0) {
176+
return Optional.of(object);
177+
}
178+
if (Objects.nonNull(defaultValue) && defaultValue.length > 0) {
179+
return Optional.of(defaultValue);
180+
}
181+
return Optional.empty();
182+
}
183+
164184
/**
165185
* Determine whether the given object is empty.
166186
* <p>This method supports the following object types.

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,12 @@ void getIfNullObject() {
131131
}
132132

133133
@Test
134-
void getIfEmptyObject() {
134+
void getIfEmptyObjectCollection() {
135135
Collection<String> empty = Collections.emptyList();
136136
Collection<String> value = List.of(UUID.randomUUID().toString());
137137
Collection<String> backup = List.of(UUID.randomUUID().toString());
138138

139+
assertThat(getIfEmpty(value, backup)).contains(value);
139140
assertThat(getIfEmpty(value, backup)).contains(value);
140141
assertThat(getIfEmpty(null, backup)).contains(backup);
141142
assertThat(getIfEmpty(empty, backup)).contains(backup);
@@ -144,7 +145,22 @@ void getIfEmptyObject() {
144145
assertThat(getIfEmpty(backup, backup)).contains(backup);
145146
assertThat(getIfEmpty(null, empty)).contains(empty);
146147
assertThat(getIfEmpty(null, backup)).contains(backup);
147-
assertThat(getIfEmpty(null, null)).isEmpty();
148+
}
149+
150+
@Test
151+
void getIfEmptyObjectArray() {
152+
String[] empty = new String[0];
153+
String[] value = {UUID.randomUUID().toString()};
154+
String[] backup = {UUID.randomUUID().toString()};
155+
156+
assertThat(getIfEmpty(value, backup)).contains(value);
157+
assertThat(getIfEmpty(null, backup)).contains(backup);
158+
assertThat(getIfEmpty(empty, backup)).contains(backup);
159+
assertThat(getIfEmpty(value, empty)).contains(value);
160+
assertThat(getIfEmpty(empty, empty)).isEmpty();
161+
assertThat(getIfEmpty(backup, backup)).contains(backup);
162+
assertThat(getIfEmpty(null, empty)).isEmpty();
163+
assertThat(getIfEmpty(null, backup)).contains(backup);
148164
}
149165

150166
@Test

0 commit comments

Comments
 (0)