From 9f0d58985f4588981eefaca0408e767eff1bef73 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Wed, 24 May 2017 08:20:09 +0000 Subject: [PATCH] COLLECTIONS-603: Small improvements for generics, conditional statements, and warnings suppressions Thanks to Artem Konovalov This closes #17 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1796010 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 3 +++ .../commons/collections4/CollectionUtils.java | 21 +++++++------------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 86e2ece3b..9b39615d2 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -21,6 +21,9 @@ + + Small improvements for generics, conditional statements, and warnings suppressions. + Web site spelling error: MultiValuedMapeList. diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index 334f20d6e..5bab383df 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -19,12 +19,12 @@ package org.apache.commons.collections4; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Map; @@ -180,8 +180,7 @@ public class CollectionUtils { * undesirable. This implementation only implements Collection. */ @SuppressWarnings("rawtypes") // we deliberately use the raw type here - public static final Collection EMPTY_COLLECTION = - UnmodifiableCollection.unmodifiableCollection(new ArrayList()); + public static final Collection EMPTY_COLLECTION = Collections.emptyList(); /** * CollectionUtils should not normally be instantiated. @@ -209,9 +208,8 @@ public class CollectionUtils { * @param collection the collection, possibly null * @return an empty collection if the argument is null */ - @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type public static Collection emptyIfNull(final Collection collection) { - return collection == null ? EMPTY_COLLECTION : collection; + return collection == null ? CollectionUtils.emptyCollection() : collection; } /** @@ -389,9 +387,7 @@ public class CollectionUtils { } } - if (foundCurrentElement) { - continue; - } else { + if (!foundCurrentElement) { return false; } } @@ -830,7 +826,7 @@ public class CollectionUtils { */ @Deprecated public static boolean exists(final Iterable input, final Predicate predicate) { - return predicate == null ? false : IterableUtils.matchesAny(input, predicate); + return predicate != null && IterableUtils.matchesAny(input, predicate); } /** @@ -850,7 +846,7 @@ public class CollectionUtils { */ @Deprecated public static boolean matchesAll(final Iterable input, final Predicate predicate) { - return predicate == null ? false : IterableUtils.matchesAll(input, predicate); + return predicate != null && IterableUtils.matchesAll(input, predicate); } /** @@ -1266,9 +1262,6 @@ public class CollectionUtils { } else if (object instanceof Iterable) { final Iterable iterable = (Iterable) object; return IterableUtils.get(iterable, i); - } else if (object instanceof Collection) { - final Iterator iterator = ((Collection) object).iterator(); - return IteratorUtils.get(iterator, i); } else if (object instanceof Enumeration) { final Enumeration it = (Enumeration) object; return EnumerationUtils.get(it, i); @@ -1631,7 +1624,7 @@ public class CollectionUtils { */ public static Collection> permutations(final Collection collection) { final PermutationIterator it = new PermutationIterator(collection); - final Collection> result = new LinkedList>(); + final Collection> result = new ArrayList>(); while (it.hasNext()) { result.add(it.next()); }