-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add a CollectionUtils with a safe toString
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/org/opentripplanner/framework/collection/CollectionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(", ", "[", "]")); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/org/opentripplanner/framework/collection/CollectionUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |