Repository: commons-collections Updated Branches: refs/heads/master 5723ce72d -> 611c73889
[COLLECTIONS-669] Update org.apache.commons.collections4.CollectionUtils.addAll(Collection<C>, C[]) to addAll(Collection<C>, C...). Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-collections/commit/611c7388 Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/611c7388 Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/611c7388 Branch: refs/heads/master Commit: 611c73889b772dab7315c952902d5c649635126b Parents: 5723ce7 Author: Gary Gregory <ggreg...@apache.org> Authored: Tue Jan 2 15:54:09 2018 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Tue Jan 2 15:54:09 2018 -0700 ---------------------------------------------------------------------- src/changes/changes.xml | 7 +- .../apache/commons/collections4/ArrayUtils.java | 133 +++++++++++++++++++ .../commons/collections4/CollectionUtils.java | 29 ++++ .../collections4/CollectionUtilsTest.java | 36 ++++- 4 files changed, 202 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-collections/blob/611c7388/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index fdd5c4f..eb6ab22 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -69,8 +69,11 @@ <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. </action> - <action issue="COLLECTIONS-669" dev="ggregory" type="update" due-to="Gary Gregory"> - Update org.apache.commons.collections4.CollectionUtils.addAll(Collection<C>, C[]) to addAll(Collection<C>, C...) + <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...). + </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> </release> <release version="4.1" date="2015-11-28" description="This is a security and minor release."> http://git-wip-us.apache.org/repos/asf/commons-collections/blob/611c7388/src/main/java/org/apache/commons/collections4/ArrayUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/collections4/ArrayUtils.java b/src/main/java/org/apache/commons/collections4/ArrayUtils.java new file mode 100644 index 0000000..696fd89 --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/ArrayUtils.java @@ -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; + } + +} http://git-wip-us.apache.org/repos/asf/commons-collections/blob/611c7388/src/main/java/org/apache/commons/collections4/CollectionUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index 0d93e6c..55be039 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -402,6 +402,35 @@ public class CollectionUtils { * @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. + * <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 2.1 * @see #intersection */ http://git-wip-us.apache.org/repos/asf/commons-collections/blob/611c7388/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java index d61aa42..27fbd26 100644 --- a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java @@ -319,7 +319,7 @@ public class CollectionUtilsTest extends MockTestCase { } @Test - public void containsAny() { + public void containsAnyInCollection() { final Collection<String> empty = new ArrayList<>(0); final Collection<String> one = new ArrayList<>(1); one.add("1"); @@ -348,6 +348,40 @@ public class CollectionUtilsTest extends MockTestCase { } @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 public void union() { final Collection<Integer> col = CollectionUtils.union(iterableA, iterableC); final Map<Integer, Integer> freq = CollectionUtils.getCardinalityMap(col);