From 603001480377a440a3ee4cb605c5e5d8b99c3545 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 8 Mar 2019 23:04:42 +0100 Subject: [PATCH] CollectionUtils.toIterator tolerates null Enumeration as input See gh-22547 --- .../springframework/util/CollectionUtils.java | 21 +++---- .../org/springframework/util/StringUtils.java | 58 +++++++++---------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index 8edb77e9d5..f8ad181483 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 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. @@ -125,7 +125,7 @@ public abstract class CollectionUtils { * Check whether the given Iterator contains the given element. * @param iterator the Iterator to check * @param element the element to look for - * @return {@code true} if found, {@code false} else + * @return {@code true} if found, {@code false} otherwise */ public static boolean contains(Iterator iterator, Object element) { if (iterator != null) { @@ -143,7 +143,7 @@ public abstract class CollectionUtils { * Check whether the given Enumeration contains the given element. * @param enumeration the Enumeration to check * @param element the element to look for - * @return {@code true} if found, {@code false} else + * @return {@code true} if found, {@code false} otherwise */ public static boolean contains(Enumeration enumeration, Object element) { if (enumeration != null) { @@ -163,7 +163,7 @@ public abstract class CollectionUtils { * {@code true} for an equal element as well. * @param collection the Collection to check * @param element the element to look for - * @return {@code true} if found, {@code false} else + * @return {@code true} if found, {@code false} otherwise */ public static boolean containsInstance(Collection collection, Object element) { if (collection != null) { @@ -268,7 +268,7 @@ public abstract class CollectionUtils { * Determine whether the given Collection only contains a single unique object. * @param collection the Collection to check * @return {@code true} if the collection contains a single reference or - * multiple references to the same instance, {@code false} else + * multiple references to the same instance, {@code false} otherwise */ public static boolean hasUniqueObject(Collection collection) { if (isEmpty(collection)) { @@ -326,12 +326,13 @@ 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} */ + @SuppressWarnings("unchecked") public static Iterator toIterator(Enumeration enumeration) { - return new EnumerationIterator(enumeration); + return (enumeration != null ? new EnumerationIterator(enumeration) : Collections.EMPTY_SET.iterator()); } /** @@ -508,7 +509,7 @@ public abstract class CollectionUtils { if (this == other) { return true; } - return map.equals(other); + return this.map.equals(other); } @Override diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 50827aa08b..2ecab2dc6d 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -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. @@ -765,6 +765,32 @@ public abstract class StringUtils { // Convenience methods for working with String arrays //--------------------------------------------------------------------- + /** + * Copy the given {@link Collection} into a {@code String} array. + *

The {@code Collection} must contain {@code String} elements only. + * @param collection the {@code Collection} to copy + * @return the resulting {@code String} array + */ + public static String[] toStringArray(Collection collection) { + if (collection == null) { + return null; + } + return collection.toArray(new String[collection.size()]); + } + + /** + * Copy the given {@link Enumeration} into a {@code String} array. + *

The {@code Enumeration} must contain {@code String} elements only. + * @param enumeration the {@code Enumeration} to copy + * @return the resulting {@code String} array + */ + public static String[] toStringArray(Enumeration enumeration) { + if (enumeration == null) { + return null; + } + return toStringArray(Collections.list(enumeration)); + } + /** * Append the given {@code String} to the given {@code String} array, * returning a new array consisting of the input array contents plus @@ -838,8 +864,8 @@ public abstract class StringUtils { } /** - * Turn given source {@code String} array into sorted array. - * @param array the source array + * Sort the given {@code String} array if necessary. + * @param array the original array * @return the sorted array (never {@code null}) */ public static String[] sortStringArray(String[] array) { @@ -851,32 +877,6 @@ public abstract class StringUtils { return array; } - /** - * Copy the given {@code Collection} into a {@code String} array. - *

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 collection) { - if (collection == null) { - return null; - } - return collection.toArray(new String[collection.size()]); - } - - /** - * 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 enumeration) { - if (enumeration == null) { - return null; - } - return toStringArray(Collections.list(enumeration)); - } - /** * Trim the elements of the given {@code String} array, * calling {@code String.trim()} on each of them.