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 1 commit
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,48 @@ public static boolean isEmpty(@Nullable Object @Nullable [] array) {
return (array == null || array.length == 0);
}

/**
* Returns the object if it is not {@code null}; otherwise, returns the default value.
*
* <pre>
* ObjectUtils.getIfNull(null, null) = null
* ObjectUtils.getIfNull(null, "") = ""
* ObjectUtils.getIfNull(null, "zz") = "zz"
* ObjectUtils.getIfNull("abc", *) = "abc"
* ObjectUtils.getIfNull(Boolean.TRUE, *) = Boolean.TRUE
* </pre>
*
* @param <T> the type of the object
* @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}
* @return {@code object} if it is not {@code null}; otherwise, {@code defaultValue}
*/
@Nullable
public static <T> T getIfNull(@Nullable final T object, @Nullable final T defaultValue) {
return Objects.nonNull(object) ? object : defaultValue;
}

/**
* Returns the collection if it is not {@code null} and not empty; otherwise, returns the default value.
*
* <pre>
* ObjectUtils.getIfEmpty(List.of("a"), List.of("b")) = List.of("a")
* ObjectUtils.getIfEmpty(null, List.of("b")) = List.of("b")
* ObjectUtils.getIfEmpty(Collections.emptyList(), List.of("b")) = List.of("b")
* ObjectUtils.getIfEmpty(null, null) = null
* </pre>
*
* @param <T> the type of elements in the collection
* @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}
* @return {@code object} if it is not {@code null} and not empty; otherwise, {@code defaultValue}
*/
@Nullable
public static <T> Collection<T> getIfEmpty(@Nullable final Collection<T> object,
@Nullable final Collection<T> defaultValue) {
return Objects.nonNull(object) && !object.isEmpty() ? object : 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,8 @@
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.isEmpty;

/**
Expand Down Expand Up @@ -111,6 +114,35 @@ 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)).isEqualTo(value);
assertThat(getIfNull(value, value)).isEqualTo(value);
assertThat(getIfNull(backup, backup)).isEqualTo(backup);
assertThat(getIfNull(null, value)).isEqualTo(value);
assertThat(getIfNull(null, backup)).isEqualTo(backup);
assertThat(getIfNull("null", backup)).isEqualTo("null");
assertThat(Optional.ofNullable(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)).isEqualTo(value);
assertThat(getIfEmpty(null, backup)).isEqualTo(backup);
assertThat(getIfEmpty(empty, backup)).isEqualTo(backup);
assertThat(getIfEmpty(value, empty)).isEqualTo(value);
assertThat(getIfEmpty(empty, empty)).isEqualTo(empty);
assertThat(getIfEmpty(backup, backup)).isEqualTo(backup);
assertThat(getIfEmpty(null, empty)).isEqualTo(empty);
assertThat(getIfEmpty(null, backup)).isEqualTo(backup);
assertThat(Optional.ofNullable(getIfEmpty(null, null))).isEmpty();
}

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