[COLLECTIONS-669] Update
org.apache.commons.collections4.CollectionUtils.addAll(Collection<C>, C[]) to addAll(Collection<C>, C...).
This commit is contained in:
parent
5723ce72d0
commit
611c73889b
|
@ -69,8 +69,11 @@
|
||||||
<action issue="COLLECTIONS-666" dev="ggregory" type="update" due-to="BELUGA BEHR">
|
<action issue="COLLECTIONS-666" dev="ggregory" type="update" due-to="BELUGA BEHR">
|
||||||
org.apache.commons.collections4.ListUtils.union(List, List) should pre-allocate result list.
|
org.apache.commons.collections4.ListUtils.union(List, List) should pre-allocate result list.
|
||||||
</action>
|
</action>
|
||||||
<action issue="COLLECTIONS-669" dev="ggregory" type="update" due-to="Gary Gregory">
|
<action issue="COLLECTIONS-669" dev="ggregory" type="update" due-to="BELUGA BEHR, Gary Gregory">
|
||||||
Update org.apache.commons.collections4.CollectionUtils.addAll(Collection<C>, C[]) to addAll(Collection<C>, C...)
|
Update org.apache.commons.collections4.CollectionUtils.addAll(Collection<C>, C[]) to addAll(Collection<C>, C...).
|
||||||
|
</action>
|
||||||
|
<action issue="COLLECTIONS-668" dev="ggregory" type="add" due-to="Gary Gregory">
|
||||||
|
Add CollectionUtils containsAny method for primitive array: org.apache.commons.collections4.CollectionUtils.containsAny(Collection<?>, T...).
|
||||||
</action>
|
</action>
|
||||||
</release>
|
</release>
|
||||||
<release version="4.1" date="2015-11-28" description="This is a security and minor release.">
|
<release version="4.1" date="2015-11-28" description="This is a security and minor release.">
|
||||||
|
|
|
@ -0,0 +1,133 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.collections4;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Operations on arrays, primitive arrays (like {@code int[]}) and primitive wrapper arrays (like {@code Integer[]}).
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* This class tries to handle {@code null} input gracefully. An exception will not be thrown for a {@code null} array
|
||||||
|
* input. However, an Object array that contains a {@code null} element may throw an exception. Each method documents
|
||||||
|
* its behaviour.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* Package private, might move to an internal package if this needs to be public.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* #ThreadSafe#
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @since 4.2 (Copied from Apache Commons Lang.)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class ArrayUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The index value when an element is not found in a list or array: {@code -1}. This value is returned by methods in
|
||||||
|
* this class and can also be used in comparisons with values returned by various method from
|
||||||
|
* {@link java.util.List}.
|
||||||
|
*/
|
||||||
|
static final int INDEX_NOT_FOUND = -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Checks if the object is in the given array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* The method returns {@code false} if a {@code null} array is passed in.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to search through
|
||||||
|
* @param objectToFind
|
||||||
|
* the object to find
|
||||||
|
* @return {@code true} if the array contains the object
|
||||||
|
*/
|
||||||
|
static boolean contains(final Object[] array, final Object objectToFind) {
|
||||||
|
return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Finds the index of the given object in the array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to search through for the object, may be {@code null}
|
||||||
|
* @param objectToFind
|
||||||
|
* the object to find, may be {@code null}
|
||||||
|
* @return the index of the object within the array, {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or
|
||||||
|
* {@code null} array input
|
||||||
|
*/
|
||||||
|
static <T> int indexOf(final T[] array, final Object objectToFind) {
|
||||||
|
return indexOf(array, objectToFind, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Finds the index of the given object in the array starting at the given index.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* A negative startIndex is treated as zero. A startIndex larger than the array length will return
|
||||||
|
* {@link #INDEX_NOT_FOUND} ({@code -1}).
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param array
|
||||||
|
* the array to search through for the object, may be {@code null}
|
||||||
|
* @param objectToFind
|
||||||
|
* the object to find, may be {@code null}
|
||||||
|
* @param startIndex
|
||||||
|
* the index to start searching at
|
||||||
|
* @return the index of the object within the array starting at the index, {@link #INDEX_NOT_FOUND} ({@code -1}) if
|
||||||
|
* not found or {@code null} array input
|
||||||
|
*/
|
||||||
|
static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
|
||||||
|
if (array == null) {
|
||||||
|
return INDEX_NOT_FOUND;
|
||||||
|
}
|
||||||
|
if (startIndex < 0) {
|
||||||
|
startIndex = 0;
|
||||||
|
}
|
||||||
|
if (objectToFind == null) {
|
||||||
|
for (int i = startIndex; i < array.length; i++) {
|
||||||
|
if (array[i] == null) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (int i = startIndex; i < array.length; i++) {
|
||||||
|
if (objectToFind.equals(array[i])) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return INDEX_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -393,6 +393,35 @@ public class CollectionUtils {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns <code>true</code> iff at least one element is in both collections.
|
||||||
|
* <p>
|
||||||
|
* In other words, this method returns <code>true</code> iff the
|
||||||
|
* {@link #intersection} of <i>coll1</i> and <i>coll2</i> is not empty.
|
||||||
|
*
|
||||||
|
* @param coll1 the first collection, must not be null
|
||||||
|
* @param coll2 the second collection, must not be null
|
||||||
|
* @return <code>true</code> iff the intersection of the collections is non-empty
|
||||||
|
* @since 4.2
|
||||||
|
* @see #intersection
|
||||||
|
*/
|
||||||
|
public static <T> boolean containsAny(final Collection<?> coll1, @SuppressWarnings("unchecked") final T... coll2) {
|
||||||
|
if (coll1.size() < coll2.length) {
|
||||||
|
for (final Object aColl1 : coll1) {
|
||||||
|
if (ArrayUtils.contains(coll2, aColl1)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (final Object aColl2 : coll2) {
|
||||||
|
if (coll1.contains(aColl2)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns <code>true</code> iff at least one element is in both collections.
|
* Returns <code>true</code> iff at least one element is in both collections.
|
||||||
* <p>
|
* <p>
|
||||||
|
|
|
@ -319,7 +319,7 @@ public class CollectionUtilsTest extends MockTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void containsAny() {
|
public void containsAnyInCollection() {
|
||||||
final Collection<String> empty = new ArrayList<>(0);
|
final Collection<String> empty = new ArrayList<>(0);
|
||||||
final Collection<String> one = new ArrayList<>(1);
|
final Collection<String> one = new ArrayList<>(1);
|
||||||
one.add("1");
|
one.add("1");
|
||||||
|
@ -347,6 +347,40 @@ public class CollectionUtilsTest extends MockTestCase {
|
||||||
assertTrue("containsAny({},{}) should return false.", !CollectionUtils.containsAny(empty, empty));
|
assertTrue("containsAny({},{}) should return false.", !CollectionUtils.containsAny(empty, empty));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void containsAnyInArray() {
|
||||||
|
final Collection<String> empty = new ArrayList<>(0);
|
||||||
|
final String[] emptyArr = {};
|
||||||
|
final Collection<String> one = new ArrayList<>(1);
|
||||||
|
one.add("1");
|
||||||
|
final String[] oneArr = {"1"};
|
||||||
|
final Collection<String> two = new ArrayList<>(1);
|
||||||
|
two.add("2");
|
||||||
|
final String[] twoArr = {"2"};
|
||||||
|
final Collection<String> three = new ArrayList<>(1);
|
||||||
|
three.add("3");
|
||||||
|
final String[] threeArr = {"3"};
|
||||||
|
final Collection<String> odds = new ArrayList<>(2);
|
||||||
|
odds.add("1");
|
||||||
|
odds.add("3");
|
||||||
|
final String[] oddsArr = {"1", "3"};
|
||||||
|
|
||||||
|
assertTrue("containsAny({1},{1,3}) should return true.", CollectionUtils.containsAny(one, oddsArr));
|
||||||
|
assertTrue("containsAny({1,3},{1}) should return true.", CollectionUtils.containsAny(odds, oneArr));
|
||||||
|
assertTrue("containsAny({3},{1,3}) should return true.", CollectionUtils.containsAny(three, oddsArr));
|
||||||
|
assertTrue("containsAny({1,3},{3}) should return true.", CollectionUtils.containsAny(odds, threeArr));
|
||||||
|
assertTrue("containsAny({2},{2}) should return true.", CollectionUtils.containsAny(two, twoArr));
|
||||||
|
assertTrue("containsAny({1,3},{1,3}) should return true.", CollectionUtils.containsAny(odds, oddsArr));
|
||||||
|
|
||||||
|
assertTrue("containsAny({2},{1,3}) should return false.", !CollectionUtils.containsAny(two, oddsArr));
|
||||||
|
assertTrue("containsAny({1,3},{2}) should return false.", !CollectionUtils.containsAny(odds, twoArr));
|
||||||
|
assertTrue("containsAny({1},{3}) should return false.", !CollectionUtils.containsAny(one, threeArr));
|
||||||
|
assertTrue("containsAny({3},{1}) should return false.", !CollectionUtils.containsAny(three, oneArr));
|
||||||
|
assertTrue("containsAny({1,3},{}) should return false.", !CollectionUtils.containsAny(odds, emptyArr));
|
||||||
|
assertTrue("containsAny({},{1,3}) should return false.", !CollectionUtils.containsAny(empty, oddsArr));
|
||||||
|
assertTrue("containsAny({},{}) should return false.", !CollectionUtils.containsAny(empty, emptyArr));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void union() {
|
public void union() {
|
||||||
final Collection<Integer> col = CollectionUtils.union(iterableA, iterableC);
|
final Collection<Integer> col = CollectionUtils.union(iterableA, iterableC);
|
||||||
|
|
Loading…
Reference in New Issue