@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 forEachUsingFromTo() { final ArrayList<Integer> integers = Interval.oneTo(5).addAllTo(new ArrayList<Integer>()); ArrayList<Integer> results = new ArrayList<Integer>(); ArrayListIterate.forEach(integers, 0, 4, CollectionAddProcedure.on(results)); Assert.assertEquals(integers, results); MutableList<Integer> reverseResults = Lists.mutable.of(); final CollectionAddProcedure<Integer> procedure = CollectionAddProcedure.on(reverseResults); ArrayListIterate.forEach(integers, 4, 0, procedure); Assert.assertEquals(ListIterate.reverseThis(integers), reverseResults); Verify.assertThrows( IndexOutOfBoundsException.class, new Runnable() { public void run() { ArrayListIterate.forEach(integers, 4, -1, procedure); } }); Verify.assertThrows( IndexOutOfBoundsException.class, new Runnable() { public void run() { ArrayListIterate.forEach(integers, -1, 4, procedure); } }); }
@Test public void forEachWith() { MutableList<String> result = Lists.mutable.of(); MutableSet<String> source = Sets.fixedSize.of("1", "2", "3", "4"); source.forEachWith(Procedures2.fromProcedure(CollectionAddProcedure.on(result)), null); Assert.assertEquals(FastList.newListWith("1", "2", "3", "4"), result); }
@Test public void forEachWithIndex() { MutableBag<String> result = Bags.mutable.of(); ImmutableBag<String> strings = this.newBag(); strings.forEachWithIndex(ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(result))); Assert.assertEquals(strings, result); }
@Test public void forEach() { MutableBag<String> result = Bags.mutable.of(); ImmutableBag<String> collection = this.newBag(); collection.forEach(CollectionAddProcedure.on(result)); Assert.assertEquals(collection, result); }
@Test public void reverseForEach_emptyList() { ArrayList<Integer> integers = new ArrayList<Integer>(); MutableList<Integer> results = Lists.mutable.of(); ArrayListIterate.reverseForEach(integers, CollectionAddProcedure.on(results)); Assert.assertEquals(integers, results); }
@Test public void reverseForEach() { ArrayList<Integer> integers = Interval.oneTo(5).addAllTo(new ArrayList<Integer>()); MutableList<Integer> reverseResults = Lists.mutable.of(); ArrayListIterate.reverseForEach(integers, CollectionAddProcedure.on(reverseResults)); Assert.assertEquals(ListIterate.reverseThis(integers), reverseResults); }
@Test public void forEachKey() { MutableList<Integer> collection = Lists.mutable.of(); Multimap<Integer, String> multimap = this.newMultimapWithKeysValues(1, "1", 2, "2", 3, "3"); multimap.forEachKey(CollectionAddProcedure.on(collection)); Assert.assertEquals(FastList.newListWith(1, 2, 3), collection); }
@Test public void forEachValue() { MutableBag<String> collection = Bags.mutable.of(); Multimap<Integer, String> multimap = this.newMultimapWithKeysValues(1, "1", 2, "2", 3, "3"); multimap.forEachValue(CollectionAddProcedure.on(collection)); Assert.assertEquals(HashBag.newBagWith("1", "2", "3"), collection); }
@Test public void forEachOver100() { ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101)); Iterate.sortThis(list); FastList<Integer> result = FastList.newList(101); ArrayListIterate.forEach(list, CollectionAddProcedure.<Integer>on(result)); Verify.assertListsEqual(list, result); }
@Test public void forEach() { ArrayList<Integer> list = this.getIntegerList(); Iterate.sortThis(list); MutableList<Integer> result = Lists.mutable.of(); ArrayListIterate.forEach(list, CollectionAddProcedure.<Integer>on(result)); Verify.assertListsEqual(list, result); }
@Test public void testForEachWithFromToWithCommandoPatternOptimization() { MutableList<Integer> result2 = Lists.mutable.of(); // Requires list of 100+ elements to engage commando pattern optimization ArrayListAdapter.adapt(new ArrayList<>(Interval.oneTo(200))) .forEach(99, 199, CollectionAddProcedure.on(result2)); Verify.assertSize(101, result2); }
@Override @Test public void forEach() { super.forEach(); MutableList<Integer> list = FastList.newList(); CompositeFastList<Integer> iterables = new CompositeFastList<Integer>(); iterables.addComposited(Interval.oneTo(5).toList()); iterables.addComposited(Interval.fromTo(6, 10).toList()); iterables.forEach(CollectionAddProcedure.on(list)); Verify.assertSize(10, list); Verify.assertAllSatisfy(list, Predicates.greaterThan(0).and(Predicates.lessThan(11))); }
@Override public MutableList<T> toList() { FastList<T> result = FastList.newList(this.size()); this.forEach(CollectionAddProcedure.on(result)); return result; }
@Override public MutableSet<T> toSet() { UnifiedSet<T> result = UnifiedSet.newSet(this.sizeDistinct()); this.items.forEachKey(CollectionAddProcedure.on(result)); return result; }