Applying Fredrik Kjellberg's patch that adds getIteratorIndex() as per COLLECTIONS-289

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@638693 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2008-03-19 03:43:19 +00:00
parent 3ff2649833
commit cfe724a900
2 changed files with 47 additions and 0 deletions

View File

@ -253,6 +253,20 @@ public class CollatingIterator implements Iterator {
it.remove(); it.remove();
} }
/**
* Returns the index of the iterator that returned the last element.
*
* @return the index of the iterator that returned the last element
* @throws IllegalStateException if there is no last returned element
*/
public int getIteratorIndex() {
if (lastReturned == -1) {
throw new IllegalStateException("No value has been returned yet");
}
return lastReturned;
}
// Private Methods // Private Methods
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** /**

View File

@ -104,6 +104,7 @@ public class TestCollatingIterator extends AbstractTestIterator {
for(int i=0;i<evens.size();i++) { for(int i=0;i<evens.size();i++) {
assertTrue(iter.hasNext()); assertTrue(iter.hasNext());
assertEquals(evens.get(i),iter.next()); assertEquals(evens.get(i),iter.next());
assertEquals(0,iter.getIteratorIndex());
} }
assertTrue(!iter.hasNext()); assertTrue(!iter.hasNext());
} }
@ -113,6 +114,7 @@ public class TestCollatingIterator extends AbstractTestIterator {
for(int i=0;i<20;i++) { for(int i=0;i<20;i++) {
assertTrue(iter.hasNext()); assertTrue(iter.hasNext());
assertEquals(new Integer(i),iter.next()); assertEquals(new Integer(i),iter.next());
assertEquals(i % 2,iter.getIteratorIndex());
} }
assertTrue(!iter.hasNext()); assertTrue(!iter.hasNext());
} }
@ -122,6 +124,7 @@ public class TestCollatingIterator extends AbstractTestIterator {
for(int i=0;i<20;i++) { for(int i=0;i<20;i++) {
assertTrue(iter.hasNext()); assertTrue(iter.hasNext());
assertEquals(new Integer(i),iter.next()); assertEquals(new Integer(i),iter.next());
assertEquals((i % 2) == 0 ? 1 : 0,iter.getIteratorIndex());
} }
assertTrue(!iter.hasNext()); assertTrue(!iter.hasNext());
} }
@ -133,8 +136,10 @@ public class TestCollatingIterator extends AbstractTestIterator {
for(int i=0;i<evens.size();i++) { for(int i=0;i<evens.size();i++) {
assertTrue(iter.hasNext()); assertTrue(iter.hasNext());
assertEquals(evens.get(i),iter.next()); assertEquals(evens.get(i),iter.next());
assertEquals(0,iter.getIteratorIndex());
assertTrue(iter.hasNext()); assertTrue(iter.hasNext());
assertEquals(evens.get(i),iter.next()); assertEquals(evens.get(i),iter.next());
assertEquals(1,iter.getIteratorIndex());
} }
assertTrue(!iter.hasNext()); assertTrue(!iter.hasNext());
} }
@ -147,33 +152,61 @@ public class TestCollatingIterator extends AbstractTestIterator {
iter.addIterator(odds.iterator()); iter.addIterator(odds.iterator());
assertEquals(new Integer(0),iter.next()); // even 0 assertEquals(new Integer(0),iter.next()); // even 0
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(1),iter.next()); // fib 1 assertEquals(new Integer(1),iter.next()); // fib 1
assertEquals(0,iter.getIteratorIndex());
assertEquals(new Integer(1),iter.next()); // fib 1 assertEquals(new Integer(1),iter.next()); // fib 1
assertEquals(0,iter.getIteratorIndex());
assertEquals(new Integer(1),iter.next()); // odd 1 assertEquals(new Integer(1),iter.next()); // odd 1
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(2),iter.next()); // fib 2 assertEquals(new Integer(2),iter.next()); // fib 2
assertEquals(0,iter.getIteratorIndex());
assertEquals(new Integer(2),iter.next()); // even 2 assertEquals(new Integer(2),iter.next()); // even 2
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(3),iter.next()); // fib 3 assertEquals(new Integer(3),iter.next()); // fib 3
assertEquals(0,iter.getIteratorIndex());
assertEquals(new Integer(3),iter.next()); // odd 3 assertEquals(new Integer(3),iter.next()); // odd 3
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(4),iter.next()); // even 4 assertEquals(new Integer(4),iter.next()); // even 4
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(5),iter.next()); // fib 5 assertEquals(new Integer(5),iter.next()); // fib 5
assertEquals(0,iter.getIteratorIndex());
assertEquals(new Integer(5),iter.next()); // odd 5 assertEquals(new Integer(5),iter.next()); // odd 5
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(6),iter.next()); // even 6 assertEquals(new Integer(6),iter.next()); // even 6
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(7),iter.next()); // odd 7 assertEquals(new Integer(7),iter.next()); // odd 7
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(8),iter.next()); // fib 8 assertEquals(new Integer(8),iter.next()); // fib 8
assertEquals(0,iter.getIteratorIndex());
assertEquals(new Integer(8),iter.next()); // even 8 assertEquals(new Integer(8),iter.next()); // even 8
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(9),iter.next()); // odd 9 assertEquals(new Integer(9),iter.next()); // odd 9
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(10),iter.next()); // even 10 assertEquals(new Integer(10),iter.next()); // even 10
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(11),iter.next()); // odd 11 assertEquals(new Integer(11),iter.next()); // odd 11
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(12),iter.next()); // even 12 assertEquals(new Integer(12),iter.next()); // even 12
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(13),iter.next()); // fib 13 assertEquals(new Integer(13),iter.next()); // fib 13
assertEquals(0,iter.getIteratorIndex());
assertEquals(new Integer(13),iter.next()); // odd 13 assertEquals(new Integer(13),iter.next()); // odd 13
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(14),iter.next()); // even 14 assertEquals(new Integer(14),iter.next()); // even 14
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(15),iter.next()); // odd 15 assertEquals(new Integer(15),iter.next()); // odd 15
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(16),iter.next()); // even 16 assertEquals(new Integer(16),iter.next()); // even 16
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(17),iter.next()); // odd 17 assertEquals(new Integer(17),iter.next()); // odd 17
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(18),iter.next()); // even 18 assertEquals(new Integer(18),iter.next()); // even 18
assertEquals(1,iter.getIteratorIndex());
assertEquals(new Integer(19),iter.next()); // odd 19 assertEquals(new Integer(19),iter.next()); // odd 19
assertEquals(2,iter.getIteratorIndex());
assertEquals(new Integer(21),iter.next()); // fib 21 assertEquals(new Integer(21),iter.next()); // fib 21
assertEquals(0,iter.getIteratorIndex());
assertTrue(!iter.hasNext()); assertTrue(!iter.hasNext());
} }