This commit is contained in:
Gary Gregory 2019-11-01 11:36:38 -04:00
commit 843429515b
3 changed files with 221 additions and 1 deletions

View File

@ -1791,6 +1791,72 @@ public class CollectionUtils {
return list;
}
/**
* 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>

View File

@ -1472,6 +1472,115 @@ public class CollectionUtilsTest extends MockTestCase {
} // this is what we want
}
@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<>();

View File

@ -422,7 +422,52 @@ public class CircularFifoQueueTest<E> extends AbstractQueueTest<E> {
}
}
@Override
public void testAddNull() {
final CircularFifoQueue<E> b = new CircularFifoQueue<>(2);
try {
b.add(null);
fail();
} catch (final NullPointerException ex) {
return;
}
fail();
}
public void testDefaultSizeAndGetError1() {
final CircularFifoQueue<E> fifo = new CircularFifoQueue<>();
assertEquals(32,fifo.maxSize());
fifo.add((E) "1");
fifo.add((E) "2");
fifo.add((E) "3");
fifo.add((E) "4");
fifo.add((E) "5");
assertEquals(5,fifo.size());
try {
fifo.get(5);
} catch (final NoSuchElementException ex) {
return;
}
fail();
}
public void testDefaultSizeAndGetError2() {
final CircularFifoQueue<E> fifo = new CircularFifoQueue<>();
assertEquals(32,fifo.maxSize());
fifo.add((E) "1");
fifo.add((E) "2");
fifo.add((E) "3");
fifo.add((E) "4");
fifo.add((E) "5");
assertEquals(5,fifo.size());
try {
fifo.get(-2);
} catch (final NoSuchElementException ex) {
return;
}
fail();
}
@Override
public String getCompatibilityVersion() {
return "4";
}