This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push: new c423a99 [COLLECTIONS-674] Add drain method to CollectionUtils (#91) c423a99 is described below commit c423a9964fa8e17028b378abbe47a50907e6d189 Author: dota17 <50514813+dot...@users.noreply.github.com> AuthorDate: Fri Nov 1 23:36:24 2019 +0800 [COLLECTIONS-674] Add drain method to CollectionUtils (#91) * add removeRange&removeCount to CollectionUtils * modify removeRange&removeCount --- .../commons/collections4/CollectionUtils.java | 66 +++++++++++++ .../commons/collections4/CollectionUtilsTest.java | 109 +++++++++++++++++++++ 2 files changed, 175 insertions(+) diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index 2e6a79f..8afcba2 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -1792,6 +1792,72 @@ public class CollectionUtils { } /** + * Removes elements whose index are between startIndex, inclusive and endIndex, + * exclusive in the collection and returns them. + * This method modifies the input collections. + * + * @param <E> the type of object the {@link Collection} contains + * @param input the collection will be operated, can't be null + * @param startIndex the start index (inclusive) to remove element, can't be less than 0 + * @param endIndex the end index (exclusive) to remove, can't be less than startIndex + * @return collection of elements that removed from the input collection + * @since 4.5 + */ + public static <E> Collection<E> removeRange(final Collection<E> input, final int startIndex, final int endIndex) { + if (null == input) { + throw new IllegalArgumentException("The collection can't be null."); + } + if (endIndex < startIndex) { + throw new IllegalArgumentException("The end index can't be less than the start index."); + } + if (input.size() < endIndex) { + throw new IndexOutOfBoundsException("The end index can't be greater than the size of collection."); + } + return CollectionUtils.removeCount(input, startIndex, endIndex - startIndex); + } + + /** + * Removes the specified number of elements from the start index in the collection and returns them. + * This method modifies the input collections. + * + * @param <E> the type of object the {@link Collection} contains + * @param input the collection will be operated, can't be null + * @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 + * @return collection of elements that removed from the input collection + * @since 4.5 + */ + public static <E> Collection<E> removeCount(final Collection<E> input, int startIndex, int count) { + if (null == input) { + throw new IllegalArgumentException("The collection can't be null."); + } + if (startIndex < 0) { + throw new IndexOutOfBoundsException("The start index can't be less than 0."); + } + if (count < 0) { + throw new IndexOutOfBoundsException("The count can't be less than 0."); + } + if (input.size() < startIndex + count) { + throw new IndexOutOfBoundsException( + "The sum of start index and count can't be greater than the size of collection."); + } + + Collection<E> result = new ArrayList<E>(count); + Iterator<E> iterator = input.iterator(); + while (count > 0) { + if (startIndex > 0) { + startIndex = startIndex - 1; + iterator.next(); + continue; + } + count = count - 1; + result.add(iterator.next()); + iterator.remove(); + } + return result; + } + + /** * Removes the elements in <code>remove</code> from <code>collection</code>. That is, this * method returns a collection containing all the elements in <code>c</code> * that are not in <code>remove</code>. The cardinality of an element <code>e</code> diff --git a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java index 2ef3030..6f4a5b1 100644 --- a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java @@ -1473,6 +1473,115 @@ public class CollectionUtilsTest extends MockTestCase { } @Test + public void testRemoveRange() { + List<Integer> list = new ArrayList<>(); + list.add(1); + Collection<Integer> result = CollectionUtils.removeRange(list, 0, 0); + assertEquals(1, list.size()); + assertEquals(0, result.size()); + + list.add(2); + list.add(3); + result = CollectionUtils.removeRange(list, 1, 3); + assertEquals(1, list.size()); + assertEquals(1, (int) list.get(0)); + assertEquals(2, result.size()); + assertTrue(result.contains(2)); + assertTrue(result.contains(3)); + } + + @Test(expected=IllegalArgumentException.class) + public void testRemoveRangeNull() { + Collection<Integer> list = null; + Collection result = CollectionUtils.removeRange(list, 0, 0); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testRemoveRangeStartIndexNegative() { + Collection<Integer> list = new ArrayList<>(); + list.add(1); + Collection result = CollectionUtils.removeRange(list, -1, 1); + } + + @Test(expected=IllegalArgumentException.class) + public void testRemoveRangeEndIndexNegative() { + Collection<Integer> list = new ArrayList<>(); + list.add(1); + Collection result = CollectionUtils.removeRange(list, 0, -1); + } + + @Test(expected=IllegalArgumentException.class) + public void testRemoveRangeEndLowStart() { + Collection<Integer> list = new ArrayList<>(); + list.add(1); + list.add(2); + Collection result = CollectionUtils.removeRange(list, 1, 0); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testRemoveRangeWrongEndIndex() { + Collection<Integer> list = new ArrayList<>(); + list.add(1); + Collection result = CollectionUtils.removeRange(list, 0, 2); + } + + @Test + public void testRemoveCount() { + List<Integer> list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + Collection<Integer> result = CollectionUtils.removeCount(list, 0, 0); + assertEquals(4, list.size()); + assertEquals(0, result.size()); + + result = CollectionUtils.removeCount(list, 0, 1); + assertEquals(3, list.size()); + assertEquals(2, (int) list.get(0)); + assertEquals(1, result.size()); + assertTrue(result.contains(1)); + + list.add(5); + list.add(6); + result = CollectionUtils.removeCount(list, 1, 3); + + assertEquals(2, list.size()); + assertEquals(2, (int) list.get(0)); + assertEquals(6, (int) list.get(1)); + assertEquals(3, result.size()); + assertTrue(result.contains(3)); + assertTrue(result.contains(4)); + assertTrue(result.contains(5)); + } + + @Test(expected=IllegalArgumentException.class) + public void testRemoveCountWithNull() { + Collection<Integer> list = null; + Collection result = CollectionUtils.removeCount(list, 0, 1); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testRemoveCountStartNegative() { + Collection<Integer> list = new ArrayList<>(); + Collection result = CollectionUtils.removeCount(list, -1, 1); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testRemoveCountNegative() { + Collection<Integer> list = new ArrayList<>(); + Collection result = CollectionUtils.removeCount(list, 0, -1); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testRemoveCountWrongCount() { + Collection<Integer> list = new ArrayList<>(); + list.add(1); + Collection result = CollectionUtils.removeCount(list, 0, 2); + } + + @Test public void testRemoveAll() { final List<String> base = new ArrayList<>(); base.add("A");