@Test
  public void zip() {
    List<Object> nulls = Collections.nCopies(this.lazyIterable.size(), null);
    List<Object> nullsPlusOne = Collections.nCopies(this.lazyIterable.size() + 1, null);
    List<Object> nullsMinusOne = Collections.nCopies(this.lazyIterable.size() - 1, null);

    LazyIterable<Pair<Integer, Object>> pairs = this.lazyIterable.zip(nulls);
    Assert.assertEquals(
        this.lazyIterable.toSet(), pairs.collect(Functions.<Integer>firstOfPair()).toSet());
    Assert.assertEquals(nulls, pairs.collect(Functions.<Object>secondOfPair(), Lists.mutable.of()));

    LazyIterable<Pair<Integer, Object>> pairsPlusOne = this.lazyIterable.zip(nullsPlusOne);
    Assert.assertEquals(
        this.lazyIterable.toSet(), pairsPlusOne.collect(Functions.<Integer>firstOfPair()).toSet());
    Assert.assertEquals(
        nulls, pairsPlusOne.collect(Functions.<Object>secondOfPair(), Lists.mutable.of()));

    LazyIterable<Pair<Integer, Object>> pairsMinusOne = this.lazyIterable.zip(nullsMinusOne);
    Assert.assertEquals(this.lazyIterable.size() - 1, pairsMinusOne.size());
    Assert.assertTrue(
        this.lazyIterable.containsAllIterable(
            pairsMinusOne.collect(Functions.<Integer>firstOfPair())));

    Assert.assertEquals(
        this.lazyIterable.zip(nulls).toSet(),
        this.lazyIterable.zip(nulls, UnifiedSet.<Pair<Integer, Object>>newSet()));
  }
  @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 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 toMap() {
   RichIterable<Integer> integers = this.newWith(1, 2, 3, 4);
   MutableMap<String, String> map =
       integers.toMap(Functions.getToString(), Functions.getToString());
   Assert.assertEquals(
       UnifiedMap.<String, String>newWithKeysValues("1", "1", "2", "2", "3", "3", "4", "4"), map);
 }
 @Override
 @Test
 public void toSortedMap() {
   MutableSortedMap<String, String> map =
       this.newBag().toSortedMap(Functions.getStringPassThru(), Functions.getStringPassThru());
   Verify.assertEmpty(map);
   Verify.assertInstanceOf(TreeSortedMap.class, map);
 }
  @Test
  public void toSortedMap() {
    MutableSortedMap<Integer, String> map =
        this.newBag().toSortedMap(Integer::valueOf, Functions.<String>getPassThru());

    Verify.assertMapsEqual(
        this.newBag().toMap(Integer::valueOf, Functions.<String>getPassThru()), map);
    Verify.assertListsEqual(Interval.oneTo(this.numKeys()), map.keySet().toList());
  }
  @Test
  public void collectWith() {
    ImmutableCollection<Integer> integers = this.classUnderTest();
    ImmutableCollection<String> expected =
        integers.collect(Functions.chain(Functions.getToString(), StringFunctions.append("!")));
    ImmutableCollection<String> actual =
        integers.collectWith((argument1, argument2) -> argument1 + argument2, "!");

    Assert.assertEquals(expected, actual);
  }
 @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 toMapWithFunctions() {
   String tokens = "1:Ted|2:Mary";
   Function<String, String> stringPassThruFunction = Functions.getPassThru();
   MutableMap<Integer, String> results =
       StringIterate.tokensToMap(
           tokens, "|", ":", Functions.getStringToInteger(), stringPassThruFunction);
   Verify.assertSize(2, results);
   Verify.assertContainsKeyValue(1, "Ted", results);
   Verify.assertContainsKeyValue(2, "Mary", results);
 }
 @Test
 public void collect_target() {
   ImmutableBag<String> strings = this.newBag();
   HashBag<String> target = HashBag.<String>newBag();
   HashBag<String> actual = strings.collect(Functions.getStringPassThru(), target);
   Assert.assertEquals(strings, actual);
   Assert.assertSame(target, actual);
   Assert.assertEquals(
       strings,
       strings.collect(Functions.getStringPassThru(), FastList.<String>newList()).toBag());
 }
 @Test
 public void aggregateByNonMutating() {
   Function0<Integer> valueCreator = Functions0.value(0);
   Function2<Integer, Integer, Integer> sumAggregator = Functions2.integerAddition();
   MapIterable<String, Integer> actual =
       this.classUnderTest().aggregateBy(Functions.getToString(), valueCreator, sumAggregator);
   MapIterable<String, Integer> expected =
       this.classUnderTest()
           .toBag()
           .aggregateBy(Functions.getToString(), valueCreator, sumAggregator);
   Assert.assertEquals(expected, actual);
 }
 @Test
 public void aggregateByMutating() {
   Procedure2<Counter, Integer> sumAggregator = Counter::add;
   MapIterable<String, Counter> actual =
       this.classUnderTest()
           .aggregateInPlaceBy(Functions.getToString(), Counter::new, sumAggregator);
   MapIterable<String, Counter> expected =
       this.classUnderTest()
           .toBag()
           .aggregateInPlaceBy(Functions.getToString(), Counter::new, sumAggregator);
   Assert.assertEquals(expected, actual);
 }
  @Test
  public void toSortedMap_with_comparator() {
    MutableSortedMap<Integer, String> map =
        this.newBag()
            .toSortedMap(
                Comparators.<Integer>reverseNaturalOrder(),
                Integer::valueOf,
                Functions.<String>getPassThru());

    Verify.assertMapsEqual(
        this.newBag().toMap(Integer::valueOf, Functions.<String>getPassThru()), map);
    Verify.assertListsEqual(Interval.fromTo(this.numKeys(), 1), map.keySet().toList());
  }
 @Override
 @Test
 public void toSortedMap_with_comparator() {
   MutableSortedMap<String, String> map =
       this.newBag()
           .toSortedMap(
               Comparators.<String>reverseNaturalOrder(),
               Functions.getStringPassThru(),
               Functions.getStringPassThru());
   Verify.assertEmpty(map);
   Verify.assertInstanceOf(TreeSortedMap.class, map);
   Assert.assertEquals(Comparators.<String>reverseNaturalOrder(), map.comparator());
 }
 @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 zipWithIndex() {
    LazyIterable<Pair<Integer, Integer>> pairs = this.lazyIterable.zipWithIndex();

    Assert.assertEquals(
        this.lazyIterable.toSet(), pairs.collect(Functions.<Integer>firstOfPair()).toSet());
    Assert.assertEquals(
        Interval.zeroTo(this.lazyIterable.size() - 1).toSet(),
        pairs.collect(Functions.<Integer>secondOfPair(), UnifiedSet.<Integer>newSet()));

    Assert.assertEquals(
        this.lazyIterable.zipWithIndex().toSet(),
        this.lazyIterable.zipWithIndex(UnifiedSet.<Pair<Integer, Integer>>newSet()));
  }
  @Test
  public void toMap() {
    MutableMap<String, String> map =
        this.newBag().toMap(Functions.<String>getPassThru(), Functions.<String>getPassThru());

    for (int i = 1; i <= this.numKeys(); i++) {
      String key = String.valueOf(i);
      Assert.assertTrue(map.containsKey(key));
      Assert.assertEquals(key, map.get(key));
    }

    String missingKey = "0";
    Assert.assertFalse(map.containsKey(missingKey));
    Assert.assertNull(map.get(missingKey));
  }
 @Test
 public void collectIfOver100() {
   ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101));
   ArrayList<Class<?>> result =
       ArrayListIterate.collectIf(list, Predicates.equal(101), Functions.getToClass());
   Assert.assertEquals(FastList.newListWith(Integer.class), result);
 }
  @Test
  public void defaultValue() {
    CaseFunction<Foo, String> function =
        Functions.caseDefault(
            Functions.getFixedValue("Yow!"),
            Predicates.attributeGreaterThan(Foo.TO_VALUE, 5.0D),
            Functions.getFixedValue("Patience, grasshopper"));

    Assert.assertEquals("Yow!", function.valueOf(new Foo("", 1.0D)));

    function.setDefault(Functions.getFixedValue("Patience, young grasshopper"));
    Assert.assertEquals("Patience, grasshopper", function.valueOf(new Foo("", 6.0D)));
    Assert.assertEquals("Patience, young grasshopper", function.valueOf(new Foo("", 1.0D)));

    Verify.assertContains("CaseFunction", function.toString());
  }
 @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 collect_value() {
    ImmutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3, "4", 4);
    MutableSet<String> collect = map.collect(Functions.getToString()).toSet();
    UnifiedSet<String> collectToTarget = map.collect(String::valueOf, UnifiedSet.<String>newSet());

    switch (map.size()) {
      case 1:
        Verify.assertContainsAll(collect, "1");
        Verify.assertContainsAll(collectToTarget, "1");
        break;
      case 2:
        Verify.assertContainsAll(collect, "1", "2");
        Verify.assertContainsAll(collectToTarget, "1", "2");
        break;
      case 3:
        Verify.assertContainsAll(collect, "1", "2", "3");
        Verify.assertContainsAll(collectToTarget, "1", "2", "3");
        break;
      case 4:
        Verify.assertContainsAll(collect, "1", "2", "3", "4");
        Verify.assertContainsAll(collectToTarget, "1", "2", "3", "4");
        break;
      default:
        Verify.assertEmpty(collect);
        break;
    }
  }
 @Test
 public void toSortedSetBy() {
   this.unmodifiableCollection = this.newWith("2", "4", "1", "3");
   MutableSortedSet<String> set =
       this.unmodifiableCollection.toSortedSetBy(Functions.getStringToInteger());
   Verify.assertSortedSetsEqual(TreeSortedSet.newSetWith("1", "2", "3", "4"), set);
 }
 @Test
 public void collectOver100() {
   ArrayList<Integer> list = new ArrayList<Integer>(Interval.oneTo(101));
   ArrayList<Class<?>> newCollection = ArrayListIterate.collect(list, Functions.getToClass());
   Verify.assertSize(101, newCollection);
   Verify.assertContains(Integer.class, newCollection);
 }
 @Test
 public void collectIf() {
   ImmutableCollection<Integer> integers = this.classUnderTest();
   Assert.assertEquals(
       integers,
       integers.collectIf(Predicates.instanceOf(Integer.class), Functions.getIntegerPassThru()));
 }
  @Override
  @Test
  public void ifPresentApply() {
    Integer absentKey = this.size() + 1;

    ImmutableSortedMap<Integer, String> classUnderTest = this.classUnderTest();
    Assert.assertNull(classUnderTest.ifPresentApply(absentKey, Functions.<String>getPassThru()));
  }
 @Test
 public void groupByWithOptimisedList() {
   ArrayList<Integer> list = new ArrayList<Integer>(Interval.toReverseList(1, 105));
   MutableMultimap<String, Integer> target = new FastListMultimap<String, Integer>();
   MutableMultimap<String, Integer> result =
       ArrayListIterate.groupBy(list, Functions.getToString(), target);
   Assert.assertEquals(result.get("105"), FastList.newListWith(105));
 }
 @Test
 public void collectIf() {
   Assert.assertEquals(
       FastList.newListWith("1", "2", "3"),
       this.newWith(1, 2, 3)
           .collectIf(Predicates.instanceOf(Integer.class), Functions.getToString())
           .toList());
 }
 @Test
 public void collectIfWithTarget() {
   ImmutableBag<String> strings = this.newBag();
   Assert.assertEquals(
       strings,
       strings.collectIf(
           String.class::isInstance, Functions.getStringPassThru(), HashBag.<String>newBag()));
 }
  @Test
  public void collectWith_target() {
    ImmutableCollection<Integer> integers = this.classUnderTest();
    MutableCollection<String> expected =
        this.<String>newMutable()
            .with("?")
            .withAll(
                integers.collect(
                    Functions.chain(Functions.getToString(), StringFunctions.append("!"))));
    MutableCollection<String> targetCollection = this.<String>newMutable().with("?");
    MutableCollection<String> actual =
        integers.collectWith(
            (argument1, argument2) -> argument1 + argument2, "!", targetCollection);

    Assert.assertEquals(expected, actual);
    Assert.assertSame(targetCollection, actual);
  }
  @Test
  public void flatCollect() {
    RichIterable<String> actual =
        this.classUnderTest().flatCollect(integer -> Lists.fixedSize.of(String.valueOf(integer)));

    ImmutableCollection<String> expected = this.classUnderTest().collect(Functions.getToString());

    Assert.assertEquals(expected, actual);
  }