-
Notifications
You must be signed in to change notification settings - Fork 511
small improvements for collectionUtils #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,12 +19,12 @@ | |
| 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 Collection<O> list() { | |
| * 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<Object>()); | ||
| public static final Collection EMPTY_COLLECTION = Collections.emptyList(); | ||
|
|
||
| /** | ||
| * <code>CollectionUtils</code> should not normally be instantiated. | ||
|
|
@@ -209,9 +208,8 @@ public static <T> Collection<T> emptyCollection() { | |
| * @param collection the collection, possibly <code>null</code> | ||
| * @return an empty collection if the argument is <code>null</code> | ||
| */ | ||
| @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type | ||
| public static <T> Collection<T> emptyIfNull(final Collection<T> collection) { | ||
| return collection == null ? EMPTY_COLLECTION : collection; | ||
| return collection == null ? CollectionUtils.<T>emptyCollection() : collection; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -389,9 +387,7 @@ public static boolean containsAll(final Collection<?> coll1, final Collection<?> | |
| } | ||
| } | ||
|
|
||
| if (foundCurrentElement) { | ||
| continue; | ||
| } else { | ||
| if (!foundCurrentElement) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
@@ -830,7 +826,7 @@ public static <C> int countMatches(final Iterable<C> input, final Predicate<? su | |
| */ | ||
| @Deprecated | ||
| public static <C> boolean exists(final Iterable<C> input, final Predicate<? super C> predicate) { | ||
| return predicate == null ? false : IterableUtils.matchesAny(input, predicate); | ||
| return predicate != null && IterableUtils.matchesAny(input, predicate); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -850,7 +846,7 @@ public static <C> boolean exists(final Iterable<C> input, final Predicate<? supe | |
| */ | ||
| @Deprecated | ||
| public static <C> boolean matchesAll(final Iterable<C> input, final Predicate<? super C> predicate) { | ||
| return predicate == null ? false : IterableUtils.matchesAll(input, predicate); | ||
| return predicate != null && IterableUtils.matchesAll(input, predicate); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1266,9 +1262,6 @@ public static Object get(final Object object, final int index) { | |
| } else if (object instanceof Iterable<?>) { | ||
| final Iterable<?> iterable = (Iterable<?>) object; | ||
| return IterableUtils.get(iterable, i); | ||
| } else if (object instanceof Collection<?>) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this one really safe to remove? It seems to assume that the else condition will handle with the same behaviour, but I have not tested it to confirm. Might be worth also moving that
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing of this condition is safe. Moreover, this check is useless, because, any collection implements interface Iterable, and if object is Collection, then previous condition
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. Will check out the code to review with the IDE, plus some coffee 💤 I think it looks good to be merged. Thanks for the quick replies @izebit
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oooohh, just re-read, and finally understood. Sorry, nice catch again. |
||
| 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 static <O> List<O> collate(final Iterable<? extends O> a, final Iterable< | |
| */ | ||
| public static <E> Collection<List<E>> permutations(final Collection<E> collection) { | ||
| final PermutationIterator<E> it = new PermutationIterator<E>(collection); | ||
| final Collection<List<E>> result = new LinkedList<List<E>>(); | ||
| final Collection<List<E>> result = new ArrayList<List<E>>(); | ||
| while (it.hasNext()) { | ||
| result.add(it.next()); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:))