Skip to content

Commit bf49b43

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

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

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

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

121-
122121
/**
123122
* Returns the first non-null element from the provided varargs.
124123
*
@@ -133,6 +132,20 @@ public static <T> Optional<T> firstNonNull(@Nullable final T... objects) {
133132
.orElse(Optional.empty());
134133
}
135134

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

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

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

+37-3
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)