Skip to content

Commit

Permalink
refactor: Add a CollectionUtils with a safe toString
Browse files Browse the repository at this point in the history
  • Loading branch information
t2gran committed Nov 13, 2023
1 parent 0a3370a commit b56b8e1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.opentripplanner.framework.collection;

import java.util.Collection;
import java.util.Set;
import java.util.SortedSet;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

public class CollectionUtils {

/**
* A null-safe version of toString() for a collections.
* <p>
* If the collection is {@code null} the given {@code nullText} is returned.
* <p>
* All elements are also converted to a sting using the {@code toString()} method or if
* {@code null} to the given {@code nullText}.
* <p>
* If the collection is a set, but not a SortedSet then the elements are sorted. This is done
* to return the elements in a deterministic manner, witch is important if this is used in
* for example a unit-test.
* <p>
* The final result string examples: {@code "[]", "[a]", "[a, b, c]"}
*/
public static <T> String toString(@Nullable Collection<T> c, String nullText) {
if (c == null) {
return nullText;
}
var stream = c.stream().map(it -> it == null ? nullText : it.toString());
if (c instanceof Set && !(c instanceof SortedSet<T>)) {
stream = stream.sorted();
}
return stream.collect(Collectors.joining(", ", "[", "]"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.opentripplanner.framework.collection;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.google.type.Month;
import java.time.Duration;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.Test;

class CollectionUtilsTest {

public static final String NULL_STRING = "<null>";

@Test
void testToString() {
assertEquals("<null>", CollectionUtils.toString(null, NULL_STRING));
assertEquals("[]", CollectionUtils.toString(Set.of(), NULL_STRING));
assertEquals("[]", CollectionUtils.toString(List.of(), NULL_STRING));
assertEquals("[JUNE]", CollectionUtils.toString(EnumSet.of(Month.JUNE), NULL_STRING));
assertEquals(
"[APRIL, JANUARY, JUNE]",
CollectionUtils.toString(EnumSet.of(Month.JANUARY, Month.JUNE, Month.APRIL), "#")
);

// Given a list of objects
List<Object> list = new ArrayList<>();
list.add(Duration.ofHours(3));
list.add(null);
list.add(Month.JUNE);
list.add(Month.APRIL);

// Then: keep list order, do not sort
assertEquals("[PT3H, <null>, JUNE, APRIL]", CollectionUtils.toString(list, NULL_STRING));

// And: Set should be sorted alphabetically
Set<Object> set = new HashSet<>(list);
assertEquals("[<null>, APRIL, JUNE, PT3H]", CollectionUtils.toString(set, NULL_STRING));
}
}

0 comments on commit b56b8e1

Please sign in to comment.