Better NPE messages in CollectionUtils with Objects.requireNonNull (#117)

* Deal the null for CollectionUtils

* Deal the null for CollectionUtils

* Deal the null for CollectionUtils

* Deal the NPE with Objects.requireNonNull in the CollectionUtils.java

* Keep it simple.

* Update the Javadoc and keep it simple.
This commit is contained in:
Chen 2020-04-09 21:02:30 +08:00 committed by GitHub
parent b2b52ef972
commit 7eb78290c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 395 additions and 83 deletions

View File

@ -228,9 +228,12 @@ public class CollectionUtils {
* @param <O> the generic type that is able to represent the types contained * @param <O> the generic type that is able to represent the types contained
* in both input collections. * in both input collections.
* @return the union of the two collections * @return the union of the two collections
* @throws NullPointerException if either collection is null
* @see Collection#addAll * @see Collection#addAll
*/ */
public static <O> Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b) { public static <O> Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b) {
Objects.requireNonNull(a, "a");
Objects.requireNonNull(b, "b");
final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<>(a, b); final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<>(a, b);
for (final O obj : helper) { for (final O obj : helper) {
helper.setCardinality(obj, helper.max(obj)); helper.setCardinality(obj, helper.max(obj));
@ -252,10 +255,13 @@ public class CollectionUtils {
* @param <O> the generic type that is able to represent the types contained * @param <O> the generic type that is able to represent the types contained
* in both input collections. * in both input collections.
* @return the intersection of the two collections * @return the intersection of the two collections
* @throws NullPointerException if either collection is null
* @see Collection#retainAll * @see Collection#retainAll
* @see #containsAny * @see #containsAny
*/ */
public static <O> Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b) { public static <O> Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b) {
Objects.requireNonNull(a, "a");
Objects.requireNonNull(b, "b");
final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<>(a, b); final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<>(a, b);
for (final O obj : helper) { for (final O obj : helper) {
helper.setCardinality(obj, helper.min(obj)); helper.setCardinality(obj, helper.min(obj));
@ -284,8 +290,11 @@ public class CollectionUtils {
* @param <O> the generic type that is able to represent the types contained * @param <O> the generic type that is able to represent the types contained
* in both input collections. * in both input collections.
* @return the symmetric difference of the two collections * @return the symmetric difference of the two collections
* @throws NullPointerException if either collection is null
*/ */
public static <O> Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b) { public static <O> Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b) {
Objects.requireNonNull(a, "a");
Objects.requireNonNull(b, "b");
final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<>(a, b); final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<>(a, b);
for (final O obj : helper) { for (final O obj : helper) {
helper.setCardinality(obj, helper.max(obj) - helper.min(obj)); helper.setCardinality(obj, helper.max(obj) - helper.min(obj));
@ -333,12 +342,16 @@ public class CollectionUtils {
* @param <O> the generic type that is able to represent the types contained * @param <O> the generic type that is able to represent the types contained
* in both input collections. * in both input collections.
* @return a new collection with the results * @return a new collection with the results
* @throws NullPointerException if either collection or p is null
* @since 4.0 * @since 4.0
* @see Collection#removeAll * @see Collection#removeAll
*/ */
public static <O> Collection<O> subtract(final Iterable<? extends O> a, public static <O> Collection<O> subtract(final Iterable<? extends O> a,
final Iterable<? extends O> b, final Iterable<? extends O> b,
final Predicate<O> p) { final Predicate<O> p) {
Objects.requireNonNull(a, "a");
Objects.requireNonNull(b, "b");
Objects.requireNonNull(p, "p");
final ArrayList<O> list = new ArrayList<>(); final ArrayList<O> list = new ArrayList<>();
final HashBag<O> bag = new HashBag<>(); final HashBag<O> bag = new HashBag<>();
for (final O element : b) { for (final O element : b) {
@ -376,9 +389,12 @@ public class CollectionUtils {
* @param coll2 the second collection, must not be null * @param coll2 the second collection, must not be null
* @return {@code true} iff the intersection of the collections has the same cardinality * @return {@code true} iff the intersection of the collections has the same cardinality
* as the set of unique elements from the second collection * as the set of unique elements from the second collection
* @throws NullPointerException if coll1 or coll2 is null
* @since 4.0 * @since 4.0
*/ */
public static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2) { public static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2) {
Objects.requireNonNull(coll1, "coll1");
Objects.requireNonNull(coll2, "coll2");
if (coll2.isEmpty()) { if (coll2.isEmpty()) {
return true; return true;
} }
@ -417,10 +433,13 @@ public class CollectionUtils {
* @param coll1 the first collection, must not be null * @param coll1 the first collection, must not be null
* @param coll2 the second collection, must not be null * @param coll2 the second collection, must not be null
* @return {@code true} iff the intersection of the collections is non-empty * @return {@code true} iff the intersection of the collections is non-empty
* @throws NullPointerException if coll1 or coll2 is null
* @since 4.2 * @since 4.2
* @see #intersection * @see #intersection
*/ */
public static <T> boolean containsAny(final Collection<?> coll1, @SuppressWarnings("unchecked") final T... coll2) { public static <T> boolean containsAny(final Collection<?> coll1, @SuppressWarnings("unchecked") final T... coll2) {
Objects.requireNonNull(coll1, "coll1");
Objects.requireNonNull(coll2, "coll2");
if (coll1.size() < coll2.length) { if (coll1.size() < coll2.length) {
for (final Object aColl1 : coll1) { for (final Object aColl1 : coll1) {
if (ArrayUtils.contains(coll2, aColl1)) { if (ArrayUtils.contains(coll2, aColl1)) {
@ -447,10 +466,13 @@ public class CollectionUtils {
* @param coll1 the first collection, must not be null * @param coll1 the first collection, must not be null
* @param coll2 the second collection, must not be null * @param coll2 the second collection, must not be null
* @return {@code true} iff the intersection of the collections is non-empty * @return {@code true} iff the intersection of the collections is non-empty
* @throws NullPointerException if coll1 or coll2 is null
* @since 2.1 * @since 2.1
* @see #intersection * @see #intersection
*/ */
public static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2) { public static boolean containsAny(final Collection<?> coll1, final Collection<?> coll2) {
Objects.requireNonNull(coll1, "coll1");
Objects.requireNonNull(coll2, "coll2");
if (coll1.size() < coll2.size()) { if (coll1.size() < coll2.size()) {
for (final Object aColl1 : coll1) { for (final Object aColl1 : coll1) {
if (coll2.contains(aColl1)) { if (coll2.contains(aColl1)) {
@ -479,8 +501,10 @@ public class CollectionUtils {
* @param <O> the type of object in the returned {@link Map}. This is a super type of &lt;I&gt;. * @param <O> the type of object in the returned {@link Map}. This is a super type of &lt;I&gt;.
* @param coll the collection to get the cardinality map for, must not be null * @param coll the collection to get the cardinality map for, must not be null
* @return the populated cardinality map * @return the populated cardinality map
* @throws NullPointerException if coll is null
*/ */
public static <O> Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll) { public static <O> Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll) {
Objects.requireNonNull(coll, "coll");
final Map<O, Integer> count = new HashMap<>(); final Map<O, Integer> count = new HashMap<>();
for (final O obj : coll) { for (final O obj : coll) {
final Integer c = count.get(obj); final Integer c = count.get(obj);
@ -502,10 +526,13 @@ public class CollectionUtils {
* @param a the first (sub?) collection, must not be null * @param a the first (sub?) collection, must not be null
* @param b the second (super?) collection, must not be null * @param b the second (super?) collection, must not be null
* @return {@code true} iff <i>a</i> is a sub-collection of <i>b</i> * @return {@code true} iff <i>a</i> is a sub-collection of <i>b</i>
* @throws NullPointerException if either collection is null
* @see #isProperSubCollection * @see #isProperSubCollection
* @see Collection#containsAll * @see Collection#containsAll
*/ */
public static boolean isSubCollection(final Collection<?> a, final Collection<?> b) { public static boolean isSubCollection(final Collection<?> a, final Collection<?> b) {
Objects.requireNonNull(a, "a");
Objects.requireNonNull(b, "b");
final CardinalityHelper<Object> helper = new CardinalityHelper<>(a, b); final CardinalityHelper<Object> helper = new CardinalityHelper<>(a, b);
for (final Object obj : a) { for (final Object obj : a) {
if (helper.freqA(obj) > helper.freqB(obj)) { if (helper.freqA(obj) > helper.freqB(obj)) {
@ -534,10 +561,13 @@ public class CollectionUtils {
* @param a the first (sub?) collection, must not be null * @param a the first (sub?) collection, must not be null
* @param b the second (super?) collection, must not be null * @param b the second (super?) collection, must not be null
* @return {@code true} iff <i>a</i> is a <i>proper</i> sub-collection of <i>b</i> * @return {@code true} iff <i>a</i> is a <i>proper</i> sub-collection of <i>b</i>
* @throws NullPointerException if either collection is null
* @see #isSubCollection * @see #isSubCollection
* @see Collection#containsAll * @see Collection#containsAll
*/ */
public static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b) { public static boolean isProperSubCollection(final Collection<?> a, final Collection<?> b) {
Objects.requireNonNull(a, "a");
Objects.requireNonNull(b, "b");
return a.size() < b.size() && CollectionUtils.isSubCollection(a, b); return a.size() < b.size() && CollectionUtils.isSubCollection(a, b);
} }
@ -553,8 +583,11 @@ public class CollectionUtils {
* @param a the first collection, must not be null * @param a the first collection, must not be null
* @param b the second collection, must not be null * @param b the second collection, must not be null
* @return {@code true} iff the collections contain the same elements with the same cardinalities. * @return {@code true} iff the collections contain the same elements with the same cardinalities.
* @throws NullPointerException if either collection is null
*/ */
public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) { public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) {
Objects.requireNonNull(a, "a");
Objects.requireNonNull(b, "b");
if (a.size() != b.size()) { if (a.size() != b.size()) {
return false; return false;
} }
@ -590,12 +623,14 @@ public class CollectionUtils {
* @param b the second collection, must not be null * @param b the second collection, must not be null
* @param equator the Equator used for testing equality * @param equator the Equator used for testing equality
* @return {@code true} iff the collections contain the same elements with the same cardinalities. * @return {@code true} iff the collections contain the same elements with the same cardinalities.
* @throws NullPointerException if the equator is null * @throws NullPointerException if either collection or equator is null
* @since 4.0 * @since 4.0
*/ */
public static <E> boolean isEqualCollection(final Collection<? extends E> a, public static <E> boolean isEqualCollection(final Collection<? extends E> a,
final Collection<? extends E> b, final Collection<? extends E> b,
final Equator<? super E> equator) { final Equator<? super E> equator) {
Objects.requireNonNull(a, "a");
Objects.requireNonNull(b, "b");
Objects.requireNonNull(equator, "equator"); Objects.requireNonNull(equator, "equator");
if (a.size() != b.size()) { if (a.size() != b.size()) {
@ -654,7 +689,7 @@ public class CollectionUtils {
* @param collection the {@link Iterable} to search * @param collection the {@link Iterable} to search
* @param <O> the type of object that the {@link Iterable} may contain. * @param <O> the type of object that the {@link Iterable} may contain.
* @return the number of occurrences of obj in coll * @return the number of occurrences of obj in coll
* @throws NullPointerException if coll is null * @throws NullPointerException if collection is null
* @deprecated since 4.1, use {@link IterableUtils#frequency(Iterable, Object)} instead. * @deprecated since 4.1, use {@link IterableUtils#frequency(Iterable, Object)} instead.
* Be aware that the order of parameters has changed. * Be aware that the order of parameters has changed.
*/ */
@ -916,12 +951,14 @@ public class CollectionUtils {
* @param inputCollection the collection to get the input from, may not be null * @param inputCollection the collection to get the input from, may not be null
* @param predicate the predicate to use, may be null * @param predicate the predicate to use, may be null
* @return the elements matching the predicate (new list) * @return the elements matching the predicate (new list)
* @throws NullPointerException if the input collection is null
*/ */
public static <O> Collection<O> select(final Iterable<? extends O> inputCollection, public static <O> Collection<O> select(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate) { final Predicate<? super O> predicate) {
final Collection<O> answer = inputCollection instanceof Collection<?> ? int size = 0;
new ArrayList<>(((Collection<?>) inputCollection).size()) : new ArrayList<>(); if (null != inputCollection) {
size = inputCollection instanceof Collection<?> ? ((Collection<?>) inputCollection).size() : 0;
}
final Collection<O> answer = size == 0 ? new ArrayList<>() : new ArrayList<>(size);
return select(inputCollection, predicate, answer); return select(inputCollection, predicate, answer);
} }
@ -1011,12 +1048,14 @@ public class CollectionUtils {
* @param inputCollection the collection to get the input from, may not be null * @param inputCollection the collection to get the input from, may not be null
* @param predicate the predicate to use, may be null * @param predicate the predicate to use, may be null
* @return the elements <b>not</b> matching the predicate (new list) * @return the elements <b>not</b> matching the predicate (new list)
* @throws NullPointerException if the input collection is null
*/ */
public static <O> Collection<O> selectRejected(final Iterable<? extends O> inputCollection, public static <O> Collection<O> selectRejected(final Iterable<? extends O> inputCollection,
final Predicate<? super O> predicate) { final Predicate<? super O> predicate) {
final Collection<O> answer = inputCollection instanceof Collection<?> ? int size = 0;
new ArrayList<>(((Collection<?>) inputCollection).size()) : new ArrayList<>(); if (null != inputCollection) {
size = inputCollection instanceof Collection<?> ? ((Collection<?>) inputCollection).size() : 0;
}
final Collection<O> answer = size == 0 ? new ArrayList<>() : new ArrayList<>(size);
return selectRejected(inputCollection, predicate, answer); return selectRejected(inputCollection, predicate, answer);
} }
@ -1061,12 +1100,16 @@ public class CollectionUtils {
* @param inputCollection the collection to get the input from, may not be null * @param inputCollection the collection to get the input from, may not be null
* @param transformer the transformer to use, may be null * @param transformer the transformer to use, may be null
* @return the transformed result (new list) * @return the transformed result (new list)
* @throws NullPointerException if the input collection is null * @throws NullPointerException if the outputCollection is null and both, inputCollection and
* transformer are not null
*/ */
public static <I, O> Collection<O> collect(final Iterable<I> inputCollection, public static <I, O> Collection<O> collect(final Iterable<I> inputCollection,
final Transformer<? super I, ? extends O> transformer) { final Transformer<? super I, ? extends O> transformer) {
final Collection<O> answer = inputCollection instanceof Collection<?> ? int size = 0;
new ArrayList<>(((Collection<?>) inputCollection).size()) : new ArrayList<>(); if (null != inputCollection) {
size = inputCollection instanceof Collection<?> ? ((Collection<?>) inputCollection).size() : 0;
}
final Collection<O> answer = size == 0 ? new ArrayList<>() : new ArrayList<>(size);
return collect(inputCollection, transformer, answer); return collect(inputCollection, transformer, answer);
} }
@ -1171,9 +1214,11 @@ public class CollectionUtils {
* @param collection the collection to add to, must not be null * @param collection the collection to add to, must not be null
* @param iterable the iterable of elements to add, must not be null * @param iterable the iterable of elements to add, must not be null
* @return a boolean indicating whether the collection has changed or not. * @return a boolean indicating whether the collection has changed or not.
* @throws NullPointerException if the collection or iterator is null * @throws NullPointerException if the collection or iterable is null
*/ */
public static <C> boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable) { public static <C> boolean addAll(final Collection<C> collection, final Iterable<? extends C> iterable) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(iterable, "iterable");
if (iterable instanceof Collection<?>) { if (iterable instanceof Collection<?>) {
return collection.addAll((Collection<? extends C>) iterable); return collection.addAll((Collection<? extends C>) iterable);
} }
@ -1190,6 +1235,8 @@ public class CollectionUtils {
* @throws NullPointerException if the collection or iterator is null * @throws NullPointerException if the collection or iterator is null
*/ */
public static <C> boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator) { public static <C> boolean addAll(final Collection<C> collection, final Iterator<? extends C> iterator) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(iterator, "iterator");
boolean changed = false; boolean changed = false;
while (iterator.hasNext()) { while (iterator.hasNext()) {
changed |= collection.add(iterator.next()); changed |= collection.add(iterator.next());
@ -1207,6 +1254,8 @@ public class CollectionUtils {
* @throws NullPointerException if the collection or enumeration is null * @throws NullPointerException if the collection or enumeration is null
*/ */
public static <C> boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration) { public static <C> boolean addAll(final Collection<C> collection, final Enumeration<? extends C> enumeration) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(enumeration, "enumeration");
boolean changed = false; boolean changed = false;
while (enumeration.hasMoreElements()) { while (enumeration.hasMoreElements()) {
changed |= collection.add(enumeration.nextElement()); changed |= collection.add(enumeration.nextElement());
@ -1221,9 +1270,11 @@ public class CollectionUtils {
* @param collection the collection to add to, must not be null * @param collection the collection to add to, must not be null
* @param elements the array of elements to add, must not be null * @param elements the array of elements to add, must not be null
* @return {@code true} if the collection was changed, {@code false} otherwise * @return {@code true} if the collection was changed, {@code false} otherwise
* @throws NullPointerException if the collection or array is null * @throws NullPointerException if the collection or elements is null
*/ */
public static <C> boolean addAll(final Collection<C> collection, final C... elements) { public static <C> boolean addAll(final Collection<C> collection, final C... elements) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(elements, "elements");
boolean changed = false; boolean changed = false;
for (final C element : elements) { for (final C element : elements) {
changed |= collection.add(element); changed |= collection.add(element);
@ -1245,10 +1296,12 @@ public class CollectionUtils {
* @return the object at the specified index * @return the object at the specified index
* @throws IndexOutOfBoundsException if the index is invalid * @throws IndexOutOfBoundsException if the index is invalid
* @throws IllegalArgumentException if the object type is invalid * @throws IllegalArgumentException if the object type is invalid
* @throws NullPointerException if iterator is null
* @deprecated since 4.1, use {@code IteratorUtils.get(Iterator, int)} instead * @deprecated since 4.1, use {@code IteratorUtils.get(Iterator, int)} instead
*/ */
@Deprecated @Deprecated
public static <T> T get(final Iterator<T> iterator, final int index) { public static <T> T get(final Iterator<T> iterator, final int index) {
Objects.requireNonNull(iterator, "iterator");
return IteratorUtils.get(iterator, index); return IteratorUtils.get(iterator, index);
} }
@ -1279,6 +1332,7 @@ public class CollectionUtils {
*/ */
@Deprecated @Deprecated
public static <T> T get(final Iterable<T> iterable, final int index) { public static <T> T get(final Iterable<T> iterable, final int index) {
Objects.requireNonNull(iterable, "iterable");
return IterableUtils.get(iterable, index); return IterableUtils.get(iterable, index);
} }
@ -1349,13 +1403,14 @@ public class CollectionUtils {
* throwing {@code IndexOutOfBoundsException} if there is no such element. * throwing {@code IndexOutOfBoundsException} if there is no such element.
* *
* @param <K> the key type in the {@link Map} * @param <K> the key type in the {@link Map}
* @param <V> the key type in the {@link Map} * @param <V> the value type in the {@link Map}
* @param map the object to get a value from * @param map the object to get a value from
* @param index the index to get * @param index the index to get
* @return the object at the specified index * @return the object at the specified index
* @throws IndexOutOfBoundsException if the index is invalid * @throws IndexOutOfBoundsException if the index is invalid
*/ */
public static <K, V> Map.Entry<K, V> get(final Map<K, V> map, final int index) { public static <K, V> Map.Entry<K, V> get(final Map<K, V> map, final int index) {
Objects.requireNonNull(map, "map");
checkIndexBounds(index); checkIndexBounds(index);
return get(map.entrySet(), index); return get(map.entrySet(), index);
} }
@ -1491,6 +1546,7 @@ public class CollectionUtils {
* @param array the array to reverse * @param array the array to reverse
*/ */
public static void reverseArray(final Object[] array) { public static void reverseArray(final Object[] array) {
Objects.requireNonNull(array, "array");
int i = 0; int i = 0;
int j = array.length - 1; int j = array.length - 1;
Object tmp; Object tmp;
@ -1692,12 +1748,13 @@ public class CollectionUtils {
* @see PermutationIterator * @see PermutationIterator
* *
* @param <E> the element type * @param <E> the element type
* @param collection the collection to create permutations for, may not be null * @param collection the collection to create permutations for, must not be null
* @return an unordered collection of all permutations of the input collection * @return an unordered collection of all permutations of the input collection
* @throws NullPointerException if collection is null * @throws NullPointerException if collection is null
* @since 4.0 * @since 4.0
*/ */
public static <E> Collection<List<E>> permutations(final Collection<E> collection) { public static <E> Collection<List<E>> permutations(final Collection<E> collection) {
Objects.requireNonNull(collection, "collection");
final PermutationIterator<E> it = new PermutationIterator<>(collection); final PermutationIterator<E> it = new PermutationIterator<>(collection);
final Collection<List<E>> result = new ArrayList<>(); final Collection<List<E>> result = new ArrayList<>();
while (it.hasNext()) { while (it.hasNext()) {
@ -1731,6 +1788,8 @@ public class CollectionUtils {
* @since 3.2 * @since 3.2
*/ */
public static <C> Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain) { public static <C> Collection<C> retainAll(final Collection<C> collection, final Collection<?> retain) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(retain, "retain");
return ListUtils.retainAll(collection, retain); return ListUtils.retainAll(collection, retain);
} }
@ -1763,7 +1822,9 @@ public class CollectionUtils {
public static <E> Collection<E> retainAll(final Iterable<E> collection, public static <E> Collection<E> retainAll(final Iterable<E> collection,
final Iterable<? extends E> retain, final Iterable<? extends E> retain,
final Equator<? super E> equator) { final Equator<? super E> equator) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(retain, "retain");
Objects.requireNonNull(equator, "equator");
final Transformer<E, EquatorWrapper<E>> transformer = input -> new EquatorWrapper<>(equator, input); final Transformer<E, EquatorWrapper<E>> transformer = input -> new EquatorWrapper<>(equator, input);
final Set<EquatorWrapper<E>> retainSet = final Set<EquatorWrapper<E>> retainSet =
@ -1784,16 +1845,15 @@ public class CollectionUtils {
* This method modifies the input collections. * This method modifies the input collections.
* *
* @param <E> the type of object the {@link Collection} contains * @param <E> the type of object the {@link Collection} contains
* @param input the collection will be operated, can't be null * @param input the collection will be operated, must not be null
* @param startIndex the start index (inclusive) to remove element, can't be less than 0 * @param startIndex the start index (inclusive) to remove element, must not be less than 0
* @param endIndex the end index (exclusive) to remove, can't be less than startIndex * @param endIndex the end index (exclusive) to remove, must not be less than startIndex
* @return collection of elements that removed from the input collection * @return collection of elements that removed from the input collection
* @throws NullPointerException if input is null
* @since 4.5 * @since 4.5
*/ */
public static <E> Collection<E> removeRange(final Collection<E> input, final int startIndex, final int endIndex) { public static <E> Collection<E> removeRange(final Collection<E> input, final int startIndex, final int endIndex) {
if (null == input) { Objects.requireNonNull(input, "input");
throw new IllegalArgumentException("The collection can't be null.");
}
if (endIndex < startIndex) { if (endIndex < startIndex) {
throw new IllegalArgumentException("The end index can't be less than the start index."); throw new IllegalArgumentException("The end index can't be less than the start index.");
} }
@ -1812,12 +1872,11 @@ public class CollectionUtils {
* @param startIndex the start index (inclusive) to remove element, can't be less than 0 * @param startIndex the start index (inclusive) to remove element, can't be less than 0
* @param count the specified number to remove, can't be less than 1 * @param count the specified number to remove, can't be less than 1
* @return collection of elements that removed from the input collection * @return collection of elements that removed from the input collection
* @throws NullPointerException if input is null
* @since 4.5 * @since 4.5
*/ */
public static <E> Collection<E> removeCount(final Collection<E> input, int startIndex, int count) { public static <E> Collection<E> removeCount(final Collection<E> input, int startIndex, int count) {
if (null == input) { Objects.requireNonNull(input, "input");
throw new IllegalArgumentException("The collection can't be null.");
}
if (startIndex < 0) { if (startIndex < 0) {
throw new IndexOutOfBoundsException("The start index can't be less than 0."); throw new IndexOutOfBoundsException("The start index can't be less than 0.");
} }
@ -1902,7 +1961,9 @@ public class CollectionUtils {
public static <E> Collection<E> removeAll(final Iterable<E> collection, public static <E> Collection<E> removeAll(final Iterable<E> collection,
final Iterable<? extends E> remove, final Iterable<? extends E> remove,
final Equator<? super E> equator) { final Equator<? super E> equator) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(remove, "remove");
Objects.requireNonNull(equator, "equator");
final Transformer<E, EquatorWrapper<E>> transformer = input -> new EquatorWrapper<>(equator, input); final Transformer<E, EquatorWrapper<E>> transformer = input -> new EquatorWrapper<>(equator, input);
final Set<EquatorWrapper<E>> removeSet = final Set<EquatorWrapper<E>> removeSet =
@ -1945,6 +2006,7 @@ public class CollectionUtils {
*/ */
@Deprecated @Deprecated
public static <C> Collection<C> synchronizedCollection(final Collection<C> collection) { public static <C> Collection<C> synchronizedCollection(final Collection<C> collection) {
Objects.requireNonNull(collection, "collection");
return SynchronizedCollection.synchronizedCollection(collection); return SynchronizedCollection.synchronizedCollection(collection);
} }
@ -1962,6 +2024,7 @@ public class CollectionUtils {
*/ */
@Deprecated @Deprecated
public static <C> Collection<C> unmodifiableCollection(final Collection<? extends C> collection) { public static <C> Collection<C> unmodifiableCollection(final Collection<? extends C> collection) {
Objects.requireNonNull(collection, "collection");
return UnmodifiableCollection.unmodifiableCollection(collection); return UnmodifiableCollection.unmodifiableCollection(collection);
} }
@ -1978,10 +2041,12 @@ public class CollectionUtils {
* @param collection the collection to predicate, must not be null * @param collection the collection to predicate, must not be null
* @param predicate the predicate for the collection, must not be null * @param predicate the predicate for the collection, must not be null
* @return a predicated collection backed by the given collection * @return a predicated collection backed by the given collection
* @throws NullPointerException if the Collection is null * @throws NullPointerException if the collection or predicate is null
*/ */
public static <C> Collection<C> predicatedCollection(final Collection<C> collection, public static <C> Collection<C> predicatedCollection(final Collection<C> collection,
final Predicate<? super C> predicate) { final Predicate<? super C> predicate) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(predicate, "predicate");
return PredicatedCollection.predicatedCollection(collection, predicate); return PredicatedCollection.predicatedCollection(collection, predicate);
} }
@ -2001,10 +2066,12 @@ public class CollectionUtils {
* @param collection the collection to predicate, must not be null * @param collection the collection to predicate, must not be null
* @param transformer the transformer for the collection, must not be null * @param transformer the transformer for the collection, must not be null
* @return a transformed collection backed by the given collection * @return a transformed collection backed by the given collection
* @throws NullPointerException if the Collection or Transformer is null * @throws NullPointerException if the collection or transformer is null
*/ */
public static <E> Collection<E> transformingCollection(final Collection<E> collection, public static <E> Collection<E> transformingCollection(final Collection<E> collection,
final Transformer<? super E, ? extends E> transformer) { final Transformer<? super E, ? extends E> transformer) {
Objects.requireNonNull(collection, "collection");
Objects.requireNonNull(transformer, "transformer");
return TransformedCollection.transformingCollection(collection, transformer); return TransformedCollection.transformingCollection(collection, transformer);
} }

View File

@ -191,6 +191,11 @@ public class CollectionUtilsTest extends MockTestCase {
assertEquals(1, (int) freqB.get(5L)); assertEquals(1, (int) freqB.get(5L));
} }
@Test(expected = NullPointerException.class)
public void testGetCardinalityMapNull() {
CollectionUtils.getCardinalityMap(null);
}
@Test @Test
@Deprecated @Deprecated
public void cardinality() { public void cardinality() {
@ -347,6 +352,29 @@ public class CollectionUtilsTest extends MockTestCase {
assertTrue("containsAny({},{}) should return false.", !CollectionUtils.containsAny(empty, empty)); assertTrue("containsAny({},{}) should return false.", !CollectionUtils.containsAny(empty, empty));
} }
@Test(expected = NullPointerException.class)
public void testContainsAnyNullColl1() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.containsAny(null, list);
}
@Test(expected = NullPointerException.class)
public void testContainsAnyNullColl2() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
final Collection<String> list2 = null;
CollectionUtils.containsAny(list, list2);
}
@Test(expected = NullPointerException.class)
public void testContainsAnyNullColl3() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
final String[] array = null;
CollectionUtils.containsAny(list, array);
}
@Test @Test
public void containsAnyInArray() { public void containsAnyInArray() {
final Collection<String> empty = new ArrayList<>(0); final Collection<String> empty = new ArrayList<>(0);
@ -381,6 +409,28 @@ public class CollectionUtilsTest extends MockTestCase {
assertTrue("containsAny({},{}) should return false.", !CollectionUtils.containsAny(empty, emptyArr)); assertTrue("containsAny({},{}) should return false.", !CollectionUtils.containsAny(empty, emptyArr));
} }
@Test(expected = NullPointerException.class)
public void testContainsAnyInArrayNullColl1() {
final String[] oneArr = {"1"};
CollectionUtils.containsAny(null, oneArr);
}
@Test(expected = NullPointerException.class)
public void testContainsAnyInArrayNullColl2() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
final Collection<String> list2 = null;
CollectionUtils.containsAny(list, list2);
}
@Test(expected = NullPointerException.class)
public void testContainsAnyInArrayNullArray() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
final String[] array = null;
CollectionUtils.containsAny(list, array);
}
@Test @Test
public void union() { public void union() {
final Collection<Integer> col = CollectionUtils.union(iterableA, iterableC); final Collection<Integer> col = CollectionUtils.union(iterableA, iterableC);
@ -400,6 +450,20 @@ public class CollectionUtilsTest extends MockTestCase {
assertEquals(Integer.valueOf(1), freq2.get(5)); assertEquals(Integer.valueOf(1), freq2.get(5));
} }
@Test(expected = NullPointerException.class)
public void testUnionNullColl1() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.union(null, list);
}
@Test(expected = NullPointerException.class)
public void testUnionNullColl2() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.union(list, null);
}
@Test @Test
public void intersection() { public void intersection() {
final Collection<Integer> col = CollectionUtils.intersection(iterableA, iterableC); final Collection<Integer> col = CollectionUtils.intersection(iterableA, iterableC);
@ -419,6 +483,20 @@ public class CollectionUtilsTest extends MockTestCase {
assertNull(freq2.get(5)); assertNull(freq2.get(5));
} }
@Test(expected = NullPointerException.class)
public void testIntersectionNullColl1() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.intersection(null, list);
}
@Test(expected = NullPointerException.class)
public void testIntersectionNullColl2() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.intersection(list, null);
}
@Test @Test
public void disjunction() { public void disjunction() {
final Collection<Integer> col = CollectionUtils.disjunction(iterableA, iterableC); final Collection<Integer> col = CollectionUtils.disjunction(iterableA, iterableC);
@ -438,6 +516,20 @@ public class CollectionUtilsTest extends MockTestCase {
assertEquals(Integer.valueOf(1), freq2.get(5)); assertEquals(Integer.valueOf(1), freq2.get(5));
} }
@Test(expected = NullPointerException.class)
public void testDisjunctionNullColl1() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.disjunction(null, list);
}
@Test(expected = NullPointerException.class)
public void testDisjunctionNullColl2() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.disjunction(list, null);
}
@Test @Test
public void testDisjunctionAsUnionMinusIntersection() { public void testDisjunctionAsUnionMinusIntersection() {
final Collection<Number> dis = CollectionUtils.<Number>disjunction(collectionA, collectionC); final Collection<Number> dis = CollectionUtils.<Number>disjunction(collectionA, collectionC);
@ -473,6 +565,20 @@ public class CollectionUtilsTest extends MockTestCase {
assertNull(freq2.get(1)); assertNull(freq2.get(1));
} }
@Test(expected = NullPointerException.class)
public void testSubtractNullColl1() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.subtract(null, list);
}
@Test(expected = NullPointerException.class)
public void testSubtractNullColl2() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.subtract(list, null);
}
@Test @Test
public void testSubtractWithPredicate() { public void testSubtractWithPredicate() {
// greater than 3 // greater than 3
@ -539,6 +645,20 @@ public class CollectionUtilsTest extends MockTestCase {
assertTrue(CollectionUtils.isSubCollection(collectionA, c)); assertTrue(CollectionUtils.isSubCollection(collectionA, c));
} }
@Test(expected = NullPointerException.class)
public void testIsSubCollectionNullColl1() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.isSubCollection(null, list);
}
@Test(expected = NullPointerException.class)
public void testIsSubCollectionNullColl2() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.isSubCollection(list, null);
}
@Test @Test
public void testIsEqualCollectionToSelf() { public void testIsEqualCollectionToSelf() {
assertTrue(CollectionUtils.isEqualCollection(collectionA, collectionA)); assertTrue(CollectionUtils.isEqualCollection(collectionA, collectionA));
@ -621,6 +741,52 @@ public class CollectionUtilsTest extends MockTestCase {
CollectionUtils.isEqualCollection(collectionA, collectionA, null); CollectionUtils.isEqualCollection(collectionA, collectionA, null);
} }
@Test(expected = NullPointerException.class)
public void testIsEqualCollectionNullColl1() {
final Collection<Integer> list = new ArrayList<>(1);
list.add(1);
final Equator<Integer> e = new Equator<Integer>() {
@Override
public boolean equate(final Integer o1, final Integer o2) {
if (o1.intValue() % 2 == 0 ^ o2.intValue() % 2 == 0) {
return false;
}
return true;
}
@Override
public int hash(final Integer o) {
return o.intValue() % 2 == 0 ? Integer.valueOf(0).hashCode() : Integer.valueOf(1).hashCode();
}
};
CollectionUtils.isEqualCollection(null, list, e);
}
@Test(expected = NullPointerException.class)
public void testIsEqualCollectionNullColl2() {
final Collection<Integer> list = new ArrayList<>(1);
list.add(1);
final Equator<Integer> e = new Equator<Integer>() {
@Override
public boolean equate(final Integer o1, final Integer o2) {
if (o1.intValue() % 2 == 0 ^ o2.intValue() % 2 == 0) {
return false;
}
return true;
}
@Override
public int hash(final Integer o) {
return o.intValue() % 2 == 0 ? Integer.valueOf(0).hashCode() : Integer.valueOf(1).hashCode();
}
};
CollectionUtils.isEqualCollection(list, null, e);
}
@Test @Test
public void testIsProperSubCollection() { public void testIsProperSubCollection() {
final Collection<String> a = new ArrayList<>(); final Collection<String> a = new ArrayList<>();
@ -643,6 +809,20 @@ public class CollectionUtilsTest extends MockTestCase {
assertTrue(!CollectionUtils.isProperSubCollection(a, CollectionUtils.subtract(a, b))); assertTrue(!CollectionUtils.isProperSubCollection(a, CollectionUtils.subtract(a, b)));
} }
@Test(expected = NullPointerException.class)
public void testIsProperSubCollectionNullColl1() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.isProperSubCollection(null, list);
}
@Test(expected = NullPointerException.class)
public void testIsProperSubCollectionNullColl2() {
final Collection<String> list = new ArrayList<>(1);
list.add("1");
CollectionUtils.isProperSubCollection(list, null);
}
@Test @Test
@Deprecated @Deprecated
public void find() { public void find() {
@ -1329,24 +1509,29 @@ public class CollectionUtilsTest extends MockTestCase {
assertEquals(true, set.contains("4")); assertEquals(true, set.contains("4"));
} }
@Test(expected = NullPointerException.class)
public void testAddIgnoreNullNullColl() {
CollectionUtils.addIgnoreNull(null, "1");
}
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@Test @Test
public void predicatedCollection() { public void predicatedCollection() {
final Predicate<Object> predicate = PredicateUtils.instanceofPredicate(Integer.class); final Predicate<Object> predicate = PredicateUtils.instanceofPredicate(Integer.class);
final Collection<Number> collection = CollectionUtils.predicatedCollection(new ArrayList<Number>(), predicate); final Collection<Number> collection = CollectionUtils.predicatedCollection(new ArrayList<Number>(), predicate);
assertTrue("returned object should be a PredicatedCollection", collection instanceof PredicatedCollection); assertTrue("returned object should be a PredicatedCollection", collection instanceof PredicatedCollection);
try { }
CollectionUtils.predicatedCollection(new ArrayList<Number>(), null);
fail("Expecting NullPointerException for null predicate."); @Test(expected = NullPointerException.class)
} catch (final NullPointerException ex) { public void testPredicatedCollectionNullColl() {
// expected final Predicate<Object> predicate = PredicateUtils.instanceofPredicate(Integer.class);
} CollectionUtils.predicatedCollection(null, predicate);
try { }
CollectionUtils.predicatedCollection(null, predicate);
fail("Expecting NullPointerException for null collection."); @Test(expected = NullPointerException.class)
} catch (final NullPointerException ex) { public void testPredicatedCollectionNullPredicate() {
// expected final Collection<Integer> list = new ArrayList<>();
} CollectionUtils.predicatedCollection(list, null);
} }
@Test @Test
@ -1355,11 +1540,6 @@ public class CollectionUtilsTest extends MockTestCase {
set.add("1"); set.add("1");
set.add("2"); set.add("2");
set.add("3"); set.add("3");
try {
CollectionUtils.isFull(null);
fail();
} catch (final NullPointerException ex) {
}
assertFalse(CollectionUtils.isFull(set)); assertFalse(CollectionUtils.isFull(set));
final CircularFifoQueue<String> buf = new CircularFifoQueue<>(set); final CircularFifoQueue<String> buf = new CircularFifoQueue<>(set);
@ -1370,6 +1550,11 @@ public class CollectionUtilsTest extends MockTestCase {
assertEquals(false, CollectionUtils.isFull(buf)); assertEquals(false, CollectionUtils.isFull(buf));
} }
@Test(expected = NullPointerException.class)
public void testIsFullNullColl() {
CollectionUtils.isFull(null);
}
@Test @Test
public void isEmpty() { public void isEmpty() {
assertFalse(CollectionUtils.isNotEmpty(null)); assertFalse(CollectionUtils.isNotEmpty(null));
@ -1382,11 +1567,6 @@ public class CollectionUtilsTest extends MockTestCase {
set.add("1"); set.add("1");
set.add("2"); set.add("2");
set.add("3"); set.add("3");
try {
CollectionUtils.maxSize(null);
fail();
} catch (final NullPointerException ex) {
}
assertEquals(-1, CollectionUtils.maxSize(set)); assertEquals(-1, CollectionUtils.maxSize(set));
final Queue<String> buf = new CircularFifoQueue<>(set); final Queue<String> buf = new CircularFifoQueue<>(set);
@ -1397,6 +1577,11 @@ public class CollectionUtilsTest extends MockTestCase {
assertEquals(3, CollectionUtils.maxSize(buf)); assertEquals(3, CollectionUtils.maxSize(buf));
} }
@Test(expected = NullPointerException.class)
public void testMaxSizeNullColl() {
CollectionUtils.maxSize(null);
}
@Test @Test
public void intersectionUsesMethodEquals() { public void intersectionUsesMethodEquals() {
// Let elta and eltb be objects... // Let elta and eltb be objects...
@ -1464,12 +1649,20 @@ public class CollectionUtilsTest extends MockTestCase {
assertEquals(true, sub.contains("A")); assertEquals(true, sub.contains("A"));
assertEquals(true, sub.contains("C")); assertEquals(true, sub.contains("C"));
assertEquals(true, sub.contains("X")); assertEquals(true, sub.contains("X"));
}
try { @Test(expected = NullPointerException.class)
CollectionUtils.retainAll(null, null); public void testRetainAllNullBaseColl() {
fail("expecting NullPointerException"); final List<Object> sub = new ArrayList<>();
} catch (final NullPointerException npe) { sub.add("A");
} // this is what we want CollectionUtils.retainAll(null, sub);
}
@Test(expected = NullPointerException.class)
public void testRetainAllNullSubColl() {
final List<String> base = new ArrayList<>();
base.add("A");
CollectionUtils.retainAll(base, null);
} }
@Test @Test
@ -1490,24 +1683,24 @@ public class CollectionUtilsTest extends MockTestCase {
assertTrue(result.contains(3)); assertTrue(result.contains(3));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=NullPointerException.class)
public void testRemoveRangeNull() { public void testRemoveRangeNull() {
final Collection<Integer> list = null; final Collection<Integer> list = null;
final Collection result = CollectionUtils.removeRange(list, 0, 0); CollectionUtils.removeRange(list, 0, 0);
} }
@Test(expected=IndexOutOfBoundsException.class) @Test(expected=IndexOutOfBoundsException.class)
public void testRemoveRangeStartIndexNegative() { public void testRemoveRangeStartIndexNegative() {
final Collection<Integer> list = new ArrayList<>(); final Collection<Integer> list = new ArrayList<>();
list.add(1); list.add(1);
final Collection result = CollectionUtils.removeRange(list, -1, 1); CollectionUtils.removeRange(list, -1, 1);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)
public void testRemoveRangeEndIndexNegative() { public void testRemoveRangeEndIndexNegative() {
final Collection<Integer> list = new ArrayList<>(); final Collection<Integer> list = new ArrayList<>();
list.add(1); list.add(1);
final Collection result = CollectionUtils.removeRange(list, 0, -1); CollectionUtils.removeRange(list, 0, -1);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)
@ -1515,14 +1708,14 @@ public class CollectionUtilsTest extends MockTestCase {
final Collection<Integer> list = new ArrayList<>(); final Collection<Integer> list = new ArrayList<>();
list.add(1); list.add(1);
list.add(2); list.add(2);
final Collection result = CollectionUtils.removeRange(list, 1, 0); CollectionUtils.removeRange(list, 1, 0);
} }
@Test(expected=IndexOutOfBoundsException.class) @Test(expected=IndexOutOfBoundsException.class)
public void testRemoveRangeWrongEndIndex() { public void testRemoveRangeWrongEndIndex() {
final Collection<Integer> list = new ArrayList<>(); final Collection<Integer> list = new ArrayList<>();
list.add(1); list.add(1);
final Collection result = CollectionUtils.removeRange(list, 0, 2); CollectionUtils.removeRange(list, 0, 2);
} }
@Test @Test
@ -1556,29 +1749,29 @@ public class CollectionUtilsTest extends MockTestCase {
assertTrue(result.contains(5)); assertTrue(result.contains(5));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=NullPointerException.class)
public void testRemoveCountWithNull() { public void testRemoveCountWithNull() {
final Collection<Integer> list = null; final Collection<Integer> list = null;
final Collection result = CollectionUtils.removeCount(list, 0, 1); CollectionUtils.removeCount(list, 0, 1);
} }
@Test(expected=IndexOutOfBoundsException.class) @Test(expected=IndexOutOfBoundsException.class)
public void testRemoveCountStartNegative() { public void testRemoveCountStartNegative() {
final Collection<Integer> list = new ArrayList<>(); final Collection<Integer> list = new ArrayList<>();
final Collection result = CollectionUtils.removeCount(list, -1, 1); CollectionUtils.removeCount(list, -1, 1);
} }
@Test(expected=IndexOutOfBoundsException.class) @Test(expected=IndexOutOfBoundsException.class)
public void testRemoveCountNegative() { public void testRemoveCountNegative() {
final Collection<Integer> list = new ArrayList<>(); final Collection<Integer> list = new ArrayList<>();
final Collection result = CollectionUtils.removeCount(list, 0, -1); CollectionUtils.removeCount(list, 0, -1);
} }
@Test(expected=IndexOutOfBoundsException.class) @Test(expected=IndexOutOfBoundsException.class)
public void testRemoveCountWrongCount() { public void testRemoveCountWrongCount() {
final Collection<Integer> list = new ArrayList<>(); final Collection<Integer> list = new ArrayList<>();
list.add(1); list.add(1);
final Collection result = CollectionUtils.removeCount(list, 0, 2); CollectionUtils.removeCount(list, 0, 2);
} }
@Test @Test
@ -1605,12 +1798,20 @@ public class CollectionUtilsTest extends MockTestCase {
assertEquals(true, sub.contains("A")); assertEquals(true, sub.contains("A"));
assertEquals(true, sub.contains("C")); assertEquals(true, sub.contains("C"));
assertEquals(true, sub.contains("X")); assertEquals(true, sub.contains("X"));
}
try { @Test(expected = NullPointerException.class)
CollectionUtils.removeAll(null, null); public void testRemoveAllNullBaseColl() {
fail("expecting NullPointerException"); final List<String> sub = new ArrayList<>();
} catch (final NullPointerException npe) { sub.add("A");
} // this is what we want CollectionUtils.removeAll(null, sub);
}
@Test(expected = NullPointerException.class)
public void testRemoveAllNullSubColl() {
final List<String> base = new ArrayList<>();
base.add("A");
CollectionUtils.removeAll(base, null);
} }
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@ -1619,18 +1820,18 @@ public class CollectionUtilsTest extends MockTestCase {
final Transformer<Object, Object> transformer = TransformerUtils.nopTransformer(); final Transformer<Object, Object> transformer = TransformerUtils.nopTransformer();
final Collection<Object> collection = CollectionUtils.transformingCollection(new ArrayList<>(), transformer); final Collection<Object> collection = CollectionUtils.transformingCollection(new ArrayList<>(), transformer);
assertTrue("returned object should be a TransformedCollection", collection instanceof TransformedCollection); assertTrue("returned object should be a TransformedCollection", collection instanceof TransformedCollection);
try { }
CollectionUtils.transformingCollection(new ArrayList<>(), null);
fail("Expecting NullPointerException for null transformer."); @Test(expected = NullPointerException.class)
} catch (final NullPointerException ex) { public void testTransformingCollectionNullColl() {
// expected final Transformer<Object, Object> transformer = TransformerUtils.nopTransformer();
} CollectionUtils.transformingCollection(null, transformer);
try { }
CollectionUtils.transformingCollection(null, transformer);
fail("Expecting NullPointerException for null collection."); @Test(expected = NullPointerException.class)
} catch (final NullPointerException ex) { public void testTransformingCollectionNullTransformer() {
// expected final List<String> list = new ArrayList<>();
} CollectionUtils.transformingCollection(list, null);
} }
@Test @Test
@ -1741,6 +1942,40 @@ public class CollectionUtilsTest extends MockTestCase {
assertTrue(collectionA.contains(5)); assertTrue(collectionA.contains(5));
} }
@Test(expected = NullPointerException.class)
public void testaddAllNullColl1() {
final List<Integer> list = new ArrayList<>();
CollectionUtils.addAll(null, list);
}
@Test(expected = NullPointerException.class)
public void testAddAllNullColl2() {
final List<Integer> list = new ArrayList<>();
final Iterable<Integer> list2 = null;
CollectionUtils.addAll(list, list2);
}
@Test(expected = NullPointerException.class)
public void testAddAllNullColl3() {
final List<Integer> list = new ArrayList<>();
final Iterator<Integer> list2 = null;
CollectionUtils.addAll(list, list2);
}
@Test(expected = NullPointerException.class)
public void testAddAllNullColl4() {
final List<Integer> list = new ArrayList<>();
final Enumeration<Integer> enumArray = null;
CollectionUtils.addAll(list, enumArray);
}
@Test(expected = NullPointerException.class)
public void testAddAllNullColl5() {
final List<Integer> list = new ArrayList<>();
final Integer[] array = null;
CollectionUtils.addAll(list, array);
}
@Test(expected = IndexOutOfBoundsException.class) @Test(expected = IndexOutOfBoundsException.class)
public void getNegative() { public void getNegative() {
CollectionUtils.get((Object) collectionA, -3); CollectionUtils.get((Object) collectionA, -3);
@ -1793,6 +2028,11 @@ public class CollectionUtilsTest extends MockTestCase {
assertEquals(collectionA, Arrays.asList(a)); assertEquals(collectionA, Arrays.asList(a));
} }
@Test(expected = NullPointerException.class)
public void testReverseArrayNull() {
CollectionUtils.reverseArray(null);
}
@Test @Test
public void extractSingleton() { public void extractSingleton() {
ArrayList<String> coll = null; ArrayList<String> coll = null;
@ -1825,6 +2065,11 @@ public class CollectionUtilsTest extends MockTestCase {
expect(iterator.next()).andReturn(t); expect(iterator.next()).andReturn(t);
} }
@Test(expected=NullPointerException.class)
public void collateException0() {
CollectionUtils.collate(null, collectionC);
}
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void collateException1() { public void collateException1() {
CollectionUtils.collate(collectionA, null); CollectionUtils.collate(collectionA, null);