Skip to content

Commit 69c6a40

Browse files
committed
Consistent nullability for array/collection input parameters
Includes pre-sizing of LinkedHashSet for conversion from array. Issue: SPR-17123 Issue: SPR-17074
1 parent a9a38fe commit 69c6a40

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -983,12 +983,12 @@ public static String[] toStringArray(Enumeration<String> enumeration) {
983983
/**
984984
* Trim the elements of the given {@code String} array,
985985
* calling {@code String.trim()} on each of them.
986-
* @param array the original {@code String} array
986+
* @param array the original {@code String} array (potentially {@code null} or empty)
987987
* @return the resulting array (of the same size) with trimmed elements
988988
*/
989-
public static String[] trimArrayElements(@Nullable String[] array) {
989+
public static String[] trimArrayElements(String[] array) {
990990
if (ObjectUtils.isEmpty(array)) {
991-
return new String[0];
991+
return array;
992992
}
993993

994994
String[] result = new String[array.length];
@@ -1002,26 +1002,23 @@ public static String[] trimArrayElements(@Nullable String[] array) {
10021002
/**
10031003
* Remove duplicate strings from the given array.
10041004
* <p>As of 4.2, it preserves the original order, as it uses a {@link LinkedHashSet}.
1005-
* @param array the {@code String} array
1005+
* @param array the {@code String} array (potentially empty)
10061006
* @return an array without duplicates, in natural sort order
10071007
*/
10081008
public static String[] removeDuplicateStrings(String[] array) {
10091009
if (ObjectUtils.isEmpty(array)) {
10101010
return array;
10111011
}
10121012

1013-
Set<String> set = new LinkedHashSet<>();
1014-
for (String element : array) {
1015-
set.add(element);
1016-
}
1013+
Set<String> set = new LinkedHashSet<>(Arrays.asList(array));
10171014
return toStringArray(set);
10181015
}
10191016

10201017
/**
10211018
* Split a {@code String} at the first occurrence of the delimiter.
10221019
* Does not include the delimiter in the result.
1023-
* @param toSplit the string to split
1024-
* @param delimiter to split the string up with
1020+
* @param toSplit the string to split (potentially {@code null} or empty)
1021+
* @param delimiter to split the string up with (potentially {@code null} or empty)
10251022
* @return a two element array with index 0 being before the delimiter, and
10261023
* index 1 being after the delimiter (neither element includes the delimiter);
10271024
* or {@code null} if the delimiter wasn't found in the given input {@code String}
@@ -1100,7 +1097,7 @@ public static Properties splitArrayElementsIntoProperties(
11001097
* delimiter characters. Each of those characters can be used to separate
11011098
* tokens. A delimiter is always a single character; for multi-character
11021099
* delimiters, consider using {@link #delimitedListToStringArray}.
1103-
* @param str the {@code String} to tokenize
1100+
* @param str the {@code String} to tokenize (potentially {@code null} or empty)
11041101
* @param delimiters the delimiter characters, assembled as a {@code String}
11051102
* (each of the characters is individually considered as a delimiter)
11061103
* @return an array of the tokens
@@ -1119,7 +1116,7 @@ public static String[] tokenizeToStringArray(@Nullable String str, String delimi
11191116
* delimiter characters. Each of those characters can be used to separate
11201117
* tokens. A delimiter is always a single character; for multi-character
11211118
* delimiters, consider using {@link #delimitedListToStringArray}.
1122-
* @param str the {@code String} to tokenize
1119+
* @param str the {@code String} to tokenize (potentially {@code null} or empty)
11231120
* @param delimiters the delimiter characters, assembled as a {@code String}
11241121
* (each of the characters is individually considered as a delimiter)
11251122
* @param trimTokens trim the tokens via {@link String#trim()}
@@ -1159,7 +1156,7 @@ public static String[] tokenizeToStringArray(
11591156
* but it will still be considered as a single delimiter string, rather
11601157
* than as bunch of potential delimiter characters, in contrast to
11611158
* {@link #tokenizeToStringArray}.
1162-
* @param str the input {@code String}
1159+
* @param str the input {@code String} (potentially {@code null} or empty)
11631160
* @param delimiter the delimiter between elements (this is a single delimiter,
11641161
* rather than a bunch individual delimiter characters)
11651162
* @return an array of the tokens in the list
@@ -1176,7 +1173,7 @@ public static String[] delimitedListToStringArray(@Nullable String str, @Nullabl
11761173
* but it will still be considered as a single delimiter string, rather
11771174
* than as bunch of potential delimiter characters, in contrast to
11781175
* {@link #tokenizeToStringArray}.
1179-
* @param str the input {@code String}
1176+
* @param str the input {@code String} (potentially {@code null} or empty)
11801177
* @param delimiter the delimiter between elements (this is a single delimiter,
11811178
* rather than a bunch individual delimiter characters)
11821179
* @param charsToDelete a set of characters to delete; useful for deleting unwanted
@@ -1218,7 +1215,7 @@ public static String[] delimitedListToStringArray(
12181215
/**
12191216
* Convert a comma delimited list (e.g., a row from a CSV file) into an
12201217
* array of strings.
1221-
* @param str the input {@code String}
1218+
* @param str the input {@code String} (potentially {@code null} or empty)
12221219
* @return an array of strings, or the empty array in case of empty input
12231220
*/
12241221
public static String[] commaDelimitedListToStringArray(@Nullable String str) {
@@ -1229,23 +1226,19 @@ public static String[] commaDelimitedListToStringArray(@Nullable String str) {
12291226
* Convert a comma delimited list (e.g., a row from a CSV file) into a set.
12301227
* <p>Note that this will suppress duplicates, and as of 4.2, the elements in
12311228
* the returned set will preserve the original order in a {@link LinkedHashSet}.
1232-
* @param str the input {@code String}
1229+
* @param str the input {@code String} (potentially {@code null} or empty)
12331230
* @return a set of {@code String} entries in the list
12341231
* @see #removeDuplicateStrings(String[])
12351232
*/
12361233
public static Set<String> commaDelimitedListToSet(@Nullable String str) {
1237-
Set<String> set = new LinkedHashSet<>();
12381234
String[] tokens = commaDelimitedListToStringArray(str);
1239-
for (String token : tokens) {
1240-
set.add(token);
1241-
}
1242-
return set;
1235+
return new LinkedHashSet<>(Arrays.asList(tokens));
12431236
}
12441237

12451238
/**
12461239
* Convert a {@link Collection} to a delimited {@code String} (e.g. CSV).
12471240
* <p>Useful for {@code toString()} implementations.
1248-
* @param coll the {@code Collection} to convert
1241+
* @param coll the {@code Collection} to convert (potentially {@code null} or empty)
12491242
* @param delim the delimiter to use (typically a ",")
12501243
* @param prefix the {@code String} to start each element with
12511244
* @param suffix the {@code String} to end each element with
@@ -1272,7 +1265,7 @@ public static String collectionToDelimitedString(
12721265
/**
12731266
* Convert a {@code Collection} into a delimited {@code String} (e.g. CSV).
12741267
* <p>Useful for {@code toString()} implementations.
1275-
* @param coll the {@code Collection} to convert
1268+
* @param coll the {@code Collection} to convert (potentially {@code null} or empty)
12761269
* @param delim the delimiter to use (typically a ",")
12771270
* @return the delimited {@code String}
12781271
*/
@@ -1283,17 +1276,17 @@ public static String collectionToDelimitedString(@Nullable Collection<?> coll, S
12831276
/**
12841277
* Convert a {@code Collection} into a delimited {@code String} (e.g., CSV).
12851278
* <p>Useful for {@code toString()} implementations.
1286-
* @param coll the {@code Collection} to convert
1279+
* @param coll the {@code Collection} to convert (potentially {@code null} or empty)
12871280
* @return the delimited {@code String}
12881281
*/
1289-
public static String collectionToCommaDelimitedString(Collection<?> coll) {
1282+
public static String collectionToCommaDelimitedString(@Nullable Collection<?> coll) {
12901283
return collectionToDelimitedString(coll, ",");
12911284
}
12921285

12931286
/**
12941287
* Convert a {@code String} array into a delimited {@code String} (e.g. CSV).
12951288
* <p>Useful for {@code toString()} implementations.
1296-
* @param arr the array to display
1289+
* @param arr the array to display (potentially {@code null} or empty)
12971290
* @param delim the delimiter to use (typically a ",")
12981291
* @return the delimited {@code String}
12991292
*/
@@ -1319,7 +1312,7 @@ public static String arrayToDelimitedString(@Nullable Object[] arr, String delim
13191312
* Convert a {@code String} array into a comma delimited {@code String}
13201313
* (i.e., CSV).
13211314
* <p>Useful for {@code toString()} implementations.
1322-
* @param arr the array to display
1315+
* @param arr the array to display (potentially {@code null} or empty)
13231316
* @return the delimited {@code String}
13241317
*/
13251318
public static String arrayToCommaDelimitedString(@Nullable Object[] arr) {

0 commit comments

Comments
 (0)