private void basicReject(RichIterable<Integer> iterable) { Collection<Integer> actual1 = ParallelIterate.reject(iterable, Predicates.greaterThan(10000)); Collection<Integer> actual2 = ParallelIterate.reject( iterable, Predicates.greaterThan(10000), HashBag.<Integer>newBag(), 3, this.executor, true); Collection<Integer> actual3 = ParallelIterate.reject(iterable, Predicates.greaterThan(10000), true); RichIterable<Integer> expected = iterable.reject(Predicates.greaterThan(10000)); Assert.assertEquals( expected.getClass().getSimpleName() + '/' + actual1.getClass().getSimpleName(), expected, actual1); Assert.assertEquals( expected.getClass().getSimpleName() + '/' + actual2.getClass().getSimpleName(), expected.toBag(), actual2); Assert.assertEquals( expected.getClass().getSimpleName() + '/' + actual3.getClass().getSimpleName(), expected.toBag(), HashBag.newBag(actual3)); }
@Test public void zip() { ImmutableBag<String> immutableBag = this.newBag(); List<Object> nulls = Collections.nCopies(immutableBag.size(), null); List<Object> nullsPlusOne = Collections.nCopies(immutableBag.size() + 1, null); List<Object> nullsMinusOne = Collections.nCopies(immutableBag.size() - 1, null); ImmutableBag<Pair<String, Object>> pairs = immutableBag.zip(nulls); Assert.assertEquals( immutableBag, pairs.collect((Function<Pair<String, ?>, String>) Pair::getOne)); Assert.assertEquals( HashBag.newBag(nulls), pairs.collect((Function<Pair<?, Object>, Object>) Pair::getTwo)); ImmutableBag<Pair<String, Object>> pairsPlusOne = immutableBag.zip(nullsPlusOne); Assert.assertEquals( immutableBag, pairsPlusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne)); Assert.assertEquals( HashBag.newBag(nulls), pairsPlusOne.collect((Function<Pair<?, Object>, Object>) Pair::getTwo)); ImmutableBag<Pair<String, Object>> pairsMinusOne = immutableBag.zip(nullsMinusOne); Assert.assertEquals(immutableBag.size() - 1, pairsMinusOne.size()); Assert.assertTrue( immutableBag.containsAllIterable( pairsMinusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne))); Assert.assertEquals( immutableBag.zip(nulls), immutableBag.zip(nulls, HashBag.<Pair<String, Object>>newBag())); }
@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 selectByOccurrences() { MutableBag<Integer> integers = UnmodifiableBag.of(HashBag.newBagWith(1, 1, 1, 1, 2, 2, 2, 3, 3, 4)); Assert.assertEquals( iBag(1, 1, 1, 1, 3, 3), integers.selectByOccurrences(IntPredicates.isEven())); }
@Test public void collectWith() { ImmutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3, "4", 4); MutableBag<Integer> collectWith = map.collectWith(AddFunction.INTEGER, 1, HashBag.<Integer>newBag()); switch (map.size()) { case 1: Assert.assertEquals(Bags.mutable.of(2), collectWith); break; case 2: Assert.assertEquals(Bags.mutable.of(2, 3), collectWith); break; case 3: Assert.assertEquals(Bags.mutable.of(2, 3, 4), collectWith); break; case 4: Assert.assertEquals(Bags.mutable.of(2, 3, 4, 5), collectWith); break; default: Verify.assertEmpty(collectWith); break; } }
@Test public void collectIfWithTarget() { ImmutableBag<String> strings = this.newBag(); Assert.assertEquals( strings, strings.collectIf( String.class::isInstance, Functions.getStringPassThru(), HashBag.<String>newBag())); }
@Override @Test public void collectLong() { MutableBag<Integer> integers = UnmodifiableBag.of(HashBag.newBagWith(1, 2, 2, 3, 3, 3)); Assert.assertEquals( LongHashBag.newBagWith(1L, 2L, 2L, 3L, 3L, 3L), integers.collectLong(PrimitiveFunctions.unboxIntegerToLong())); }
@Override @Test public void collectFloat() { MutableBag<Integer> integers = UnmodifiableBag.of(HashBag.newBagWith(1, 2, 2, 3, 3, 3)); Assert.assertEquals( FloatHashBag.newBagWith(1.0f, 2.0f, 2.0f, 3.0f, 3.0f, 3.0f), integers.collectFloat(PrimitiveFunctions.unboxIntegerToFloat())); }
@Override @Test public void collectDouble() { MutableBag<Integer> integers = UnmodifiableBag.of(HashBag.newBagWith(1, 2, 2, 3, 3, 3)); Assert.assertEquals( DoubleHashBag.newBagWith(1.0d, 2.0d, 2.0d, 3.0d, 3.0d, 3.0d), integers.collectDouble(PrimitiveFunctions.unboxIntegerToDouble())); }
@Override @Test public void collectByte() { MutableBag<Integer> integers = UnmodifiableBag.of(HashBag.newBagWith(1, 2, 2, 3, 3, 3)); Assert.assertEquals( ByteHashBag.newBagWith((byte) 1, (byte) 2, (byte) 2, (byte) 3, (byte) 3, (byte) 3), integers.collectByte(PrimitiveFunctions.unboxIntegerToByte())); }
@Override @Test public void collectBoolean() { MutableBag<Integer> integers = UnmodifiableBag.of(HashBag.newBagWith(0, 1, 2, 2)); Assert.assertEquals( BooleanHashBag.newBagWith(false, true, true, true), integers.collectBoolean(PrimitiveFunctions.integerIsPositive())); }
@Override @Test public void collectShort() { MutableBag<Integer> integers = UnmodifiableBag.of(HashBag.newBagWith(1, 2, 2, 3, 3, 3)); Assert.assertEquals( ShortHashBag.newBagWith((short) 1, (short) 2, (short) 2, (short) 3, (short) 3, (short) 3), integers.collectShort(PrimitiveFunctions.unboxIntegerToShort())); }
@Test public void forEachKeyValue() { MutableBag<String> collection = Bags.mutable.of(); Multimap<Integer, String> multimap = this.newMultimapWithKeysValues(1, "One", 2, "Two", 3, "Three"); multimap.forEachKeyValue((key, value) -> collection.add(key + value)); Assert.assertEquals(HashBag.newBagWith("1One", "2Two", "3Three"), collection); }
@Test public void newWithAll() { ImmutableBag<String> bag = this.newBag(); ImmutableBag<String> newBag = bag.newWithAll(Bags.mutable.of("0")); Assert.assertNotEquals(bag, newBag); Assert.assertEquals(HashBag.newBag(bag).with("0"), newBag); Assert.assertEquals(newBag.size(), bag.size() + 1); }
@Test public void flatCollectWithTarget() { MutableBag<String> actual = this.newBag().flatCollect(Lists.fixedSize::of, HashBag.<String>newBag()); ImmutableBag<String> expected = this.newBag().collect(String::valueOf); Assert.assertEquals(expected, actual); }
@Test public void equalsAndHashCode() { ImmutableBag<String> immutable = this.newBag(); MutableBag<String> mutable = HashBag.newBag(immutable); Verify.assertEqualsAndHashCode(immutable, mutable); Assert.assertNotEquals(immutable, FastList.newList(mutable)); Assert.assertEquals(this.newBag().toMapOfItemToCount().hashCode(), this.newBag().hashCode()); Assert.assertNotEquals(immutable, mutable.with("5").without("1")); }
public static <E> HashBag<E> newBag(Bag<? extends E> source) { final HashBag<E> result = HashBag.newBag(); source.forEachWithOccurrences( new ObjectIntProcedure<E>() { public void value(E each, int occurrences) { result.addOccurrences(each, occurrences); } }); return result; }
@Test public void flatCollect() { ImmutableMap<Integer, Integer> map = this.newMapWithKeysValues(1, 1, 2, 2, 3, 3, 4, 4); if (map.isEmpty()) { Function<Integer, Iterable<Object>> fail = each -> { throw new AssertionError(); }; Assert.assertEquals(Bags.immutable.empty(), map.flatCollect(fail)); Assert.assertEquals(Bags.immutable.empty(), map.flatCollect(fail, HashBag.newBag())); } else { MutableBag<Integer> expected = Interval.oneTo(map.size()).flatCollect(each -> Interval.oneTo(each)).toBag(); Assert.assertEquals(expected, map.flatCollect(each -> Interval.oneTo(each))); Assert.assertEquals( expected, map.flatCollect(each -> Interval.oneTo(each), HashBag.newBag())); } }
@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()); }
@Override public <V> MutableBag<V> collect(final Function<? super T, ? extends V> function) { final HashBag<V> result = HashBag.newBag(this.items.size()); this.forEachWithOccurrences( new ObjectIntProcedure<T>() { public void value(T each, int occurrences) { result.addOccurrences(function.valueOf(each), occurrences); } }); return result; }
public <S> MutableBag<S> selectInstancesOf(final Class<S> clazz) { final MutableBag<S> result = HashBag.newBag(); this.forEachWithOccurrences( new ObjectIntProcedure<T>() { public void value(T each, int occurrences) { if (clazz.isInstance(each)) { result.addOccurrences((S) each, occurrences); } } }); return result; }
@Test public void updateValue_collisions() { MutableMapIterable<Integer, Integer> map = this.newMap(); MutableList<Integer> list = Interval.oneTo(2000).toList(); Collections.shuffle(list); Iterate.forEach(list, each -> map.updateValue(each % 1000, () -> 0, integer -> integer + 1)); Assert.assertEquals(Interval.zeroTo(999).toSet(), map.keySet()); Assert.assertEquals( HashBag.newBag(map.values()).toStringOfItemToCount(), FastList.newList(Collections.nCopies(1000, 2)), FastList.newList(map.values())); }
@Test public void groupByEach() { ImmutableBag<Integer> immutableBag = this.newBag().collect(Integer::valueOf); MutableMultimap<Integer, Integer> expected = HashBagMultimap.newMultimap(); int keys = this.numKeys(); immutableBag.forEachWithOccurrences( (each, parameter) -> { HashBag<Integer> bag = HashBag.newBag(); Interval.fromTo(each, keys) .forEach((int eachInt) -> bag.addOccurrences(eachInt, eachInt)); expected.putAll(-each, bag); }); Multimap<Integer, Integer> actual = immutableBag.groupByEach(new NegativeIntervalFunction()); Assert.assertEquals(expected, actual); Multimap<Integer, Integer> actualWithTarget = immutableBag.groupByEach( new NegativeIntervalFunction(), HashBagMultimap.<Integer, Integer>newMultimap()); Assert.assertEquals(expected, actualWithTarget); }
@Test public void collectWith_target() { ImmutableBag<String> strings = this.newBag(); String argument = "thing"; HashBag<String> targetCollection = HashBag.<String>newBag(); HashBag<String> actual = strings.collectWith( this.generateAssertingPassThroughFunction2(argument), argument, targetCollection); Assert.assertEquals(strings, actual); Assert.assertSame(targetCollection, actual); }
private void basicCollectIf(RichIterable<Integer> collection) { Predicate<Integer> greaterThan = Predicates.greaterThan(10000); Collection<String> actual1 = ParallelIterate.collectIf(collection, greaterThan, String::valueOf); Collection<String> actual2 = ParallelIterate.collectIf( collection, greaterThan, String::valueOf, HashBag.<String>newBag(), 3, this.executor, true); Collection<String> actual3 = ParallelIterate.collectIf( collection, greaterThan, String::valueOf, HashBag.<String>newBag(), 3, this.executor, true); Bag<String> expected = collection.collectIf(greaterThan, String::valueOf).toBag(); Verify.assertSize(10000, actual1); Verify.assertNotContains(String.valueOf(9000), actual1); Verify.assertNotContains(String.valueOf(21000), actual1); Verify.assertContains(String.valueOf(15976), actual1); Assert.assertEquals( expected.getClass().getSimpleName() + '/' + actual1.getClass().getSimpleName(), expected, HashBag.newBag(actual1)); Assert.assertEquals( expected.getClass().getSimpleName() + '/' + actual2.getClass().getSimpleName(), expected, actual2); Assert.assertEquals( expected.getClass().getSimpleName() + '/' + actual3.getClass().getSimpleName(), expected, actual3); }
private void basicCollect(RichIterable<Integer> iterable) { Collection<String> actual1 = ParallelIterate.collect(iterable, String::valueOf); Collection<String> actual2 = ParallelIterate.collect( iterable, String::valueOf, HashBag.<String>newBag(), 3, this.executor, false); Collection<String> actual3 = ParallelIterate.collect(iterable, String::valueOf, true); RichIterable<String> expected = iterable.collect(String::valueOf); Verify.assertSize(20000, actual1); Verify.assertContains(String.valueOf(20000), actual1); Assert.assertEquals( expected.getClass().getSimpleName() + '/' + actual1.getClass().getSimpleName(), expected, actual1); Assert.assertEquals( expected.getClass().getSimpleName() + '/' + actual2.getClass().getSimpleName(), expected.toBag(), actual2); Assert.assertEquals( expected.getClass().getSimpleName() + '/' + actual3.getClass().getSimpleName(), expected.toBag(), HashBag.newBag(actual3)); }
@Override @Test public void collectChar() { MutableBag<Integer> integers = UnmodifiableBag.of(HashBag.newBagWith(1, 2, 2, 3, 3, 3)); Assert.assertEquals( CharHashBag.newBagWith('A', 'B', 'B', 'C', 'C', 'C'), integers.collectChar( new CharFunction<Integer>() { public char charValueOf(Integer integer) { return (char) (integer.intValue() + 64); } })); }
@Override public MutableBag<T> reject(final Predicate<? super T> predicate) { final MutableBag<T> result = HashBag.newBag(); this.forEachWithOccurrences( new ObjectIntProcedure<T>() { public void value(T each, int index) { if (!predicate.accept(each)) { result.addOccurrences(each, index); } } }); return result; }
private void basicFlatCollect(RichIterable<Integer> iterable) { Collection<String> actual1 = ParallelIterate.flatCollect(iterable, INT_TO_TWO_STRINGS); Collection<String> actual2 = ParallelIterate.flatCollect( iterable, INT_TO_TWO_STRINGS, HashBag.<String>newBag(), 3, this.executor, false); Collection<String> actual3 = ParallelIterate.flatCollect(iterable, INT_TO_TWO_STRINGS, true); RichIterable<String> expected1 = iterable.flatCollect(INT_TO_TWO_STRINGS); RichIterable<String> expected2 = iterable.flatCollect(INT_TO_TWO_STRINGS, HashBag.<String>newBag()); Verify.assertContains(String.valueOf(20000), actual1); Assert.assertEquals( expected1.getClass().getSimpleName() + '/' + actual1.getClass().getSimpleName(), expected1, actual1); Assert.assertEquals( expected2.getClass().getSimpleName() + '/' + actual2.getClass().getSimpleName(), expected2, actual2); Assert.assertEquals( expected1.getClass().getSimpleName() + '/' + actual3.getClass().getSimpleName(), expected1.toBag(), HashBag.newBag(actual3)); }
@Override public <V> MutableBag<V> collectIf( final Predicate<? super T> predicate, final Function<? super T, ? extends V> function) { final MutableBag<V> result = HashBag.newBag(); this.forEachWithOccurrences( new ObjectIntProcedure<T>() { public void value(T each, int occurrences) { if (predicate.accept(each)) { result.addOccurrences(function.valueOf(each), occurrences); } } }); return result; }