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 d1c0c46a70..bab4651257 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -45,6 +45,7 @@ import org.springframework.lang.Contract; * @author Juergen Hoeller * @author Rob Harrop * @author Arjen Poutsma + * @author Sam Brannen * @since 1.1.3 */ public abstract class CollectionUtils { @@ -195,13 +196,15 @@ public abstract class CollectionUtils { /** - * Check whether the given Iterator contains the given element. - * @param iterator the Iterator to check + * Check whether the given {@link Iterator} contains the given element. + * @param iterator the {@code Iterator} to check * @param element the element to look for * @return {@code true} if found, {@code false} otherwise */ @Contract("null, _ -> false") - public static boolean contains(@Nullable Iterator> iterator, Object element) { + public static boolean contains(@Nullable Iterator extends @Nullable Object> iterator, + @Nullable Object element) { + if (iterator != null) { while (iterator.hasNext()) { Object candidate = iterator.next(); @@ -214,13 +217,15 @@ public abstract class CollectionUtils { } /** - * Check whether the given Enumeration contains the given element. - * @param enumeration the Enumeration to check + * Check whether the given {@link Enumeration} contains the given element. + * @param enumeration the {@code Enumeration} to check * @param element the element to look for * @return {@code true} if found, {@code false} otherwise */ @Contract("null, _ -> false") - public static boolean contains(@Nullable Enumeration> enumeration, Object element) { + public static boolean contains(@Nullable Enumeration extends @Nullable Object> enumeration, + @Nullable Object element) { + if (enumeration != null) { while (enumeration.hasMoreElements()) { Object candidate = enumeration.nextElement(); @@ -233,15 +238,17 @@ public abstract class CollectionUtils { } /** - * Check whether the given Collection contains the given element instance. + * Check whether the given {@link Collection} contains the given element instance. *
Enforces the given instance to be present, rather than returning
* {@code true} for an equal element as well.
- * @param collection the Collection to check
+ * @param collection the {@code Collection} to check
* @param element the element to look for
* @return {@code true} if found, {@code false} otherwise
*/
@Contract("null, _ -> false")
- public static boolean containsInstance(@Nullable Collection> collection, Object element) {
+ public static boolean containsInstance(@Nullable Collection extends @Nullable Object> collection,
+ @Nullable Object element) {
+
if (collection != null) {
for (Object candidate : collection) {
if (candidate == element) {
@@ -255,12 +262,22 @@ public abstract class CollectionUtils {
/**
* Return {@code true} if any element in '{@code candidates}' is
* contained in '{@code source}'; otherwise returns {@code false}.
- * @param source the source Collection
+ * @param source the source {@link Collection}
* @param candidates the candidates to search for
* @return whether any of the candidates has been found
*/
- public static boolean containsAny(Collection> source, Collection> candidates) {
- return findFirstMatch(source, candidates) != null;
+ public static boolean containsAny(Collection extends @Nullable Object> source,
+ Collection extends @Nullable Object> candidates) {
+
+ if (isEmpty(source) || isEmpty(candidates)) {
+ return false;
+ }
+ for (Object candidate : candidates) {
+ if (source.contains(candidate)) {
+ return true;
+ }
+ }
+ return false;
}
/**
diff --git a/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java b/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java
index a1ad7390db..0322b34d09 100644
--- a/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java
+++ b/spring-core/src/test/java/org/springframework/util/CollectionUtilsTests.java
@@ -18,10 +18,10 @@ package org.springframework.util;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -42,6 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Rob Harrop
* @author Juergen Hoeller
* @author Rick Evans
+ * @author Sam Brannen
*/
class CollectionUtilsTests {
@@ -100,19 +101,24 @@ class CollectionUtilsTests {
}
@Test
- void contains() {
+ void containsWithIterator() {
assertThat(CollectionUtils.contains((Iterator