@Test
 public void partition() {
   ImmutableCollection<Integer> integers = this.classUnderTest();
   PartitionImmutableCollection<Integer> partition = integers.partition(IntegerPredicates.isOdd());
   Assert.assertEquals(integers.select(IntegerPredicates.isOdd()), partition.getSelected());
   Assert.assertEquals(integers.select(IntegerPredicates.isEven()), partition.getRejected());
 }
Ejemplo n.º 2
0
  private void groupByAssertions(ImmutableBagMultimap<Boolean, String> multimap) {
    Verify.assertIterableEmpty(multimap.get(null));

    ImmutableBag<String> odds = multimap.get(true);
    ImmutableBag<String> evens = multimap.get(false);
    for (int i = 1; i <= this.numKeys(); i++) {
      String key = String.valueOf(i);
      ImmutableBag<String> containingBag = IntegerPredicates.isOdd().accept(i) ? odds : evens;
      ImmutableBag<String> nonContainingBag = IntegerPredicates.isOdd().accept(i) ? evens : odds;
      Assert.assertTrue(containingBag.contains(key));
      Assert.assertFalse(nonContainingBag.contains(key));

      Assert.assertEquals(i, containingBag.occurrencesOf(key));
    }
  }
 @Test
 public void rejectWith() {
   ImmutableCollection<Integer> integers = this.classUnderTest();
   Assert.assertEquals(
       this.<Integer>newMutable().withAll(integers).reject(IntegerPredicates.isOdd()),
       integers.rejectWith(Predicates2.in(), iList(1, 3, 5, 7, 9)));
 }
Ejemplo n.º 4
0
  @Test
  public void groupBy() {
    ImmutableBagMultimap<Boolean, String> multimap =
        this.newBag().groupBy(string -> IntegerPredicates.isOdd().accept(Integer.valueOf(string)));

    this.groupByAssertions(multimap);
  }
Ejemplo n.º 5
0
  @Test
  public void groupBy_with_target() {
    ImmutableBagMultimap<Boolean, String> multimap =
        this.newBag()
            .groupBy(
                string -> IntegerPredicates.isOdd().accept(Integer.valueOf(string)),
                new HashBagMultimap<Boolean, String>())
            .toImmutable();

    this.groupByAssertions(multimap);
  }
  @Test
  public void groupBy() {
    ImmutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3, "4", 4);

    Function<Integer, Boolean> isOddFunction = object -> IntegerPredicates.isOdd().accept(object);

    Multimap<Boolean, Integer> expected;

    switch (map.size()) {
      case 1:
        expected = FastListMultimap.newMultimap(Tuples.pair(Boolean.TRUE, 1));
        break;
      case 2:
        expected =
            FastListMultimap.newMultimap(
                Tuples.pair(Boolean.TRUE, 1), Tuples.pair(Boolean.FALSE, 2));
        break;
      case 3:
        expected =
            FastListMultimap.newMultimap(
                Tuples.pair(Boolean.TRUE, 1),
                Tuples.pair(Boolean.TRUE, 3),
                Tuples.pair(Boolean.FALSE, 2));
        break;
      case 4:
        expected =
            FastListMultimap.newMultimap(
                Tuples.pair(Boolean.TRUE, 1),
                Tuples.pair(Boolean.TRUE, 3),
                Tuples.pair(Boolean.FALSE, 2),
                Tuples.pair(Boolean.FALSE, 4));
        break;
      default:
        expected = FastListMultimap.newMultimap();
        break;
    }

    Multimap<Boolean, Integer> actual = map.groupBy(isOddFunction);
    Assert.assertEquals(HashBagMultimap.newMultimap(expected), HashBagMultimap.newMultimap(actual));

    Multimap<Boolean, Integer> actualFromTarget =
        map.groupBy(isOddFunction, FastListMultimap.<Boolean, Integer>newMultimap());
    Assert.assertEquals(
        HashBagMultimap.newMultimap(expected), HashBagMultimap.newMultimap(actualFromTarget));
  }