IterableUtil { public static <T> Iterable<T> createReverseIterable(List<T> list) { if (list.size() == 0) { return list; } return new ReverseIterable<T>(list); }  static Iterable<T> createReverseIterable(List<T> list);  }
@Test public void testHasNext_Next() throws Exception { List<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); Iterable<String> iterable = IterableUtil.createReverseIterable(list); Iterator<String> iter = iterable.iterator(); assertTrue(iter.hasNext()); assertEquals("C", iter.next()); assertTrue(iter.hasNext()); assertEquals("B", iter.next()); assertTrue(iter.hasNext()); assertEquals("A", iter.next()); assertFalse(iter.hasNext()); }
@Test public void testRemove() throws Exception { List<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); Iterable<String> iterable = IterableUtil.createReverseIterable(list); Iterator<String> iter = iterable.iterator(); assertTrue(iter.hasNext()); assertEquals("C", iter.next()); iter.remove(); assertEquals(2, list.size()); assertTrue(iter.hasNext()); assertEquals("B", iter.next()); iter.remove(); assertEquals(1, list.size()); assertTrue(iter.hasNext()); assertEquals("A", iter.next()); iter.remove(); assertEquals(0, list.size()); assertFalse(iter.hasNext()); }
@Test public void testEmptyIterator() throws Exception { List<String> list = new ArrayList<String>(); Iterable<String> iterable = IterableUtil.createReverseIterable(list); Iterator<String> iter = iterable.iterator(); assertFalse(iter.hasNext()); }