@Test public void forEachWithIndexUsingFromToWithOptimisableList() { ArrayList<Integer> optimisableList = Interval.oneTo(105).addAllTo(new ArrayList<Integer>()); final ArrayList<Integer> expected = Interval.oneTo(105).addAllTo(new ArrayList<Integer>()); ArrayList<Integer> results = new ArrayList<Integer>(); ArrayListIterate.forEachWithIndex( optimisableList, 0, 104, ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(results))); Assert.assertEquals(expected, results); MutableList<Integer> reverseResults = Lists.mutable.of(); final ObjectIntProcedure<Integer> objectIntProcedure = ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(reverseResults)); ArrayListIterate.forEachWithIndex(expected, 104, 0, objectIntProcedure); Assert.assertEquals(ListIterate.reverseThis(expected), reverseResults); Verify.assertThrows( IndexOutOfBoundsException.class, new Runnable() { public void run() { ArrayListIterate.forEachWithIndex(expected, 104, -1, objectIntProcedure); } }); Verify.assertThrows( IndexOutOfBoundsException.class, new Runnable() { public void run() { ArrayListIterate.forEachWithIndex(expected, -1, 104, objectIntProcedure); } }); }
@Test public void forEachWithIndexOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); Iterate.sortThis(list); ArrayListIterate.forEachWithIndex( list, new ObjectIntProcedure<Integer>() { public void value(Integer object, int index) { Assert.assertEquals(index, object - 1); } }); }
@Test public void forEachWithIndex() { ArrayList<Integer> list = this.getIntegerList(); Iterate.sortThis(list); ArrayListIterate.forEachWithIndex( list, new ObjectIntProcedure<Integer>() { public void value(Integer object, int index) { Assert.assertEquals(index, object - 1); } }); }