Skip to content

Commit

Permalink
Support arrays, collections, & maps in ObjectUtils.nullSafeConciseToS…
Browse files Browse the repository at this point in the history
…tring()

Prior to this commit, there was no explicit support for arrays,
collections, and maps in nullSafeConciseToString(). This lead to string
representations such as the following, regardless of whether the array,
collection, or map was empty.

- char[]@1623b78d
- java.util.ImmutableCollections$List12@74fe5c40
- java.util.ImmutableCollections$MapN@10e31a9a

This commit introduces explicit support for arrays, collections, and
maps in nullSafeConciseToString(), which results in the following
empty/non-empty string representations.

- array: {} / {...}
- collection: [] / [...]
- map: {} / {...}

The reason a string representation of an array uses "{}" instead of
"[]" (like in Arrays.toString(...)) is that
ObjectUtils.nullSafeToString(<array>) already follows that convention,
and the implementation of nullSafeConciseToString() aligns with that
for the sake of consistency.

Closes gh-30810
  • Loading branch information
sbrannen committed Jul 5, 2023
1 parent ae13823 commit b76664e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
Expand Up @@ -69,6 +69,9 @@ public abstract class ObjectUtils {
private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;
private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
private static final String NON_EMPTY_ARRAY = ARRAY_START + "..." + ARRAY_END;
private static final String EMPTY_COLLECTION = "[]";
private static final String NON_EMPTY_COLLECTION = "[...]";


/**
Expand Down Expand Up @@ -943,6 +946,16 @@ public static String nullSafeConciseToString(@Nullable Object obj) {
return (optional.isEmpty() ? "Optional.empty" :
"Optional[%s]".formatted(nullSafeConciseToString(optional.get())));
}
if (obj.getClass().isArray()) {
return (Array.getLength(obj) == 0 ? EMPTY_ARRAY : NON_EMPTY_ARRAY);
}
if (obj instanceof Collection<?> collection) {
return (collection.isEmpty() ? EMPTY_COLLECTION : NON_EMPTY_COLLECTION);
}
if (obj instanceof Map<?, ?> map) {
// EMPTY_ARRAY and NON_EMPTY_ARRAY are also used for maps.
return (map.isEmpty() ? EMPTY_ARRAY : NON_EMPTY_ARRAY);
}
if (obj instanceof Class<?> clazz) {
return clazz.getName();
}
Expand Down
Expand Up @@ -30,7 +30,6 @@
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Currency;
import java.util.Date;
Expand Down Expand Up @@ -1034,19 +1033,47 @@ void nullSafeConciseToStringForZoneId() {
}

@Test
void nullSafeConciseToStringForArraysAndCollections() {
List<String> list = List.of("a", "b", "c");
assertThat(ObjectUtils.nullSafeConciseToString(new int[][] {{1, 2}, {3, 4}})).startsWith(prefix(int[][].class));
assertThat(ObjectUtils.nullSafeConciseToString(list.toArray())).startsWith(prefix(Object[].class));
assertThat(ObjectUtils.nullSafeConciseToString(list.toArray(String[]::new))).startsWith(prefix(String[].class));
assertThat(ObjectUtils.nullSafeConciseToString(new ArrayList<>(list))).startsWith(prefix(ArrayList.class));
assertThat(ObjectUtils.nullSafeConciseToString(new HashSet<>(list))).startsWith(prefix(HashSet.class));
void nullSafeConciseToStringForEmptyArrays() {
assertThat(ObjectUtils.nullSafeConciseToString(new char[] {})).isEqualTo("{}");
assertThat(ObjectUtils.nullSafeConciseToString(new int[][] {})).isEqualTo("{}");
assertThat(ObjectUtils.nullSafeConciseToString(new String[] {})).isEqualTo("{}");
assertThat(ObjectUtils.nullSafeConciseToString(new Integer[][] {})).isEqualTo("{}");
}

@Test
void nullSafeConciseToStringForMaps() {
void nullSafeConciseToStringForNonEmptyArrays() {
assertThat(ObjectUtils.nullSafeConciseToString(new char[] {'a'})).isEqualTo("{...}");
assertThat(ObjectUtils.nullSafeConciseToString(new int[][] {{1}, {2}})).isEqualTo("{...}");
assertThat(ObjectUtils.nullSafeConciseToString(new String[] {"enigma"})).isEqualTo("{...}");
assertThat(ObjectUtils.nullSafeConciseToString(new Integer[][] {{1}, {2}})).isEqualTo("{...}");
}

@Test
void nullSafeConciseToStringForEmptyCollections() {
List<String> list = List.of();
Set<Integer> set = Set.of();
assertThat(ObjectUtils.nullSafeConciseToString(list)).isEqualTo("[]");
assertThat(ObjectUtils.nullSafeConciseToString(set)).isEqualTo("[]");
}

@Test
void nullSafeConciseToStringForNonEmptyCollections() {
List<String> list = List.of("a", "b");
Set<Integer> set = Set.of(1);
assertThat(ObjectUtils.nullSafeConciseToString(list)).isEqualTo("[...]");
assertThat(ObjectUtils.nullSafeConciseToString(set)).isEqualTo("[...]");
}

@Test
void nullSafeConciseToStringForEmptyMaps() {
Map<String, String> map = new HashMap<String, String>();
assertThat(ObjectUtils.nullSafeConciseToString(map)).isEqualTo("{}");
}

@Test
void nullSafeConciseToStringForNonEmptyMaps() {
Map<String, Integer> map = Map.of("a", 1, "b", 2, "c", 3);
assertThat(ObjectUtils.nullSafeConciseToString(map)).startsWith(prefix(map.getClass()));
assertThat(ObjectUtils.nullSafeConciseToString(map)).isEqualTo("{...}");
}

@Test
Expand Down

0 comments on commit b76664e

Please sign in to comment.