Skip to content

Commit 3470df0

Browse files
author
Vincent Potucek
committed
add ObjectUtils.firstNonNull/getIfNull/getIfEmpty
Signed-off-by: Vincent Potucek <vpotucek@me.com>
1 parent eef7c29 commit 3470df0

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ public static <T> Optional<T> firstNonNull(@Nullable final T... objects) {
133133
.orElse(Optional.empty());
134134
}
135135

136+
/**
137+
* Returns the first non-null element from the provided collection.
138+
*
139+
* @param collection The objects to check for non-null values.
140+
* @param <T> The type of the objects.
141+
* @return An Optional containing the first non-null object, or an empty Optional if all objects are null.
142+
*/
143+
public static <T> Optional<T> firstNonNull(@Nullable final Collection<T> collection) {
144+
if (Objects.isNull(collection) || collection.isEmpty()) {
145+
return Optional.empty();
146+
}
147+
return collection.stream().filter(Objects::nonNull).findFirst();
148+
}
149+
136150
/**
137151
* Returns the object if it is not {@code null}; otherwise, returns the default value.
138152
*
@@ -161,6 +175,26 @@ public static <T> Optional<Collection<T>> getIfEmpty(@Nullable final Collection<
161175
.or(() -> Optional.ofNullable(defaultValue));
162176
}
163177

178+
/**
179+
* Returns the array if it is not {@code null} and not empty; otherwise, returns the default value.
180+
*
181+
* @param object the array to test, may be {@code null}
182+
* @param defaultValue the default value to return if the array is {@code null} or empty, may be {@code null}
183+
* @param <T> the type of elements in the array
184+
* @return An Optional containing the array if not null and not empty, otherwise an Optional of the default
185+
* value.
186+
*/
187+
public static <T> Optional<T[]> getIfEmpty(@Nullable final T[] object,
188+
@Nullable final T[] defaultValue) {
189+
if (Objects.nonNull(object) && object.length > 0) {
190+
return Optional.of(object);
191+
}
192+
if (Objects.nonNull(defaultValue) && defaultValue.length > 0) {
193+
return Optional.of(defaultValue);
194+
}
195+
return Optional.empty();
196+
}
197+
164198
/**
165199
* Determine whether the given object is empty.
166200
* <p>This method supports the following object types.

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

Lines changed: 37 additions & 3 deletions
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,11 +145,44 @@ 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();
148148
}
149149

150150
@Test
151-
void firstNonNullObject() {
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);
164+
}
165+
166+
@Test
167+
void firstNonNullObjectCollection() {
168+
String value = UUID.randomUUID().toString();
169+
String backup = UUID.randomUUID().toString();
170+
171+
assertThat(ObjectUtils.firstNonNull(Arrays.asList(backup, value, value))).contains(backup);
172+
assertThat(ObjectUtils.firstNonNull(Arrays.asList(value, null, backup))).contains(value);
173+
assertThat(ObjectUtils.firstNonNull(Arrays.asList(backup, value, null))).contains(backup);
174+
assertThat(ObjectUtils.firstNonNull(Arrays.asList(value, backup))).contains(value);
175+
assertThat(ObjectUtils.firstNonNull(Arrays.asList(null, null, value))).contains(value);
176+
177+
String _null = null;
178+
assertThat(ObjectUtils.firstNonNull(Arrays.asList(null, null, _null))).isEmpty();
179+
assertThat(ObjectUtils.firstNonNull(Arrays.asList(null, "null", backup))).contains("null");
180+
assertThat(ObjectUtils.firstNonNull(Arrays.asList(null, null))).isEmpty();
181+
assertThat(ObjectUtils.firstNonNull(Collections.emptyList())).isEmpty();
182+
}
183+
184+
@Test
185+
void firstNonNullObjectArray() {
152186
String value = UUID.randomUUID().toString();
153187
String backup = UUID.randomUUID().toString();
154188

0 commit comments

Comments
 (0)