@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()); }
@Test public void select_value() { MutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3); ImmutableSet<Integer> expected = this.expectSelect(map.size()); Assert.assertEquals(expected, map.select(IntegerPredicates.isEven()).toSet()); Assert.assertEquals( expected, map.select(IntegerPredicates.isEven(), UnifiedSet.<Integer>newSet())); }
@Test public void reject_value() { ImmutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3, "4", 4); MutableSet<Integer> rejected = map.reject(IntegerPredicates.isEven()).toSet(); UnifiedSet<Integer> rejectedIntoTarget = map.reject(IntegerPredicates.isEven(), UnifiedSet.<Integer>newSet()); ImmutableSet<Integer> expected = this.expectReject(map.size()); Assert.assertEquals(expected, rejected); Assert.assertEquals(expected, rejectedIntoTarget); }
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))); }
@Test public void groupBy() { ImmutableBagMultimap<Boolean, String> multimap = this.newBag().groupBy(string -> IntegerPredicates.isOdd().accept(Integer.valueOf(string))); this.groupByAssertions(multimap); }
@Test public void partition_value() { MutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3); PartitionMutableCollection<Integer> partition = map.partition(IntegerPredicates.isEven()); Assert.assertEquals(this.expectSelect(map.size()), partition.getSelected().toSet()); Assert.assertEquals(this.expectReject(map.size()), partition.getRejected().toSet()); }
@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)); }
@Test public void remove_with_hashingStrategy() { HashingStrategy<Integer> hashingStrategy = HashingStrategies.nullSafeHashingStrategy( new HashingStrategy<Integer>() { public int computeHashCode(Integer object) { return object % 1000; } public boolean equals(Integer object1, Integer object2) { return object1.equals(object2); } }); UnifiedSetWithHashingStrategy<Integer> integers = UnifiedSetWithHashingStrategy.newSet(hashingStrategy, 2) .with(COLLISION_1, COLLISION_1 + 1000, COLLISION_1 + 2000, null); // Testing remove null from the end of the chain Assert.assertTrue(integers.remove(null)); // Adding null back and creating a deep chain. integers.with(null, COLLISION_1 + 3000, COLLISION_1 + 4000, COLLISION_1 + 5000); // Removing null from the first position of a bucket in the deep chain Assert.assertTrue(integers.remove(null)); Assert.assertFalse(integers.remove(null)); // Removing from the end of the deep chain Assert.assertTrue(integers.remove(COLLISION_1 + 4000)); // Removing from the first spot of the chain Assert.assertTrue(integers.remove(COLLISION_1)); Verify.assertSize(4, integers); // Testing removing a non existent element from a non bucket slot integers.add(2); integers.add(4); Assert.assertFalse(integers.remove(1002)); // Testing removeIf integers.removeIf(IntegerPredicates.isEven()); Verify.assertEmpty(integers); }
@Test public void selectByOccurrences() { ImmutableBag<String> strings = this.newBag().selectByOccurrences(IntPredicates.isEven()); ImmutableBag<Integer> collect = strings.collect(Integer::valueOf); Verify.assertAllSatisfy(collect, IntegerPredicates.isEven()); }
@Test public void partition() { PartitionIterable<Integer> partition = this.lazyIterable.partition(IntegerPredicates.isEven()); Assert.assertEquals(iList(2, 4, 6), partition.getSelected()); Assert.assertEquals(iList(1, 3, 5, 7), partition.getRejected()); }