Consistent nullability for array/collection input parameters

Includes pre-sizing of LinkedHashSet for conversion from array.

Issue: SPR-17123
Issue: SPR-17074
This commit is contained in:
Juergen Hoeller 2018-08-06 14:11:59 +02:00
parent a9a38fe67e
commit 69c6a40c50
1 changed files with 20 additions and 27 deletions

View File

@ -983,12 +983,12 @@ public abstract class StringUtils {
/**
* Trim the elements of the given {@code String} array,
* calling {@code String.trim()} on each of them.
* @param array the original {@code String} array
* @param array the original {@code String} array (potentially {@code null} or empty)
* @return the resulting array (of the same size) with trimmed elements
*/
public static String[] trimArrayElements(@Nullable String[] array) {
public static String[] trimArrayElements(String[] array) {
if (ObjectUtils.isEmpty(array)) {
return new String[0];
return array;
}
String[] result = new String[array.length];
@ -1002,7 +1002,7 @@ public abstract class StringUtils {
/**
* Remove duplicate strings from the given array.
* <p>As of 4.2, it preserves the original order, as it uses a {@link LinkedHashSet}.
* @param array the {@code String} array
* @param array the {@code String} array (potentially empty)
* @return an array without duplicates, in natural sort order
*/
public static String[] removeDuplicateStrings(String[] array) {
@ -1010,18 +1010,15 @@ public abstract class StringUtils {
return array;
}
Set<String> set = new LinkedHashSet<>();
for (String element : array) {
set.add(element);
}
Set<String> set = new LinkedHashSet<>(Arrays.asList(array));
return toStringArray(set);
}
/**
* Split a {@code String} at the first occurrence of the delimiter.
* Does not include the delimiter in the result.
* @param toSplit the string to split
* @param delimiter to split the string up with
* @param toSplit the string to split (potentially {@code null} or empty)
* @param delimiter to split the string up with (potentially {@code null} or empty)
* @return a two element array with index 0 being before the delimiter, and
* index 1 being after the delimiter (neither element includes the delimiter);
* or {@code null} if the delimiter wasn't found in the given input {@code String}
@ -1100,7 +1097,7 @@ public abstract class StringUtils {
* delimiter characters. Each of those characters can be used to separate
* tokens. A delimiter is always a single character; for multi-character
* delimiters, consider using {@link #delimitedListToStringArray}.
* @param str the {@code String} to tokenize
* @param str the {@code String} to tokenize (potentially {@code null} or empty)
* @param delimiters the delimiter characters, assembled as a {@code String}
* (each of the characters is individually considered as a delimiter)
* @return an array of the tokens
@ -1119,7 +1116,7 @@ public abstract class StringUtils {
* delimiter characters. Each of those characters can be used to separate
* tokens. A delimiter is always a single character; for multi-character
* delimiters, consider using {@link #delimitedListToStringArray}.
* @param str the {@code String} to tokenize
* @param str the {@code String} to tokenize (potentially {@code null} or empty)
* @param delimiters the delimiter characters, assembled as a {@code String}
* (each of the characters is individually considered as a delimiter)
* @param trimTokens trim the tokens via {@link String#trim()}
@ -1159,7 +1156,7 @@ public abstract class StringUtils {
* but it will still be considered as a single delimiter string, rather
* than as bunch of potential delimiter characters, in contrast to
* {@link #tokenizeToStringArray}.
* @param str the input {@code String}
* @param str the input {@code String} (potentially {@code null} or empty)
* @param delimiter the delimiter between elements (this is a single delimiter,
* rather than a bunch individual delimiter characters)
* @return an array of the tokens in the list
@ -1176,7 +1173,7 @@ public abstract class StringUtils {
* but it will still be considered as a single delimiter string, rather
* than as bunch of potential delimiter characters, in contrast to
* {@link #tokenizeToStringArray}.
* @param str the input {@code String}
* @param str the input {@code String} (potentially {@code null} or empty)
* @param delimiter the delimiter between elements (this is a single delimiter,
* rather than a bunch individual delimiter characters)
* @param charsToDelete a set of characters to delete; useful for deleting unwanted
@ -1218,7 +1215,7 @@ public abstract class StringUtils {
/**
* Convert a comma delimited list (e.g., a row from a CSV file) into an
* array of strings.
* @param str the input {@code String}
* @param str the input {@code String} (potentially {@code null} or empty)
* @return an array of strings, or the empty array in case of empty input
*/
public static String[] commaDelimitedListToStringArray(@Nullable String str) {
@ -1229,23 +1226,19 @@ public abstract class StringUtils {
* Convert a comma delimited list (e.g., a row from a CSV file) into a set.
* <p>Note that this will suppress duplicates, and as of 4.2, the elements in
* the returned set will preserve the original order in a {@link LinkedHashSet}.
* @param str the input {@code String}
* @param str the input {@code String} (potentially {@code null} or empty)
* @return a set of {@code String} entries in the list
* @see #removeDuplicateStrings(String[])
*/
public static Set<String> commaDelimitedListToSet(@Nullable String str) {
Set<String> set = new LinkedHashSet<>();
String[] tokens = commaDelimitedListToStringArray(str);
for (String token : tokens) {
set.add(token);
}
return set;
return new LinkedHashSet<>(Arrays.asList(tokens));
}
/**
* Convert a {@link Collection} to a delimited {@code String} (e.g. CSV).
* <p>Useful for {@code toString()} implementations.
* @param coll the {@code Collection} to convert
* @param coll the {@code Collection} to convert (potentially {@code null} or empty)
* @param delim the delimiter to use (typically a ",")
* @param prefix the {@code String} to start each element with
* @param suffix the {@code String} to end each element with
@ -1272,7 +1265,7 @@ public abstract class StringUtils {
/**
* Convert a {@code Collection} into a delimited {@code String} (e.g. CSV).
* <p>Useful for {@code toString()} implementations.
* @param coll the {@code Collection} to convert
* @param coll the {@code Collection} to convert (potentially {@code null} or empty)
* @param delim the delimiter to use (typically a ",")
* @return the delimited {@code String}
*/
@ -1283,17 +1276,17 @@ public abstract class StringUtils {
/**
* Convert a {@code Collection} into a delimited {@code String} (e.g., CSV).
* <p>Useful for {@code toString()} implementations.
* @param coll the {@code Collection} to convert
* @param coll the {@code Collection} to convert (potentially {@code null} or empty)
* @return the delimited {@code String}
*/
public static String collectionToCommaDelimitedString(Collection<?> coll) {
public static String collectionToCommaDelimitedString(@Nullable Collection<?> coll) {
return collectionToDelimitedString(coll, ",");
}
/**
* Convert a {@code String} array into a delimited {@code String} (e.g. CSV).
* <p>Useful for {@code toString()} implementations.
* @param arr the array to display
* @param arr the array to display (potentially {@code null} or empty)
* @param delim the delimiter to use (typically a ",")
* @return the delimited {@code String}
*/
@ -1319,7 +1312,7 @@ public abstract class StringUtils {
* Convert a {@code String} array into a comma delimited {@code String}
* (i.e., CSV).
* <p>Useful for {@code toString()} implementations.
* @param arr the array to display
* @param arr the array to display (potentially {@code null} or empty)
* @return the delimited {@code String}
*/
public static String arrayToCommaDelimitedString(@Nullable Object[] arr) {