@Test public void testForEachUsingMap() { // Test the default batch size calculations IntegerSum sum1 = new IntegerSum(0); MutableMap<String, Integer> map1 = Interval.fromTo(1, 10000).toMap(String::valueOf, Functions.getIntegerPassThru()); ParallelIterate.forEach(map1, new SumProcedure(sum1), new SumCombiner(sum1)); Assert.assertEquals(50005000, sum1.getSum()); // Testing batch size 1 IntegerSum sum2 = new IntegerSum(0); UnifiedMap<String, Integer> map2 = (UnifiedMap<String, Integer>) Interval.fromTo(1, 100).toMap(String::valueOf, Functions.getIntegerPassThru()); ParallelIterate.forEach( map2, new SumProcedure(sum2), new SumCombiner(sum2), 1, map2.getBatchCount(map2.size())); Assert.assertEquals(5050, sum2.getSum()); // Testing an uneven batch size IntegerSum sum3 = new IntegerSum(0); UnifiedMap<String, Integer> set3 = (UnifiedMap<String, Integer>) Interval.fromTo(1, 100).toMap(String::valueOf, Functions.getIntegerPassThru()); ParallelIterate.forEach( set3, new SumProcedure(sum3), new SumCombiner(sum3), 1, set3.getBatchCount(13)); Assert.assertEquals(5050, sum3.getSum()); }
@Test public void getIfAbsentPutWithKey() { MutableMapIterable<Integer, Integer> map = this.newMapWithKeysValues(1, 1, 2, 2, 3, 3); Assert.assertNull(map.get(4)); Assert.assertEquals( Integer.valueOf(4), map.getIfAbsentPutWithKey(4, Functions.getIntegerPassThru())); Assert.assertEquals( Integer.valueOf(3), map.getIfAbsentPutWithKey(3, Functions.getIntegerPassThru())); Verify.assertContainsKeyValue(Integer.valueOf(4), Integer.valueOf(4), map); }
@Test public void collectIf() { ImmutableCollection<Integer> integers = this.classUnderTest(); Assert.assertEquals( integers, integers.collectIf(Predicates.instanceOf(Integer.class), Functions.getIntegerPassThru())); }
@Test public void basicCase() { CaseFunction<Integer, Integer> function = new CaseFunction<>(); function.addCase(ignored -> true, Functions.getIntegerPassThru()); Integer fortyTwo = 42; Assert.assertEquals(fortyTwo, function.valueOf(fortyTwo)); }
@Test public void toSortedMap() { LazyIterable<Integer> integers = this.newWith(1, 2, 3); MutableSortedMap<Integer, String> map = integers.toSortedMap(Functions.getIntegerPassThru(), Functions.getToString()); Verify.assertMapsEqual(TreeSortedMap.newMapWith(1, "1", 2, "2", 3, "3"), map); Verify.assertListsEqual(FastList.newListWith(1, 2, 3), map.keySet().toList()); }
@Test public void toSortedMap_with_comparator() { LazyIterable<Integer> integers = this.newWith(1, 2, 3); MutableSortedMap<Integer, String> map = integers.toSortedMap( Comparators.<Integer>reverseNaturalOrder(), Functions.getIntegerPassThru(), Functions.getToString()); Verify.assertMapsEqual( TreeSortedMap.newMapWith( Comparators.<Integer>reverseNaturalOrder(), 1, "1", 2, "2", 3, "3"), map); Verify.assertListsEqual(FastList.newListWith(3, 2, 1), map.keySet().toList()); }
@Test public void maxBy() { Assert.assertEquals( Integer.valueOf(this.classUnderTest().size()), this.classUnderTest().maxBy(Functions.getIntegerPassThru())); }
@Test public void collect() { ImmutableCollection<Integer> integers = this.classUnderTest(); Assert.assertEquals(integers, integers.collect(Functions.getIntegerPassThru())); }