/** * Gets an iterator that loops continuously over the supplied list. * <p> * The iterator will only stop looping if the remove method is called * enough times to empty the list, or if the list is empty to start with. * * @param list the list to iterate over, not null * @return a new looping iterator * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ public static ResettableListIterator loopingListIterator(List list) { if (list == null) { throw new NullPointerException("List must not be null"); } return new LoopingListIterator(list); }
/** * Gets an iterator that loops continuously over the supplied list. * <p> * The iterator will only stop looping if the remove method is called * enough times to empty the list, or if the list is empty to start with. * * @param list the list to iterate over, not null * @return a new looping iterator * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ public static ResettableListIterator loopingListIterator(List list) { if (list == null) { throw new NullPointerException("List must not be null"); } return new LoopingListIterator(list); }
/** * Tests constructor exception. */ public void testConstructorEx() throws Exception { try { new LoopingListIterator(null); fail(); } catch (NullPointerException ex) { } }
/** * Tests whether an empty looping list iterator works. */ public void testLooping0() throws Exception { List list = new ArrayList(); LoopingListIterator loop = new LoopingListIterator(list); assertFalse(loop.hasNext()); assertFalse(loop.hasPrevious()); try { loop.next(); fail(); } catch (NoSuchElementException ex) { } try { loop.previous(); fail(); } catch (NoSuchElementException ex) { } }
/** * Tests jogging back and forth between two elements over the * begin/end boundary of the list. */ public void testJoggingOverBoundary() { List list = new ArrayList(Arrays.asList(new String[] { "a", "b" })); LoopingListIterator loop = new LoopingListIterator(list); // <a> b // Try jogging back and forth between the elements, but not // over the begin/end boundary. assertEquals("b", loop.previous()); // a <b> assertEquals("b", loop.next()); // <a> b assertEquals("b", loop.previous()); // a <b> assertEquals("a", loop.previous()); // <a> b assertEquals("a", loop.next()); // a <b> assertEquals("a", loop.previous()); // <a> b }
/** * Tests jogging back and forth between two elements, but not over * the begin/end boundary of the list. */ public void testJoggingNotOverBoundary() { List list = new ArrayList(Arrays.asList(new String[] { "a", "b" })); LoopingListIterator loop = new LoopingListIterator(list); // <a> b // Try jogging back and forth between the elements, but not // over the begin/end boundary. loop.reset(); assertEquals("a", loop.next()); // a <b> assertEquals("a", loop.previous()); // <a> b assertEquals("a", loop.next()); // a <b> assertEquals("b", loop.next()); // <a> b assertEquals("b", loop.previous()); // a <b> assertEquals("b", loop.next()); // <a> b }
/** * Tests removing an element from a wrapped ArrayList. */ public void testRemovingElementsAndIteratingForward() { List list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c" })); LoopingListIterator loop = new LoopingListIterator(list); // <a> b c assertTrue(loop.hasNext()); assertEquals("a", loop.next()); // a <b> c loop.remove(); // <b> c assertEquals(2, list.size()); assertTrue(loop.hasNext()); assertEquals("b", loop.next()); // b <c> loop.remove(); // <c> assertEquals(1, list.size()); assertTrue(loop.hasNext()); assertEquals("c", loop.next()); // <c> loop.remove(); // --- assertEquals(0, list.size()); assertFalse(loop.hasNext()); try { loop.next(); fail(); } catch (NoSuchElementException ex) { } }
/** * Tests removing an element from a wrapped ArrayList. */ public void testRemovingElementsAndIteratingBackwards() { List list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c" })); LoopingListIterator loop = new LoopingListIterator(list); // <a> b c assertTrue(loop.hasPrevious()); assertEquals("c", loop.previous()); // a b <c> loop.remove(); // <a> b assertEquals(2, list.size()); assertTrue(loop.hasPrevious()); assertEquals("b", loop.previous()); // a <b> loop.remove(); // <a> assertEquals(1, list.size()); assertTrue(loop.hasPrevious()); assertEquals("a", loop.previous()); // <a> loop.remove(); // --- assertEquals(0, list.size()); assertFalse(loop.hasPrevious()); try { loop.previous(); fail(); } catch (NoSuchElementException ex) { } }
/** * Tests using the set method to change elements. */ public void testSet() { List list = new ArrayList(Arrays.asList(new String[] { "q", "r", "z" })); LoopingListIterator loop = new LoopingListIterator(list); // <q> r z assertEquals("z", loop.previous()); // q r <z> loop.set("c"); // q r <c> loop.reset(); // <q> r c assertEquals("q", loop.next()); // q <r> c loop.set("a"); // a <r> c assertEquals("r", loop.next()); // a r <c> loop.set("b"); // a b <c> loop.reset(); // <a> b c assertEquals("a", loop.next()); // a <b> c assertEquals("b", loop.next()); // a b <c> assertEquals("c", loop.next()); // <a> b c }
/** * Tests whether a looping list iterator works on a list with only * one element. */ public void testLooping1() throws Exception { List list = new ArrayList(Arrays.asList(new String[] { "a" })); LoopingListIterator loop = new LoopingListIterator(list); // <a> assertTrue(loop.hasNext()); assertEquals("a", loop.next()); // <a> assertTrue(loop.hasNext()); assertEquals("a", loop.next()); // <a> assertTrue(loop.hasNext()); assertEquals("a", loop.next()); // <a> assertTrue(loop.hasPrevious()); assertEquals("a", loop.previous()); // <a> assertTrue(loop.hasPrevious()); assertEquals("a", loop.previous()); // <a> assertTrue(loop.hasPrevious()); assertEquals("a", loop.previous()); // <a> }
/** * Tests whether a looping list iterator works on a list with two * elements. */ public void testLooping2() throws Exception { List list = new ArrayList(Arrays.asList(new String[] { "a", "b" })); LoopingListIterator loop = new LoopingListIterator(list); // <a> b assertTrue(loop.hasNext()); assertEquals("a", loop.next()); // a <b> assertTrue(loop.hasNext()); assertEquals("b", loop.next()); // <a> b assertTrue(loop.hasNext()); assertEquals("a", loop.next()); // a <b> // Reset the iterator and try using previous. loop.reset(); // <a> b assertTrue(loop.hasPrevious()); assertEquals("b", loop.previous()); // a <b> assertTrue(loop.hasPrevious()); assertEquals("a", loop.previous()); // <a> b assertTrue(loop.hasPrevious()); assertEquals("b", loop.previous()); // a <b> }
/** * Tests the reset method. */ public void testReset() { List list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c" })); LoopingListIterator loop = new LoopingListIterator(list); // <a> b c assertEquals("a", loop.next()); // a <b> c assertEquals("b", loop.next()); // a b <c> loop.reset(); // <a> b c assertEquals("a", loop.next()); // a <b> c loop.reset(); // <a> b c assertEquals("a", loop.next()); // a <b> c assertEquals("b", loop.next()); // a b <c> assertEquals("c", loop.next()); // <a> b c loop.reset(); // <a> b c assertEquals("c", loop.previous()); // a b <c> assertEquals("b", loop.previous()); // a <b> c loop.reset(); // <a> b c assertEquals("c", loop.previous()); // a b <c> loop.reset(); // <a> b c assertEquals("c", loop.previous()); // a b <c> assertEquals("b", loop.previous()); // a <b> c assertEquals("a", loop.previous()); // <a> b c }
/** * Tests nextIndex and previousIndex. */ public void testNextAndPreviousIndex() { List list = new ArrayList(Arrays.asList(new String[] { "a", "b", "c" })); LoopingListIterator loop = new LoopingListIterator(list); // <a> b c assertEquals(0, loop.nextIndex()); assertEquals(2, loop.previousIndex()); assertEquals("a", loop.next()); // a <b> c assertEquals(1, loop.nextIndex()); assertEquals(0, loop.previousIndex()); assertEquals("a", loop.previous()); // <a> b c assertEquals(0, loop.nextIndex()); assertEquals(2, loop.previousIndex()); assertEquals("c", loop.previous()); // a b <c> assertEquals(2, loop.nextIndex()); assertEquals(1, loop.previousIndex()); assertEquals("b", loop.previous()); // a <b> c assertEquals(1, loop.nextIndex()); assertEquals(0, loop.previousIndex()); assertEquals("a", loop.previous()); // <a> b c assertEquals(0, loop.nextIndex()); assertEquals(2, loop.previousIndex()); }
LoopingListIterator loop = new LoopingListIterator(list); // <b> e f loop = new LoopingListIterator(list); // <b> e f
/** * Gets an iterator that loops continuously over the supplied list. * <p> * The iterator will only stop looping if the remove method is called * enough times to empty the list, or if the list is empty to start with. * * @param list the list to iterate over, not null * @return a new looping iterator * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ public static ResettableListIterator loopingListIterator(List list) { if (list == null) { throw new NullPointerException("List must not be null"); } return new LoopingListIterator(list); }
/** * Gets an iterator that loops continuously over the supplied list. * <p> * The iterator will only stop looping if the remove method is called * enough times to empty the list, or if the list is empty to start with. * * @param list the list to iterate over, not null * @return a new looping iterator * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ public static ResettableListIterator loopingListIterator(List list) { if (list == null) { throw new NullPointerException("List must not be null"); } return new LoopingListIterator(list); }
/** * Gets an iterator that loops continuously over the supplied list. * <p> * The iterator will only stop looping if the remove method is called * enough times to empty the list, or if the list is empty to start with. * * @param list the list to iterate over, not null * @return a new looping iterator * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ public static ResettableListIterator loopingListIterator(List list) { if (list == null) { throw new NullPointerException("List must not be null"); } return new LoopingListIterator(list); }
/** * Gets an iterator that loops continuously over the supplied list. * <p> * The iterator will only stop looping if the remove method is called * enough times to empty the list, or if the list is empty to start with. * * @param list the list to iterate over, not null * @return a new looping iterator * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ public static ResettableListIterator loopingListIterator(List list) { if (list == null) { throw new NullPointerException("List must not be null"); } return new LoopingListIterator(list); }
/** * Gets an iterator that loops continuously over the supplied list. * <p> * The iterator will only stop looping if the remove method is called * enough times to empty the list, or if the list is empty to start with. * * @param list the list to iterate over, not null * @return a new looping iterator * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ public static ResettableListIterator loopingListIterator(List list) { if (list == null) { throw new NullPointerException("List must not be null"); } return new LoopingListIterator(list); }
/** * Gets an iterator that loops continuously over the supplied list. * <p> * The iterator will only stop looping if the remove method is called * enough times to empty the list, or if the list is empty to start with. * * @param list the list to iterate over, not null * @return a new looping iterator * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ public static ResettableListIterator loopingListIterator(List list) { if (list == null) { throw new NullPointerException("List must not be null"); } return new LoopingListIterator(list); }