/*
* 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.multimap.bag;
import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.multimap.bag.BagMultimap;
import com.gs.collections.api.multimap.bag.MutableBagMultimap;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.bag.mutable.HashBag;
import com.gs.collections.impl.factory.Bags;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.multimap.AbstractMutableMultimapTestCase;
import com.gs.collections.impl.test.Verify;
import com.gs.collections.impl.tuple.Tuples;
import com.gs.collections.impl.utility.Iterate;
import org.junit.Assert;
import org.junit.Test;
public abstract class AbstractMutableBagMultimapTestCase extends AbstractMutableMultimapTestCase
{
@Override
protected abstract <K, V> MutableBagMultimap<K, V> newMultimap();
@Override
protected abstract <K, V> MutableBagMultimap<K, V> newMultimapWithKeyValue(K key, V value);
@Override
protected abstract <K, V> MutableBagMultimap<K, V> newMultimapWithKeysValues(K key1, V value1, K key2, V value2);
@Override
protected abstract <K, V> MutableBagMultimap<K, V> newMultimapWithKeysValues(
K key1, V value1,
K key2, V value2,
K key3, V value3);
@Override
protected abstract <K, V> MutableBagMultimap<K, V> newMultimapWithKeysValues(
K key1, V value1,
K key2, V value2,
K key3, V value3,
K key4, V value4);
@Override
protected abstract <K, V> MutableBagMultimap<K, V> newMultimap(Pair<K, V>... pairs);
@Override
protected abstract <K, V> MutableBagMultimap<K, V> newMultimapFromPairs(Iterable<Pair<K, V>> inputIterable);
@Override
protected abstract <V> MutableBag<V> createCollection(V... args);
@Override
@Test
public void flip()
{
BagMultimap<String, Integer> multimap = this.newMultimapWithKeysValues("Less than 2", 1, "Less than 3", 1, "Less than 3", 2, "Less than 3", 2);
BagMultimap<Integer, String> flipped = multimap.flip();
Assert.assertEquals(Bags.immutable.with("Less than 3", "Less than 3"), flipped.get(2));
Assert.assertEquals(Bags.immutable.with("Less than 2", "Less than 3"), flipped.get(1));
}
@Override
@Test
public void selectKeysValues()
{
super.selectKeysValues();
MutableBagMultimap<String, Integer> multimap = this.newMultimap();
multimap.putAll("One", FastList.newListWith(1, 2, 3, 4, 4));
multimap.putAll("Two", FastList.newListWith(2, 3, 4, 5, 3, 2));
MutableBagMultimap<String, Integer> selectedMultimap = multimap.selectKeysValues((key, value) -> ("Two".equals(key) && (value % 2 == 0)));
MutableBagMultimap<String, Integer> expectedMultimap = HashBagMultimap.newMultimap();
expectedMultimap.putAll("Two", FastList.newListWith(2, 4, 2));
Assert.assertEquals(expectedMultimap, selectedMultimap);
}
@Override
@Test
public void rejectKeysValues()
{
super.rejectKeysValues();
MutableBagMultimap<String, Integer> multimap = this.newMultimap();
multimap.putAll("One", FastList.newListWith(1, 2, 3, 4, 1));
multimap.putAll("Two", FastList.newListWith(2, 3, 4, 5));
MutableBagMultimap<String, Integer> rejectedMultimap = multimap.rejectKeysValues((key, value) -> ("Two".equals(key) || (value % 2 == 0)));
MutableBagMultimap<String, Integer> expectedMultimap = HashBagMultimap.newMultimap();
expectedMultimap.putAll("One", FastList.newListWith(1, 3, 1));
Assert.assertEquals(expectedMultimap, rejectedMultimap);
}
@Override
@Test
public void selectKeysMultiValues()
{
super.selectKeysMultiValues();
MutableBagMultimap<Integer, String> multimap = this.newMultimap();
multimap.putAll(1, FastList.newListWith("1", "3", "4"));
multimap.putAll(2, FastList.newListWith("2", "3", "4", "5", "2"));
multimap.putAll(3, FastList.newListWith("2", "3", "4", "5", "2"));
multimap.putAll(4, FastList.newListWith("1", "3", "4"));
MutableBagMultimap<Integer, String> selectedMultimap = multimap.selectKeysMultiValues((key, values) -> (key % 2 == 0 && Iterate.sizeOf(values) > 3));
MutableBagMultimap<Integer, String> expectedMultimap = HashBagMultimap.newMultimap();
expectedMultimap.putAll(2, FastList.newListWith("2", "3", "4", "5", "2"));
Assert.assertEquals(expectedMultimap, selectedMultimap);
}
@Override
@Test
public void rejectKeysMultiValues()
{
super.rejectKeysMultiValues();
MutableBagMultimap<Integer, String> multimap = this.newMultimap();
multimap.putAll(1, FastList.newListWith("1", "2", "3", "4", "1"));
multimap.putAll(2, FastList.newListWith("2", "3", "4", "5", "1"));
multimap.putAll(3, FastList.newListWith("2", "3", "4", "2"));
multimap.putAll(4, FastList.newListWith("1", "3", "4", "5"));
MutableBagMultimap<Integer, String> rejectedMultimap = multimap.rejectKeysMultiValues((key, values) -> (key % 2 == 0 || Iterate.sizeOf(values) > 4));
MutableBagMultimap<Integer, String> expectedMultimap = HashBagMultimap.newMultimap();
expectedMultimap.putAll(3, FastList.newListWith("2", "3", "4", "2"));
Assert.assertEquals(expectedMultimap, rejectedMultimap);
}
@Override
@Test
public void collectKeysValues()
{
super.collectKeysValues();
MutableBagMultimap<String, Integer> multimap = this.newMultimap();
multimap.putAll("1", FastList.newListWith(1, 2, 3, 4, 4));
multimap.putAll("2", FastList.newListWith(2, 3, 4, 5, 3, 2));
MutableBagMultimap<Integer, String> collectedMultimap = multimap.collectKeysValues((key, value) -> Tuples.pair(Integer.valueOf(key), value + "Value"));
MutableBagMultimap<Integer, String> expectedMultimap = HashBagMultimap.newMultimap();
expectedMultimap.putAll(1, FastList.newListWith("1Value", "2Value", "3Value", "4Value", "4Value"));
expectedMultimap.putAll(2, FastList.newListWith("2Value", "3Value", "4Value", "5Value", "3Value", "2Value"));
Assert.assertEquals(expectedMultimap, collectedMultimap);
MutableBagMultimap<Integer, String> collectedMultimap2 = multimap.collectKeysValues((key, value) -> Tuples.pair(1, value + "Value"));
MutableBagMultimap<Integer, String> expectedMultimap2 = HashBagMultimap.newMultimap();
expectedMultimap2.putAll(1, FastList.newListWith("1Value", "2Value", "3Value", "4Value", "4Value"));
expectedMultimap2.putAll(1, FastList.newListWith("2Value", "3Value", "4Value", "5Value", "3Value", "2Value"));
Assert.assertEquals(expectedMultimap2, collectedMultimap2);
}
@Override
@Test
public void collectValues()
{
super.collectValues();
MutableBagMultimap<String, Integer> multimap = this.newMultimap();
multimap.putAll("1", FastList.newListWith(1, 2, 3, 4, 4));
multimap.putAll("2", FastList.newListWith(2, 3, 4, 5, 3, 2));
MutableBagMultimap<String, String> collectedMultimap = multimap.collectValues(value -> value + "Value");
MutableBagMultimap<String, String> expectedMultimap = HashBagMultimap.newMultimap();
expectedMultimap.putAll("1", FastList.newListWith("1Value", "2Value", "3Value", "4Value", "4Value"));
expectedMultimap.putAll("2", FastList.newListWith("2Value", "3Value", "4Value", "5Value", "3Value", "2Value"));
Assert.assertEquals(expectedMultimap, collectedMultimap);
}
@Test
public void putOccurrences()
{
MutableBagMultimap<String, String> multimap = this.newMultimap();
Verify.assertThrows(IllegalArgumentException.class, () -> multimap.putOccurrences("1", "a", -1));
multimap.putOccurrences("1", "a", 0);
Verify.assertEmpty(multimap);
multimap.putOccurrences("2", "b", 1);
Verify.assertSize(1, multimap);
Verify.assertBagsEqual(HashBag.<String>newBagWith("b"), multimap.get("2"));
multimap.putOccurrences("2", "b", 2);
Verify.assertSize(3, multimap);
Verify.assertBagsEqual(HashBag.<String>newBagWith("b", "b", "b"), multimap.get("2"));
multimap.putOccurrences("2", "b", 0);
Verify.assertSize(3, multimap);
Verify.assertBagsEqual(HashBag.<String>newBagWith("b", "b", "b"), multimap.get("2"));
Verify.assertThrows(IllegalArgumentException.class, () -> multimap.putOccurrences("2", "b", -1));
multimap.putOccurrences("2", "c", 2);
Verify.assertSize(5, multimap);
Verify.assertBagsEqual(HashBag.<String>newBagWith("b", "b", "b", "c", "c"), multimap.get("2"));
multimap.putOccurrences("3", "d", 3);
Verify.assertSize(8, multimap);
Verify.assertBagsEqual(HashBag.<String>newBagWith("b", "b", "b", "c", "c"), multimap.get("2"));
Verify.assertBagsEqual(HashBag.<String>newBagWith("d", "d", "d"), multimap.get("3"));
}
}