@@ -983,12 +983,12 @@ public static String[] toStringArray(Enumeration<String> enumeration) {
983
983
/**
984
984
* Trim the elements of the given {@code String} array,
985
985
* 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)
987
987
* @return the resulting array (of the same size) with trimmed elements
988
988
*/
989
- public static String [] trimArrayElements (@ Nullable String [] array ) {
989
+ public static String [] trimArrayElements (String [] array ) {
990
990
if (ObjectUtils .isEmpty (array )) {
991
- return new String [ 0 ] ;
991
+ return array ;
992
992
}
993
993
994
994
String [] result = new String [array .length ];
@@ -1002,26 +1002,23 @@ public static String[] trimArrayElements(@Nullable String[] array) {
1002
1002
/**
1003
1003
* Remove duplicate strings from the given array.
1004
1004
* <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)
1006
1006
* @return an array without duplicates, in natural sort order
1007
1007
*/
1008
1008
public static String [] removeDuplicateStrings (String [] array ) {
1009
1009
if (ObjectUtils .isEmpty (array )) {
1010
1010
return array ;
1011
1011
}
1012
1012
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 ));
1017
1014
return toStringArray (set );
1018
1015
}
1019
1016
1020
1017
/**
1021
1018
* Split a {@code String} at the first occurrence of the delimiter.
1022
1019
* 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)
1025
1022
* @return a two element array with index 0 being before the delimiter, and
1026
1023
* index 1 being after the delimiter (neither element includes the delimiter);
1027
1024
* or {@code null} if the delimiter wasn't found in the given input {@code String}
@@ -1100,7 +1097,7 @@ public static Properties splitArrayElementsIntoProperties(
1100
1097
* delimiter characters. Each of those characters can be used to separate
1101
1098
* tokens. A delimiter is always a single character; for multi-character
1102
1099
* 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)
1104
1101
* @param delimiters the delimiter characters, assembled as a {@code String}
1105
1102
* (each of the characters is individually considered as a delimiter)
1106
1103
* @return an array of the tokens
@@ -1119,7 +1116,7 @@ public static String[] tokenizeToStringArray(@Nullable String str, String delimi
1119
1116
* delimiter characters. Each of those characters can be used to separate
1120
1117
* tokens. A delimiter is always a single character; for multi-character
1121
1118
* 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)
1123
1120
* @param delimiters the delimiter characters, assembled as a {@code String}
1124
1121
* (each of the characters is individually considered as a delimiter)
1125
1122
* @param trimTokens trim the tokens via {@link String#trim()}
@@ -1159,7 +1156,7 @@ public static String[] tokenizeToStringArray(
1159
1156
* but it will still be considered as a single delimiter string, rather
1160
1157
* than as bunch of potential delimiter characters, in contrast to
1161
1158
* {@link #tokenizeToStringArray}.
1162
- * @param str the input {@code String}
1159
+ * @param str the input {@code String} (potentially {@code null} or empty)
1163
1160
* @param delimiter the delimiter between elements (this is a single delimiter,
1164
1161
* rather than a bunch individual delimiter characters)
1165
1162
* @return an array of the tokens in the list
@@ -1176,7 +1173,7 @@ public static String[] delimitedListToStringArray(@Nullable String str, @Nullabl
1176
1173
* but it will still be considered as a single delimiter string, rather
1177
1174
* than as bunch of potential delimiter characters, in contrast to
1178
1175
* {@link #tokenizeToStringArray}.
1179
- * @param str the input {@code String}
1176
+ * @param str the input {@code String} (potentially {@code null} or empty)
1180
1177
* @param delimiter the delimiter between elements (this is a single delimiter,
1181
1178
* rather than a bunch individual delimiter characters)
1182
1179
* @param charsToDelete a set of characters to delete; useful for deleting unwanted
@@ -1218,7 +1215,7 @@ public static String[] delimitedListToStringArray(
1218
1215
/**
1219
1216
* Convert a comma delimited list (e.g., a row from a CSV file) into an
1220
1217
* array of strings.
1221
- * @param str the input {@code String}
1218
+ * @param str the input {@code String} (potentially {@code null} or empty)
1222
1219
* @return an array of strings, or the empty array in case of empty input
1223
1220
*/
1224
1221
public static String [] commaDelimitedListToStringArray (@ Nullable String str ) {
@@ -1229,23 +1226,19 @@ public static String[] commaDelimitedListToStringArray(@Nullable String str) {
1229
1226
* Convert a comma delimited list (e.g., a row from a CSV file) into a set.
1230
1227
* <p>Note that this will suppress duplicates, and as of 4.2, the elements in
1231
1228
* 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)
1233
1230
* @return a set of {@code String} entries in the list
1234
1231
* @see #removeDuplicateStrings(String[])
1235
1232
*/
1236
1233
public static Set <String > commaDelimitedListToSet (@ Nullable String str ) {
1237
- Set <String > set = new LinkedHashSet <>();
1238
1234
String [] tokens = commaDelimitedListToStringArray (str );
1239
- for (String token : tokens ) {
1240
- set .add (token );
1241
- }
1242
- return set ;
1235
+ return new LinkedHashSet <>(Arrays .asList (tokens ));
1243
1236
}
1244
1237
1245
1238
/**
1246
1239
* Convert a {@link Collection} to a delimited {@code String} (e.g. CSV).
1247
1240
* <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)
1249
1242
* @param delim the delimiter to use (typically a ",")
1250
1243
* @param prefix the {@code String} to start each element with
1251
1244
* @param suffix the {@code String} to end each element with
@@ -1272,7 +1265,7 @@ public static String collectionToDelimitedString(
1272
1265
/**
1273
1266
* Convert a {@code Collection} into a delimited {@code String} (e.g. CSV).
1274
1267
* <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)
1276
1269
* @param delim the delimiter to use (typically a ",")
1277
1270
* @return the delimited {@code String}
1278
1271
*/
@@ -1283,17 +1276,17 @@ public static String collectionToDelimitedString(@Nullable Collection<?> coll, S
1283
1276
/**
1284
1277
* Convert a {@code Collection} into a delimited {@code String} (e.g., CSV).
1285
1278
* <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)
1287
1280
* @return the delimited {@code String}
1288
1281
*/
1289
- public static String collectionToCommaDelimitedString (Collection <?> coll ) {
1282
+ public static String collectionToCommaDelimitedString (@ Nullable Collection <?> coll ) {
1290
1283
return collectionToDelimitedString (coll , "," );
1291
1284
}
1292
1285
1293
1286
/**
1294
1287
* Convert a {@code String} array into a delimited {@code String} (e.g. CSV).
1295
1288
* <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)
1297
1290
* @param delim the delimiter to use (typically a ",")
1298
1291
* @return the delimited {@code String}
1299
1292
*/
@@ -1319,7 +1312,7 @@ public static String arrayToDelimitedString(@Nullable Object[] arr, String delim
1319
1312
* Convert a {@code String} array into a comma delimited {@code String}
1320
1313
* (i.e., CSV).
1321
1314
* <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)
1323
1316
* @return the delimited {@code String}
1324
1317
*/
1325
1318
public static String arrayToCommaDelimitedString (@ Nullable Object [] arr ) {
0 commit comments