提交 60300148 编写于 作者: J Juergen Hoeller

CollectionUtils.toIterator tolerates null Enumeration as input

See gh-22547
上级 a94c12ff
/*
* 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 <E> Iterator<E> toIterator(Enumeration<E> enumeration) {
return new EnumerationIterator<E>(enumeration);
return (enumeration != null ? new EnumerationIterator<E>(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
......
/*
* 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.
* <p>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<String> collection) {
if (collection == null) {
return null;
}
return collection.toArray(new String[collection.size()]);
}
/**
* 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
* @return the resulting {@code String} array
*/
public static String[] toStringArray(Enumeration<String> 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.
* <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) {
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<String> 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.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册