PagedIterator implements Iterator<E>, Iterable<E> { @Override public void remove () { if (currentElement == null) { throw new IllegalStateException (); } result = result.subList (index, result.size ()); index = 0; pageable.delete (currentElement); currentElement = null; skip--; } PagedIterator(Pageable<E> pageable, String query);   PagedIterator(Pageable<E> pageable, String query, int skip);   PagedIterator(Pageable<E> pageable, String query, int skip, int page_size); @Override boolean hasNext(); @Override E next(); @Override void remove(); @Override Iterator<E> iterator();  }
@Test public void testRemove () { List<Integer> list = new ArrayList<> (); list.addAll (Arrays.asList (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); List<Integer> removed = Arrays.asList (0, 1, 5, 8 ,9); int intial_list_size = list.size(); Pageable<Integer> pageable = new Pageable4Test (list); PagedIterator<Integer> iterator = new PagedIterator<Integer> (pageable, query, 0, 3); while (iterator.hasNext ()) { Integer element = iterator.next (); if (removed.contains (element)) { iterator.remove (); Assert.assertFalse (list.contains (element), "Removed element #" + element + " still present in list."); } } int expected_size = intial_list_size - removed.size(); Assert.assertEquals(list.size(), expected_size, "Initial list size not reduced as expected"); pageable = new Pageable4Test (list); iterator = new PagedIterator<Integer> (pageable, query, 0, 3); while (iterator.hasNext()) { iterator.next(); iterator.remove(); } try { iterator.remove (); Assert.fail ("Remove element from empty list should raise" + " IllegalStateException."); } catch (IllegalStateException e) { } }