Skip to content

Commit 6030014

Browse files
committed
CollectionUtils.toIterator tolerates null Enumeration as input
See gh-22547
1 parent a94c12f commit 6030014

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -125,7 +125,7 @@ public static <K, V> void mergePropertiesIntoMap(Properties props, Map<K, V> map
125125
* Check whether the given Iterator contains the given element.
126126
* @param iterator the Iterator to check
127127
* @param element the element to look for
128-
* @return {@code true} if found, {@code false} else
128+
* @return {@code true} if found, {@code false} otherwise
129129
*/
130130
public static boolean contains(Iterator<?> iterator, Object element) {
131131
if (iterator != null) {
@@ -143,7 +143,7 @@ public static boolean contains(Iterator<?> iterator, Object element) {
143143
* Check whether the given Enumeration contains the given element.
144144
* @param enumeration the Enumeration to check
145145
* @param element the element to look for
146-
* @return {@code true} if found, {@code false} else
146+
* @return {@code true} if found, {@code false} otherwise
147147
*/
148148
public static boolean contains(Enumeration<?> enumeration, Object element) {
149149
if (enumeration != null) {
@@ -163,7 +163,7 @@ public static boolean contains(Enumeration<?> enumeration, Object element) {
163163
* {@code true} for an equal element as well.
164164
* @param collection the Collection to check
165165
* @param element the element to look for
166-
* @return {@code true} if found, {@code false} else
166+
* @return {@code true} if found, {@code false} otherwise
167167
*/
168168
public static boolean containsInstance(Collection<?> collection, Object element) {
169169
if (collection != null) {
@@ -268,7 +268,7 @@ public static Object findValueOfType(Collection<?> collection, Class<?>[] types)
268268
* Determine whether the given Collection only contains a single unique object.
269269
* @param collection the Collection to check
270270
* @return {@code true} if the collection contains a single reference or
271-
* multiple references to the same instance, {@code false} else
271+
* multiple references to the same instance, {@code false} otherwise
272272
*/
273273
public static boolean hasUniqueObject(Collection<?> collection) {
274274
if (isEmpty(collection)) {
@@ -326,12 +326,13 @@ public static <A, E extends A> A[] toArray(Enumeration<E> enumeration, A[] array
326326
}
327327

328328
/**
329-
* Adapt an enumeration to an iterator.
330-
* @param enumeration the enumeration
331-
* @return the iterator
329+
* Adapt an {@link Enumeration} to an {@link Iterator}.
330+
* @param enumeration the original {@code Enumeration}
331+
* @return the adapted {@code Iterator}
332332
*/
333+
@SuppressWarnings("unchecked")
333334
public static <E> Iterator<E> toIterator(Enumeration<E> enumeration) {
334-
return new EnumerationIterator<E>(enumeration);
335+
return (enumeration != null ? new EnumerationIterator<E>(enumeration) : Collections.EMPTY_SET.iterator());
335336
}
336337

337338
/**
@@ -508,7 +509,7 @@ public boolean equals(Object other) {
508509
if (this == other) {
509510
return true;
510511
}
511-
return map.equals(other);
512+
return this.map.equals(other);
512513
}
513514

514515
@Override

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -765,6 +765,32 @@ public static TimeZone parseTimeZoneString(String timeZoneString) {
765765
// Convenience methods for working with String arrays
766766
//---------------------------------------------------------------------
767767

768+
/**
769+
* Copy the given {@link Collection} into a {@code String} array.
770+
* <p>The {@code Collection} must contain {@code String} elements only.
771+
* @param collection the {@code Collection} to copy
772+
* @return the resulting {@code String} array
773+
*/
774+
public static String[] toStringArray(Collection<String> collection) {
775+
if (collection == null) {
776+
return null;
777+
}
778+
return collection.toArray(new String[collection.size()]);
779+
}
780+
781+
/**
782+
* Copy the given {@link Enumeration} into a {@code String} array.
783+
* <p>The {@code Enumeration} must contain {@code String} elements only.
784+
* @param enumeration the {@code Enumeration} to copy
785+
* @return the resulting {@code String} array
786+
*/
787+
public static String[] toStringArray(Enumeration<String> enumeration) {
788+
if (enumeration == null) {
789+
return null;
790+
}
791+
return toStringArray(Collections.list(enumeration));
792+
}
793+
768794
/**
769795
* Append the given {@code String} to the given {@code String} array,
770796
* returning a new array consisting of the input array contents plus
@@ -838,8 +864,8 @@ public static String[] mergeStringArrays(String[] array1, String[] array2) {
838864
}
839865

840866
/**
841-
* Turn given source {@code String} array into sorted array.
842-
* @param array the source array
867+
* Sort the given {@code String} array if necessary.
868+
* @param array the original array
843869
* @return the sorted array (never {@code null})
844870
*/
845871
public static String[] sortStringArray(String[] array) {
@@ -851,32 +877,6 @@ public static String[] sortStringArray(String[] array) {
851877
return array;
852878
}
853879

854-
/**
855-
* Copy the given {@code Collection} into a {@code String} array.
856-
* <p>The {@code Collection} must contain {@code String} elements only.
857-
* @param collection the {@code Collection} to copy
858-
* @return the {@code String} array
859-
*/
860-
public static String[] toStringArray(Collection<String> collection) {
861-
if (collection == null) {
862-
return null;
863-
}
864-
return collection.toArray(new String[collection.size()]);
865-
}
866-
867-
/**
868-
* Copy the given Enumeration into a {@code String} array.
869-
* The Enumeration must contain {@code String} elements only.
870-
* @param enumeration the Enumeration to copy
871-
* @return the {@code String} array
872-
*/
873-
public static String[] toStringArray(Enumeration<String> enumeration) {
874-
if (enumeration == null) {
875-
return null;
876-
}
877-
return toStringArray(Collections.list(enumeration));
878-
}
879-
880880
/**
881881
* Trim the elements of the given {@code String} array,
882882
* calling {@code String.trim()} on each of them.

0 commit comments

Comments
 (0)