Browse Source

StringUtils.toStringArray/CollectionUtils.toIterator handle null input

Closes gh-22547
pull/22633/head
Juergen Hoeller 7 years ago
parent
commit
587067a7ca
  1. 14
      spring-core/src/main/java/org/springframework/util/CollectionUtils.java
  2. 50
      spring-core/src/main/java/org/springframework/util/StringUtils.java

14
spring-core/src/main/java/org/springframework/util/CollectionUtils.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -369,12 +369,12 @@ public abstract class CollectionUtils { @@ -369,12 +369,12 @@ public abstract class CollectionUtils {
}
/**
* Adapt an enumeration to an iterator.
* @param enumeration the enumeration
* @return the iterator
* Adapt an {@link Enumeration} to an {@link Iterator}.
* @param enumeration the original {@code Enumeration}
* @return the adapted {@code Iterator}
*/
public static <E> Iterator<E> toIterator(Enumeration<E> enumeration) {
return new EnumerationIterator<>(enumeration);
public static <E> Iterator<E> toIterator(@Nullable Enumeration<E> enumeration) {
return (enumeration != null ? new EnumerationIterator<>(enumeration) : Collections.emptyIterator());
}
/**
@ -557,7 +557,7 @@ public abstract class CollectionUtils { @@ -557,7 +557,7 @@ public abstract class CollectionUtils {
if (this == other) {
return true;
}
return map.equals(other);
return this.map.equals(other);
}
@Override

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -871,6 +871,28 @@ public abstract class StringUtils { @@ -871,6 +871,28 @@ public abstract class StringUtils {
// Convenience methods for working with String arrays
//---------------------------------------------------------------------
/**
* Copy the given {@link Collection} into a {@code String} array.
* <p>The {@code Collection} must contain {@code String} elements only.
* @param collection the {@code Collection} to copy
* (potentially {@code null} or empty)
* @return the resulting {@code String} array
*/
public static String[] toStringArray(@Nullable Collection<String> collection) {
return (collection != null ? collection.toArray(new String[0]) : new String[0]);
}
/**
* Copy the given {@link Enumeration} into a {@code String} array.
* <p>The {@code Enumeration} must contain {@code String} elements only.
* @param enumeration the {@code Enumeration} to copy
* (potentially {@code null} or empty)
* @return the resulting {@code String} array
*/
public static String[] toStringArray(@Nullable Enumeration<String> enumeration) {
return (enumeration != null ? toStringArray(Collections.list(enumeration)) : new String[0]);
}
/**
* Append the given {@code String} to the given {@code String} array,
* returning a new array consisting of the input array contents plus
@ -946,9 +968,9 @@ public abstract class StringUtils { @@ -946,9 +968,9 @@ public abstract class StringUtils {
}
/**
* Turn given source {@code String} array into sorted array.
* @param array the source array
* @return the sorted array (never {@code null})
* Sort the given {@code String} array if necessary.
* @param array the original array (potentially empty)
* @return the array in sorted form (never {@code null})
*/
public static String[] sortStringArray(String[] array) {
if (ObjectUtils.isEmpty(array)) {
@ -959,26 +981,6 @@ public abstract class StringUtils { @@ -959,26 +981,6 @@ public abstract class StringUtils {
return array;
}
/**
* Copy the given {@code Collection} into a {@code String} array.
* <p>The {@code Collection} must contain {@code String} elements only.
* @param collection the {@code Collection} to copy
* @return the {@code String} array
*/
public static String[] toStringArray(Collection<String> collection) {
return collection.toArray(new String[0]);
}
/**
* Copy the given Enumeration into a {@code String} array.
* The Enumeration must contain {@code String} elements only.
* @param enumeration the Enumeration to copy
* @return the {@code String} array
*/
public static String[] toStringArray(Enumeration<String> enumeration) {
return toStringArray(Collections.list(enumeration));
}
/**
* Trim the elements of the given {@code String} array,
* calling {@code String.trim()} on each of them.

Loading…
Cancel
Save