@Test public void newBagWithBag() { ImmutableBag<String> bag = Bags.immutable.of(); HashBag<String> hashBag = HashBag.newBagWith("1"); Assert.assertEquals(bag = bag.newWith("1"), hashBag.toImmutable()); hashBag.add("2"); Assert.assertEquals(bag = bag.newWith("2"), hashBag.toImmutable()); hashBag.add("3"); Assert.assertEquals(bag = bag.newWith("3"), hashBag.toImmutable()); hashBag.add("4"); Assert.assertEquals(bag = bag.newWith("4"), hashBag.toImmutable()); hashBag.add("5"); Assert.assertEquals(bag = bag.newWith("5"), hashBag.toImmutable()); hashBag.add("6"); Assert.assertEquals(bag = bag.newWith("6"), hashBag.toImmutable()); hashBag.add("7"); Assert.assertEquals(bag = bag.newWith("7"), hashBag.toImmutable()); hashBag.add("8"); Assert.assertEquals(bag = bag.newWith("8"), hashBag.toImmutable()); hashBag.add("9"); Assert.assertEquals(bag = bag.newWith("9"), hashBag.toImmutable()); hashBag.add("10"); Assert.assertEquals(bag = bag.newWith("10"), hashBag.toImmutable()); hashBag.add("11"); Assert.assertEquals(bag = bag.newWith("11"), hashBag.toImmutable()); }
@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 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); }
@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())); }
@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())); }
@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); }
@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); } })); }
@Test public void immutables() { ImmutableBagFactory bagFactory = Bags.immutable; Assert.assertEquals(HashBag.newBag(), bagFactory.of()); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of()); Assert.assertEquals(HashBag.newBagWith(1), bagFactory.of(1)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1)); Assert.assertEquals(HashBag.newBagWith(1, 2), bagFactory.of(1, 2)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1, 2)); Assert.assertEquals(HashBag.newBagWith(1, 2, 3), bagFactory.of(1, 2, 3)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1, 2, 3)); Assert.assertEquals(HashBag.newBagWith(1, 2, 3, 4), bagFactory.of(1, 2, 3, 4)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1, 2, 3, 4)); Assert.assertEquals(HashBag.newBagWith(1, 2, 3, 4, 5), bagFactory.of(1, 2, 3, 4, 5)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1, 2, 3, 4, 5)); Assert.assertEquals(HashBag.newBagWith(1, 2, 3, 4, 5, 6), bagFactory.of(1, 2, 3, 4, 5, 6)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1, 2, 3, 4, 5, 6)); Assert.assertEquals( HashBag.newBagWith(1, 2, 3, 4, 5, 6, 7), bagFactory.of(1, 2, 3, 4, 5, 6, 7)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1, 2, 3, 4, 5, 6, 7)); Assert.assertEquals( HashBag.newBagWith(1, 2, 3, 4, 5, 6, 7, 8), bagFactory.of(1, 2, 3, 4, 5, 6, 7, 8)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1, 2, 3, 4, 5, 6, 7, 8)); Assert.assertEquals( HashBag.newBagWith(1, 2, 3, 4, 5, 6, 7, 8, 9), bagFactory.of(1, 2, 3, 4, 5, 6, 7, 8, 9)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1, 2, 3, 4, 5, 6, 7, 8, 9)); Assert.assertEquals( HashBag.newBagWith(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), bagFactory.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); Assert.assertEquals(HashBag.newBagWith(3, 2, 1), bagFactory.ofAll(HashBag.newBagWith(1, 2, 3))); Verify.assertInstanceOf(ImmutableBag.class, bagFactory.ofAll(HashBag.newBagWith(1, 2, 3))); }
@Test public void selectInstancesOf() { MutableBag<Number> numbers = UnmodifiableBag.of(HashBag.<Number>newBagWith(1, 2.0, 3, 4.0, 5)); Assert.assertEquals(iBag(1, 3, 5), numbers.selectInstancesOf(Integer.class)); Assert.assertEquals(iBag(1, 2.0, 3, 4.0, 5), numbers.selectInstancesOf(Number.class)); }