Skip to content

Commit

Permalink
Merge branch 'master' of github.com:apache/commons-collections
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 committed Nov 5, 2019
2 parents 4ff7813 + 4551c3d commit 188c771
Show file tree
Hide file tree
Showing 14 changed files with 704 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@
<action dev="ggregory" type="add" due-to="dota17">
Add a test case for AbstractMultiValuedMapTest testToString() #100.
</action>
<action dev="ggregory" type="update" due-to="dota17">
Update some map test cases #104.
</action>
<action dev="ggregory" type="update" due-to="dota17">
Add three test cases in UnmodifiableQueueTest #105.
</action>
<action issue="COLLECTIONS-674" dev="ggregory" type="add" due-to="dota17">
Add CollectionUtils removeRange, removeCount #91.
</action>
<action dev="ggregory" type="add" due-to="dota17">
Add a test case AbstractMultiValuedMapTest#testMultiValuedMapIterator() #108.
</action>
<action dev="ggregory" type="update" due-to="dota17">
Remove the parentheses in the error message in CircularFifoQueue #107.
</action>
<action dev="ggregory" type="add" due-to="dota17">
Add junit for getWithNull and subList in LazyListTest; #103.
</action>
<action dev="ggregory" type="add" due-to="dota17">
Add test cases to ArrayListValuedHashMapTest; #106.
</action>
<action dev="ggregory" type="add" due-to="dota17">
Add test cases in UnmodifiableMultiValuedMapTest; #102.
</action>
</release>
<release version="4.4" date="2019-07-05" description="Maintenance release.">
<action issue="COLLECTIONS-710" dev="ggregory" type="fix" due-to="Yu Shi, Gary Gregory">
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/apache/commons/collections4/CollectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,72 @@ public static <E> Collection<E> retainAll(final Iterable<E> collection,
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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public E get(final int index) {
final int sz = size();
if (index < 0 || index >= sz) {
throw new NoSuchElementException(
String.format("The specified index (%1$d) is outside the available range [0, %2$d)",
String.format("The specified index %1$d is outside the available range [0, %2$d)",
Integer.valueOf(index), Integer.valueOf(sz)));
}

Expand Down
109 changes: 109 additions & 0 deletions src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,115 @@ public void testRetainAll() {
} // 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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,64 @@ public void testListAllowsMutationOfUnderlyingCollection() {
Assert.assertEquals("Modifying an the underlying list is allowed",
sizeBefore + 1, fixedSizeList.size());
}

private FixedSizeList<String> initFixedSizeList() {
List<String> decoratedList = new ArrayList<>();
decoratedList.add("item 1");
decoratedList.add("item 2");
//
return FixedSizeList.fixedSizeList(decoratedList);
}

public void testAdd() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();

try {
fixedSizeList.add(2,"New Value");
fail();
} catch (UnsupportedOperationException ex) {}
}


public void testAddAll() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();

List<String> addList = new ArrayList<>();
addList.add("item 3");
addList.add("item 4");

try {
fixedSizeList.addAll(2, addList);
fail();
} catch (UnsupportedOperationException ex) {}
}

public void testRemove() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();

try {
fixedSizeList.remove(1);
fail();
} catch (UnsupportedOperationException ex) {}
}

public void testSubList() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();

List<String> subFixedSizeList = fixedSizeList.subList(1, 1);
Assert.assertNotNull(subFixedSizeList);
Assert.assertEquals(0, subFixedSizeList.size());
}

public void testIsFull() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();

Assert.assertTrue(fixedSizeList.isFull());
}

public void testMaxSize() {
FixedSizeList<String> fixedSizeList = initFixedSizeList();

Assert.assertEquals(2, fixedSizeList.maxSize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,54 @@ public void testCreateNullGapsWithTransformer() {
assertNotNull(fourthElement);
}

public void testGetWithNull() {
final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
final Transformer<Integer, LocalDateTime> transformer = input -> LocalDateTime.now().withHour(hours.get(input));
List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), transformer);
LocalDateTime fourthElement = list.get(3);
assertFalse(list.isEmpty());
assertNotNull(fourthElement);
list.remove(3);
list.add(3,null);
fourthElement = list.get(3);
assertNotNull(fourthElement);
}

public void testSubListWitheFactory() {
final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
LocalDateTime fourthElement = list.get(3);
assertFalse(list.isEmpty());
assertNotNull(fourthElement);
testSubList(list);
}

public void testSubListWithTransformer() {
final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
final Transformer<Integer, LocalDateTime> transformer = input -> LocalDateTime.now().withHour(hours.get(input));
List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), transformer);
LocalDateTime fourthElement = list.get(3);
assertFalse(list.isEmpty());
assertNotNull(fourthElement);
testSubList(list);
}

private void testSubList(List<LocalDateTime> list) {
List<LocalDateTime> subList = list.subList(1, 3);
assertFalse(subList.isEmpty());
assertNotNull(subList);
assertEquals(2, subList.size());

subList = list.subList(0, 1);
assertFalse(subList.isEmpty());
assertEquals(1, subList.size());

subList = list.subList(1, 1);
assertTrue(subList.isEmpty());

subList = list.subList(0, list.size());
assertFalse(subList.isEmpty());
assertEquals(list.size(), subList.size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ public void testLegalAddAll() {
list.contains("three"));
}

public void testSubList() {
final List<E> list = makeTestList();
list.add((E) "zero");
//subList without any element of list
List<E> subList = list.subList(0, 0);
assertNotNull(subList);
assertEquals(0, subList.size());

//subList with one element oif list
subList = list.subList(0, 1);
assertEquals(1, subList.size());

final List<E> elements = new ArrayList<>();
elements.add((E) "one");
elements.add((E) "two");
elements.add((E) "three");
list.addAll(1, elements);
//subList with all elements of list
subList = list.subList(0, list.size());
assertEquals(list.size(), subList.size());
}

@Override
public String getCompatibilityVersion() {
return "4";
Expand Down
Loading

0 comments on commit 188c771

Please sign in to comment.