/*
* Copyright 2015 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.impl.bag.immutable;
import com.gs.collections.api.bag.ImmutableBag;
import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.bag.primitive.ImmutableBooleanBag;
import com.gs.collections.api.list.ImmutableList;
import com.gs.collections.api.multimap.Multimap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.tuple.primitive.ObjectIntPair;
import com.gs.collections.impl.bag.mutable.HashBag;
import com.gs.collections.impl.block.function.NegativeIntervalFunction;
import com.gs.collections.impl.factory.Maps;
import com.gs.collections.impl.list.Interval;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.multimap.bag.HashBagMultimap;
import com.gs.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;
import static com.gs.collections.impl.factory.Iterables.iBag;
public class ImmutableHashBagTest extends ImmutableBagTestCase
{
@Override
protected ImmutableBag<String> newBag()
{
return ImmutableHashBag.newBagWith("1", "2", "2", "3", "3", "3", "4", "4", "4", "4");
}
@Override
protected int numKeys()
{
return 4;
}
@Override
@Test
public void testSize()
{
Verify.assertIterableSize(10, this.newBag());
}
@Override
public void toStringOfItemToCount()
{
Assert.assertEquals("{}", ImmutableHashBag.newBag().toStringOfItemToCount());
Assert.assertEquals("{1=3}", ImmutableHashBag.newBagWith("1", "1", "1").toStringOfItemToCount());
String actual = ImmutableHashBag.newBagWith("1", "2", "2").toStringOfItemToCount();
Assert.assertTrue("{1=1, 2=2}".equals(actual) || "{2=2, 1=1}".equals(actual));
}
@Override
@Test
public void selectInstancesOf()
{
super.selectInstancesOf();
ImmutableBag<Number> numbers = ImmutableHashBag.<Number>newBagWith(1, 2.0, 2.0, 3, 3, 3, 4.0, 4.0, 4.0, 4.0);
Assert.assertEquals(iBag(1, 3, 3, 3), numbers.selectInstancesOf(Integer.class));
Assert.assertEquals(iBag(2.0, 2.0, 4.0, 4.0, 4.0, 4.0), numbers.selectInstancesOf(Double.class));
}
@Override
@Test
public void collectBoolean()
{
ImmutableBooleanBag result = this.newBag().collectBoolean("4"::equals);
Assert.assertEquals(2, result.sizeDistinct());
Assert.assertEquals(4, result.occurrencesOf(true));
Assert.assertEquals(6, result.occurrencesOf(false));
}
@Test
public void testNewBag()
{
ImmutableHashBag<Object> immutableHashBag = ImmutableHashBag.newBagWith(HashBag.newBag().with(1, 2, 3, 4));
Verify.assertSize(4, immutableHashBag);
Assert.assertEquals(FastList.newListWith(1, 2, 3, 4), immutableHashBag.toSortedList());
}
@Override
@Test
public void groupByEach()
{
ImmutableBag<Integer> immutableBag = ImmutableHashBag.newBagWith(1, 2, 2, 3, 3, 3, 4, 4, 4, 4);
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);
}
@Override
@Test
public void groupByUniqueKey()
{
ImmutableBag<Integer> immutableBag = ImmutableHashBag.newBagWith(1, 2, 3);
Assert.assertEquals(Maps.immutable.of(1, 1, 2, 2, 3, 3), immutableBag.groupByUniqueKey(id -> id));
}
@Override
@Test
public void groupByUniqueKey_target()
{
ImmutableBag<Integer> immutableBag = ImmutableHashBag.newBagWith(1, 2, 3);
Assert.assertEquals(Maps.immutable.of(0, 0, 1, 1, 2, 2, 3, 3), immutableBag.groupByUniqueKey(id -> id, UnifiedMap.newWithKeysValues(0, 0)));
}
@Test
public void topOccurrences()
{
MutableBag<String> mutable = HashBag.newBag();
mutable.addOccurrences("one", 1);
mutable.addOccurrences("two", 2);
mutable.addOccurrences("three", 3);
mutable.addOccurrences("four", 4);
mutable.addOccurrences("five", 5);
mutable.addOccurrences("six", 6);
mutable.addOccurrences("seven", 7);
mutable.addOccurrences("eight", 8);
mutable.addOccurrences("nine", 9);
mutable.addOccurrences("ten", 10);
ImmutableBag<String> strings = ImmutableHashBag.newBagWith(mutable);
ImmutableList<ObjectIntPair<String>> top5 = strings.topOccurrences(5);
Verify.assertIterableSize(5, top5);
Assert.assertEquals("ten", top5.getFirst().getOne());
Assert.assertEquals(10, top5.getFirst().getTwo());
Assert.assertEquals("six", top5.getLast().getOne());
Assert.assertEquals(6, top5.getLast().getTwo());
Verify.assertIterableSize(0, ImmutableHashBag.newBagWith().topOccurrences(5));
Verify.assertIterableSize(3, this.newWith("one", "two", "three").topOccurrences(5));
Verify.assertIterableSize(3, this.newWith("one", "two", "three").topOccurrences(1));
Verify.assertIterableSize(3, this.newWith("one", "two", "three").topOccurrences(2));
Verify.assertIterableSize(3, this.newWith("one", "one", "two", "three").topOccurrences(2));
Verify.assertIterableSize(2, this.newWith("one", "one", "two", "two", "three").topOccurrences(1));
Verify.assertIterableSize(3, this.newWith(null, "one", "two").topOccurrences(5));
Verify.assertIterableSize(3, this.newWith(null, "one", "two").topOccurrences(1));
Verify.assertIterableSize(3, this.newWith("one", "one", "two", "two", "three", "three").topOccurrences(1));
Verify.assertIterableSize(0, this.newWith("one").newWithout("one").topOccurrences(0));
Verify.assertIterableSize(0, this.newWith("one").topOccurrences(0));
Verify.assertThrows(IllegalArgumentException.class, () -> this.newWith("one").topOccurrences(-1));
}
@Test
public void bottomOccurrences()
{
MutableBag<String> mutable = HashBag.newBag();
mutable.addOccurrences("one", 1);
mutable.addOccurrences("two", 2);
mutable.addOccurrences("three", 3);
mutable.addOccurrences("four", 4);
mutable.addOccurrences("five", 5);
mutable.addOccurrences("six", 6);
mutable.addOccurrences("seven", 7);
mutable.addOccurrences("eight", 8);
mutable.addOccurrences("nine", 9);
mutable.addOccurrences("ten", 10);
ImmutableBag<String> strings = ImmutableHashBag.newBagWith(mutable);
ImmutableList<ObjectIntPair<String>> bottom5 = strings.bottomOccurrences(5);
Verify.assertIterableSize(5, bottom5);
Assert.assertEquals("one", bottom5.getFirst().getOne());
Assert.assertEquals(1, bottom5.getFirst().getTwo());
Assert.assertEquals("five", bottom5.getLast().getOne());
Assert.assertEquals(5, bottom5.getLast().getTwo());
Verify.assertIterableSize(0, ImmutableHashBag.newBagWith().bottomOccurrences(5));
Verify.assertIterableSize(3, this.newWith("one", "two", "three").bottomOccurrences(5));
Verify.assertIterableSize(3, this.newWith("one", "two", "three").bottomOccurrences(1));
Verify.assertIterableSize(3, this.newWith("one", "two", "three").bottomOccurrences(2));
Verify.assertIterableSize(3, this.newWith("one", "one", "two", "two", "three").bottomOccurrences(2));
Verify.assertIterableSize(3, this.newWith("one", "one", "two", "two", "three", "three").bottomOccurrences(1));
Verify.assertIterableSize(3, this.newWith(null, "one", "two").bottomOccurrences(5));
Verify.assertIterableSize(3, this.newWith(null, "one", "two").bottomOccurrences(1));
Verify.assertIterableSize(3, this.newWith("one", "one", "two", "two", "three", "three").bottomOccurrences(1));
Verify.assertIterableSize(0, this.newWith("one").newWithout("one").bottomOccurrences(0));
Verify.assertIterableSize(0, this.newWith("one").bottomOccurrences(0));
Verify.assertThrows(IllegalArgumentException.class, () -> this.newWith("one").bottomOccurrences(-1));
}
}