Skip to content

add ObjectUtils.getNonNull/getIfNull/getIfEmpty #34587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,49 @@ public static boolean isEmpty(@Nullable Object @Nullable [] array) {
return (array == null || array.length == 0);
}


/**
* Returns the first non-null element from the provided varargs.
*
* @param objects The objects to check for non-null values.
* @param <T> The type of the objects.
* @return An Optional containing the first non-null object, or an empty Optional if all objects are null.
*/
@SafeVarargs
public static <T> Optional<T> getNonNull(@Nullable final T... objects) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would have the bigger impact imho @currenjin

return Optional.ofNullable(objects)
.map(arr -> Arrays.stream(arr).filter(Objects::nonNull).findFirst())
.orElse(Optional.empty());
}

/**
* Returns the object if it is not {@code null}; otherwise, returns the default value.
*
* @param object the object to test, may be {@code null}
* @param defaultValue the default value to return if the object is {@code null}, may be {@code null}
* @param <T> the type of the object
* @return An Optional containing either the object if not null, or the default value wrapped in an Optional.
*/
public static <T> Optional<T> getIfNull(@Nullable final T object, @Nullable final T defaultValue) {
return Optional.ofNullable(object).or(() -> Optional.ofNullable(defaultValue));
}

/**
* Returns the collection if it is not {@code null} and not empty; otherwise, returns the default value.
*
* @param object the collection to test, may be {@code null}
* @param defaultValue the default value to return if the collection is {@code null} or empty, may be {@code null}
* @param <T> the type of elements in the collection
* @return An Optional containing the collection if not null and not empty, otherwise an Optional of the default
* value.
*/
public static <T> Optional<Collection<T>> getIfEmpty(@Nullable final Collection<T> object,
@Nullable final Collection<T> defaultValue) {
return Optional.ofNullable(object)
.filter(collection -> !collection.isEmpty())
.or(() -> Optional.ofNullable(defaultValue));
}

/**
* Determine whether the given object is empty.
* <p>This method supports the following object types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Currency;
import java.util.Date;
Expand All @@ -52,6 +53,9 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.springframework.util.ObjectUtils.getIfEmpty;
import static org.springframework.util.ObjectUtils.getIfNull;
import static org.springframework.util.ObjectUtils.getNonNull;
import static org.springframework.util.ObjectUtils.isEmpty;

/**
Expand Down Expand Up @@ -111,6 +115,55 @@ void isCompatibleWithThrowsClause() {
assertThat(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), throwable)).isTrue();
}

@Test
void getIfNullObject() {
String value = UUID.randomUUID().toString();
String backup = UUID.randomUUID().toString();

assertThat(getIfNull(value, backup)).contains(value);
assertThat(getIfNull(value, backup)).contains(value);
assertThat(getIfNull(value, value)).contains(value);
assertThat(getIfNull(backup, backup)).contains(backup);
assertThat(getIfNull(null, value)).contains(value);
assertThat(getIfNull(null, backup)).contains(backup);
assertThat(getIfNull("null", backup)).contains("null");
assertThat(getIfNull(null, null)).isEmpty();
}

@Test
void getIfEmptyObject() {
Collection<String> empty = Collections.emptyList();
Collection<String> value = List.of(UUID.randomUUID().toString());
Collection<String> backup = List.of(UUID.randomUUID().toString());

assertThat(getIfEmpty(value, backup)).contains(value);
assertThat(getIfEmpty(null, backup)).contains(backup);
assertThat(getIfEmpty(empty, backup)).contains(backup);
assertThat(getIfEmpty(value, empty)).contains(value);
assertThat(getIfEmpty(empty, empty)).contains(empty);
assertThat(getIfEmpty(backup, backup)).contains(backup);
assertThat(getIfEmpty(null, empty)).contains(empty);
assertThat(getIfEmpty(null, backup)).contains(backup);
assertThat(getIfEmpty(null, null)).isEmpty();
}

@Test
void getNonNullObject() {
String value = UUID.randomUUID().toString();
String backup = UUID.randomUUID().toString();

assertThat(getNonNull(backup, value, value)).contains(backup);
assertThat(getNonNull(value, null, backup)).contains(value);
assertThat(getNonNull(backup, value, null)).contains(backup);
assertThat(getNonNull(value, backup)).contains(value);
assertThat(getNonNull(null, null, value)).contains(value);

String _null = null;
assertThat(getNonNull(null, null, _null)).isEmpty();
assertThat(getNonNull(null, "null", backup)).contains("null");
assertThat(getNonNull(null, null)).isEmpty();
}

@Test
void isEmptyNull() {
assertThat(isEmpty(null)).isTrue();
Expand Down