@Override public void absorbContent(ListIterator<Content> contents) { // Fast forward the iterator until after this content. //noinspection StatementWithEmptyBody while (contents.hasPrevious() && contents.previous() != this) {} while (contents.hasPrevious()) { Content content = contents.previous(); if (content instanceof PathContent) { pathContents.add((PathContent) content); contents.remove(); } } }
private static void mergeDate(ListIterator<Vertex> listIterator, Vertex next, Vertex current) { current = Vertex.newTimeInstance(current.realWord + next.realWord); listIterator.previous(); listIterator.previous(); listIterator.set(current); listIterator.next(); listIterator.next(); listIterator.remove(); }
/** * Returns the previous object in the list. * <p> * If at the beginning of the list, return the last element. Note * that in this case, traversal to find that element takes linear time. * * @return the object before the last element returned * @throws NoSuchElementException if there are no elements in the list */ public Object previous() { if (list.isEmpty()) { throw new NoSuchElementException( "There are no elements for this iterator to loop on"); } if (iterator.hasPrevious() == false) { Object result = null; while (iterator.hasNext()) { result = iterator.next(); } iterator.previous(); return result; } else { return iterator.previous(); } }
private static DefaultWebFilterChain initChain(List<WebFilter> filters, WebHandler handler) { DefaultWebFilterChain chain = new DefaultWebFilterChain(filters, handler, null, null); ListIterator<? extends WebFilter> iterator = filters.listIterator(filters.size()); while (iterator.hasPrevious()) { chain = new DefaultWebFilterChain(filters, handler, iterator.previous(), chain); } return chain; }
public void testEmptyListIterator() { ListIterator<String> iterator = Iterators.emptyListIterator(); assertFalse(iterator.hasNext()); assertFalse(iterator.hasPrevious()); assertEquals(0, iterator.nextIndex()); assertEquals(-1, iterator.previousIndex()); try { iterator.next(); fail("no exception thrown"); } catch (NoSuchElementException expected) { iterator.previous(); fail("no exception thrown"); } catch (NoSuchElementException expected) {
assertEquals(0, list.size()); assertFalse(list.contains(1)); assertFalse(list.remove((Integer)1)); assertEquals(7, list.size()); assertEquals(7, list.size()); ListIterator<Integer> lit = list.listIterator(7); for (int i = 7; i > 0; i--) { assertEquals(i, lit.previous().intValue());
public void testBugCollections447() { final List treeList = new TreeList(); treeList.add("A"); treeList.add("B"); treeList.add("C"); treeList.add("D"); final ListIterator li = treeList.listIterator(); assertEquals("A", li.next()); assertEquals("B", li.next()); assertEquals("B", li.previous()); li.remove(); // Deletes "B" // previous() after remove() should move to // the element before the one just removed assertEquals("A", li.previous()); } }
private static void assertTransformListIterator(List<String> list) { ListIterator<String> iterator = list.listIterator(1); assertEquals(1, iterator.nextIndex()); assertEquals("2", iterator.next()); assertEquals("3", iterator.next()); assertEquals("4", iterator.next()); assertEquals(4, iterator.nextIndex()); try { assertEquals("4", iterator.previous()); assertEquals("3", iterator.previous()); assertEquals("2", iterator.previous()); assertTrue(iterator.hasPrevious()); assertEquals("1", iterator.previous()); assertFalse(iterator.hasPrevious()); assertEquals(-1, iterator.previousIndex()); try { iterator.previous(); fail("did not detect beginning of list"); } catch (NoSuchElementException expected) {
protected void checkUnexpectedStart(ListIterator<Token> iterator){ if (!iterator.hasPrevious()){ iterator.next(); Token current = iterator.previous(); unexpectedToken(current); } }
private void walkBackward(ListIterator expected, ListIterator testing) { while(expected.hasPrevious()) { assertEquals(expected.nextIndex(),testing.nextIndex()); assertEquals(expected.previousIndex(),testing.previousIndex()); assertTrue(testing.hasPrevious()); assertEquals(expected.previous(),testing.previous()); } }
/** An implementation of {@link List#lastIndexOf(Object)}. */ static int lastIndexOfImpl(List<?> list, @NullableDecl Object element) { if (list instanceof RandomAccess) { return lastIndexOfRandomAccess(list, element); } else { ListIterator<?> listIterator = list.listIterator(list.size()); while (listIterator.hasPrevious()) { if (Objects.equal(element, listIterator.previous())) { return listIterator.nextIndex(); } } return -1; } }
/** * Returns the previous object in the list. * <p> * If at the beginning of the list, return the last element. Note * that in this case, traversal to find that element takes linear time. * * @return the object before the last element returned * @throws NoSuchElementException if there are no elements in the list */ public Object previous() { if (list.isEmpty()) { throw new NoSuchElementException( "There are no elements for this iterator to loop on"); } if (iterator.hasPrevious() == false) { Object result = null; while (iterator.hasNext()) { result = iterator.next(); } iterator.previous(); return result; } else { return iterator.previous(); } }
/** An implementation of {@link List#lastIndexOf(Object)}. */ static int lastIndexOfImpl(List<?> list, @Nullable Object element) { if (list instanceof RandomAccess) { return lastIndexOfRandomAccess(list, element); } else { ListIterator<?> listIterator = list.listIterator(list.size()); while (listIterator.hasPrevious()) { if (Objects.equal(element, listIterator.previous())) { return listIterator.nextIndex(); } } return -1; } }
private void walkLists(List list, ListIterator testing) { ListIterator expected = list.listIterator(); while(expected.hasNext()) { assertEquals(expected.nextIndex(),testing.nextIndex()); assertEquals(expected.previousIndex(),testing.previousIndex()); assertTrue(testing.hasNext()); assertEquals(expected.next(),testing.next()); assertTrue(testing.hasPrevious()); assertEquals(expected.previous(),testing.previous()); assertTrue(testing.hasNext()); assertEquals(expected.next(),testing.next()); for(int i=0;i<list.size();i++) { assertEquals(expected.nextIndex(),testing.nextIndex()); assertEquals(expected.previousIndex(),testing.previousIndex()); assertTrue(expected.hasPrevious()); // if this one fails we've got a logic error in the test assertTrue(testing.hasPrevious()); assertEquals(expected.previous(),testing.previous()); assertTrue(expected.hasPrevious()); // if this one fails we've got a logic error in the test assertTrue(testing.hasPrevious()); assertEquals(expected.previous(),testing.previous()); if(expected.hasPrevious()) { assertEquals(walkdescr.toString(),expected.previous(),testing.previous());
NoSuchElementException.class, () -> x.iterator().next(), () -> x.listIterator().next(), () -> x.listIterator(0).next(), () -> x.listIterator().previous(), () -> x.listIterator(0).previous());
private void nextNextPrevious(ListIterator expected, ListIterator testing) { // calls to next() should change the value returned by previous() // even after previous() has been set by a call to hasPrevious() assertEquals(expected.next(),testing.next()); assertEquals(expected.hasPrevious(),testing.hasPrevious()); Object expecteda = expected.next(); Object testinga = testing.next(); assertEquals(expecteda,testinga); Object expectedb = expected.previous(); Object testingb = testing.previous(); assertEquals(expecteda,expectedb); assertEquals(testinga,testingb); }
// Substitute appropriate type. ArrayList<...> a = new ArrayList<...>(); // Add elements to list. // Generate an iterator. Start just after the last element. ListIterator li = a.listIterator(a.size()); // Iterate in reverse. while(li.hasPrevious()) { System.out.println(li.previous()); }