Skip to content

Commit cb31d6f

Browse files
committed
ListUtils: add methods to convert list to string
Of course, you can call toString() on most List implementations and get a similar result, but these methods give the caller more control, and ensure consistent behavior across different List implementations.
1 parent cca83dc commit cb31d6f

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/main/java/org/scijava/util/ListUtils.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,69 @@ public static <T> T first(final List<T> list) {
5353
return list.get(0);
5454
}
5555

56+
/**
57+
* Converts the given list to a string.
58+
* <p>
59+
* The list elements will be separated by a comma then a space. The list will
60+
* be enclosed in square brackets.
61+
* </p>
62+
*
63+
* @param list The list to stringify.
64+
* @see #string(List, String, String, String, boolean)
65+
*/
66+
public static String string(final List<?> list) {
67+
return string(list, true);
68+
}
69+
70+
/**
71+
* Converts the given list to a string.
72+
* <p>
73+
* The list elements will be separated by a comma then a space. The list will
74+
* be enclosed in square brackets unless it is a singleton with the
75+
* {@code encloseSingletons} flag set to false.
76+
* </p>
77+
*
78+
* @param list The list to stringify.
79+
* @param encloseSingletons Whether to enclose singleton lists in brackets.
80+
* @return The stringified list.
81+
* @see #string(List, String, String, String, boolean)
82+
*/
83+
public static String string(final List<?> list,
84+
final boolean encloseSingletons)
85+
{
86+
return string(list, "[", "]", ", ", encloseSingletons);
87+
}
88+
89+
/**
90+
* Converts the given list to a string.
91+
* <p>
92+
* The list elements will be comma-separated. It will be enclosed in square
93+
* brackets unless the list is a singleton with the {@code encloseSingletons}
94+
* flag set to false.
95+
* </p>
96+
*
97+
* @param list The list to stringify.
98+
* @param lDelimiter The left-hand symbol(s) in which to enclose the list.
99+
* @param rDelimiter The right-hand symbol(s) in which to enclose the list.
100+
* @param separator The symbol(s) to place in between each element.
101+
* @param encloseSingletons Whether to enclose singleton lists inside the
102+
* delimiter symbols.
103+
* @return The stringified list.
104+
*/
105+
public static String string(final List<?> list, //
106+
final String lDelimiter, final String rDelimiter, //
107+
final String separator, final boolean encloseSingletons)
108+
{
109+
final boolean delimit = encloseSingletons || list.size() != 1;
110+
final StringBuilder sb = new StringBuilder();
111+
if (delimit) sb.append(lDelimiter);
112+
boolean first = true;
113+
for (final Object e : list) {
114+
if (first) first = false;
115+
else sb.append(separator);
116+
sb.append(e);
117+
}
118+
if (delimit) sb.append(rDelimiter);
119+
return sb.toString();
120+
}
56121
}

0 commit comments

Comments
 (0)