diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/factory/Multimaps.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/factory/Multimaps.java index b1722c8008..081ab86439 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/factory/Multimaps.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/factory/Multimaps.java @@ -306,21 +306,41 @@ private ImmutableSortedBagMultimapFactory() { } + public ImmutableSortedBagMultimap of() + { + return this.of(null); + } + public ImmutableSortedBagMultimap of(Comparator comparator) { return new ImmutableSortedBagMultimapImpl<>(Maps.immutable.empty(), comparator); } + public ImmutableSortedBagMultimap with() + { + return this.of(); + } + public ImmutableSortedBagMultimap with(Comparator comparator) { return this.of(comparator); } + public ImmutableSortedBagMultimap of(K key, V value) + { + return this.with(key, value); + } + public ImmutableSortedBagMultimap of(Comparator comparator, K key, V value) { return this.with(comparator, key, value); } + public ImmutableSortedBagMultimap with(K key, V value) + { + return this.with(null, key, value); + } + public ImmutableSortedBagMultimap with(Comparator comparator, K key, V value) { TreeBagMultimap treeBagMultimap = new TreeBagMultimap<>(comparator); @@ -328,6 +348,15 @@ public ImmutableSortedBagMultimap with(Comparator compar return treeBagMultimap.toImmutable(); } + public ImmutableSortedBagMultimap of( + K key1, + V value1, + K key2, + V value2) + { + return this.with(key1, value1, key2, value2); + } + public ImmutableSortedBagMultimap of( Comparator comparator, K key1, @@ -338,6 +367,15 @@ public ImmutableSortedBagMultimap of( return this.with(comparator, key1, value1, key2, value2); } + public ImmutableSortedBagMultimap with( + K key1, + V value1, + K key2, + V value2) + { + return this.with(null, key1, value1, key2, value2); + } + public ImmutableSortedBagMultimap with( Comparator comparator, K key1, @@ -351,6 +389,17 @@ public ImmutableSortedBagMultimap with( return treeBagMultimap.toImmutable(); } + public ImmutableSortedBagMultimap of( + K key1, + V value1, + K key2, + V value2, + K key3, + V value3) + { + return this.with(key1, value1, key2, value2, key3, value3); + } + public ImmutableSortedBagMultimap of( Comparator comparator, K key1, @@ -363,6 +412,17 @@ public ImmutableSortedBagMultimap of( return this.with(comparator, key1, value1, key2, value2, key3, value3); } + public ImmutableSortedBagMultimap with( + K key1, + V value1, + K key2, + V value2, + K key3, + V value3) + { + return this.with(null, key1, value1, key2, value2, key3, value3); + } + public ImmutableSortedBagMultimap with( Comparator comparator, K key1, @@ -664,29 +724,94 @@ public MutableSortedBagMultimap empty() return TreeBagMultimap.newMultimap(); } + public MutableSortedBagMultimap empty(Comparator comparator) + { + return TreeBagMultimap.newMultimap(comparator); + } + + public MutableSortedBagMultimap of() + { + return this.empty(); + } + + public MutableSortedBagMultimap of(Comparator comparator) + { + return this.empty(comparator); + } + public MutableSortedBagMultimap with() { return this.empty(); } + public MutableSortedBagMultimap with(Comparator comparator) + { + return this.empty(comparator); + } + + public MutableSortedBagMultimap of(K key, V value) + { + return this.with(key, value); + } + + public MutableSortedBagMultimap of(Comparator comparator, K key, V value) + { + return this.with(comparator, key, value); + } + public MutableSortedBagMultimap with(K key, V value) { - TreeBagMultimap treeBagMultimap = TreeBagMultimap.newMultimap(); + return this.with(null, key, value); + } + + public MutableSortedBagMultimap with(Comparator comparator, K key, V value) + { + TreeBagMultimap treeBagMultimap = TreeBagMultimap.newMultimap(comparator); treeBagMultimap.put(key, value); return treeBagMultimap; } + public MutableSortedBagMultimap of(K key1, V value1, K key2, V value2) + { + return this.with(key1, value1, key2, value2); + } + + public MutableSortedBagMultimap of(Comparator comparator, K key1, V value1, K key2, V value2) + { + return this.with(comparator, key1, value1, key2, value2); + } + public MutableSortedBagMultimap with(K key1, V value1, K key2, V value2) { - TreeBagMultimap treeBagMultimap = TreeBagMultimap.newMultimap(); + return this.with(null, key1, value1, key2, value2); + } + + public MutableSortedBagMultimap with(Comparator comparator, K key1, V value1, K key2, V value2) + { + TreeBagMultimap treeBagMultimap = TreeBagMultimap.newMultimap(comparator); treeBagMultimap.put(key1, value1); treeBagMultimap.put(key2, value2); return treeBagMultimap; } + public MutableSortedBagMultimap of(K key1, V value1, K key2, V value2, K key3, V value3) + { + return this.with(key1, value1, key2, value2, key3, value3); + } + + public MutableSortedBagMultimap of(Comparator comparator, K key1, V value1, K key2, V value2, K key3, V value3) + { + return this.with(comparator, key1, value1, key2, value2, key3, value3); + } + public MutableSortedBagMultimap with(K key1, V value1, K key2, V value2, K key3, V value3) { - TreeBagMultimap treeBagMultimap = TreeBagMultimap.newMultimap(); + return this.with(null, key1, value1, key2, value2, key3, value3); + } + + public MutableSortedBagMultimap with(Comparator comparator, K key1, V value1, K key2, V value2, K key3, V value3) + { + TreeBagMultimap treeBagMultimap = TreeBagMultimap.newMultimap(comparator); treeBagMultimap.put(key1, value1); treeBagMultimap.put(key2, value2); treeBagMultimap.put(key3, value3); diff --git a/unit-tests/src/test/java/org/eclipse/collections/impl/factory/MultimapsTest.java b/unit-tests/src/test/java/org/eclipse/collections/impl/factory/MultimapsTest.java index a49fea71ef..32aed36319 100644 --- a/unit-tests/src/test/java/org/eclipse/collections/impl/factory/MultimapsTest.java +++ b/unit-tests/src/test/java/org/eclipse/collections/impl/factory/MultimapsTest.java @@ -90,19 +90,79 @@ public void immutableSortedSet() @Test public void immutableSortedBagMultimap() { - ImmutableSortedBagMultimap empty = Multimaps.immutable.sortedBag.of(Integer::compareTo); ImmutableSortedBagMultimap emptyWith = Multimaps.immutable.sortedBag.with(Integer::compareTo); - Verify.assertEmpty(empty); + ImmutableSortedBagMultimap emptyOf = Multimaps.immutable.sortedBag.of(Integer::compareTo); Verify.assertEmpty(emptyWith); - ImmutableSortedBagMultimap one = Multimaps.immutable.sortedBag.of(Integer::compareTo, 1, 1); - Assert.assertEquals(TreeBagMultimap.newMultimap(Tuples.pair(1, 1)), one); - ImmutableSortedBagMultimap two = Multimaps.immutable.sortedBag.of(Integer::compareTo, 1, 1, 2, 2); - ImmutableSortedBagMultimap toStringOffTwo = Multimaps.immutable.sortedBag.of(String::compareTo, "A", "B"); - Assert.assertEquals(TreeBagMultimap.newMultimap(Tuples.pair("A", "B")), toStringOffTwo); - Assert.assertEquals(TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2)), two); - ImmutableSortedBagMultimap three = Multimaps.immutable.sortedBag.of(Integer::compareTo, 1, 1, 2, 2, 3, 3); + Assert.assertNotNull(emptyWith.comparator()); + Verify.assertEmpty(emptyOf); + Assert.assertNotNull(emptyOf.comparator()); + ImmutableSortedBagMultimap oneOf = Multimaps.immutable.sortedBag.of(Integer::compareTo, 1, 1); + ImmutableSortedBagMultimap oneWith = Multimaps.immutable.sortedBag.with(Integer::compareTo, 1, 1); + TreeBagMultimap expectedOne = TreeBagMultimap.newMultimap(Tuples.pair(1, 1)); + Assert.assertEquals(expectedOne, oneOf); + Assert.assertNotNull(oneOf.comparator()); + Assert.assertEquals(expectedOne, oneWith); + Assert.assertNotNull(oneWith.comparator()); + ImmutableSortedBagMultimap toStringOfTwo = Multimaps.immutable.sortedBag.of(String::compareTo, "A", "B"); + ImmutableSortedBagMultimap toStringWithTwo = Multimaps.immutable.sortedBag.with(String::compareTo, "A", "B"); + TreeBagMultimap expectedStringOfTwo = TreeBagMultimap.newMultimap(Tuples.pair("A", "B")); + Assert.assertEquals(expectedStringOfTwo, toStringOfTwo); + Assert.assertNotNull(toStringOfTwo.comparator()); + Assert.assertEquals(expectedStringOfTwo, toStringWithTwo); + Assert.assertNotNull(toStringWithTwo.comparator()); + TreeBagMultimap expectedTwo = TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2)); + ImmutableSortedBagMultimap twoOf = Multimaps.immutable.sortedBag.of(Integer::compareTo, 1, 1, 2, 2); + ImmutableSortedBagMultimap twoWith = Multimaps.immutable.sortedBag.with(Integer::compareTo, 1, 1, 2, 2); + Assert.assertEquals(expectedTwo, twoOf); + Assert.assertNotNull(twoOf); + Assert.assertEquals(expectedTwo, twoWith); + Assert.assertNotNull(twoWith.comparator()); + ImmutableSortedBagMultimap threeOf = Multimaps.immutable.sortedBag.of(Integer::compareTo, 1, 1, 2, 2, 3, 3); + ImmutableSortedBagMultimap threeWith = Multimaps.immutable.sortedBag.with(Integer::compareTo, 1, 1, 2, 2, 3, 3); SortedBagMultimap expectedThree = TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2), Tuples.pair(3, 3)); - Assert.assertEquals(expectedThree, three); + Assert.assertEquals(expectedThree, threeOf); + Assert.assertNotNull(threeOf.comparator()); + Assert.assertEquals(expectedThree, threeWith); + Assert.assertNotNull(threeWith.comparator()); + } + + @Test + public void immutableSortedBagMultimapWithoutComparator() + { + ImmutableSortedBagMultimap emptyWith = Multimaps.immutable.sortedBag.with(); + ImmutableSortedBagMultimap emptyOf = Multimaps.immutable.sortedBag.of(); + Verify.assertEmpty(emptyWith); + Assert.assertNull(emptyWith.comparator()); + Verify.assertEmpty(emptyOf); + Assert.assertNull(emptyOf.comparator()); + ImmutableSortedBagMultimap oneOf = Multimaps.immutable.sortedBag.of(1, 1); + ImmutableSortedBagMultimap oneWith = Multimaps.immutable.sortedBag.with(1, 1); + TreeBagMultimap expectedOne = TreeBagMultimap.newMultimap(Tuples.pair(1, 1)); + Assert.assertEquals(expectedOne, oneOf); + Assert.assertNull(oneOf.comparator()); + Assert.assertEquals(expectedOne, oneWith); + Assert.assertNull(oneWith.comparator()); + ImmutableSortedBagMultimap toStringOfTwo = Multimaps.immutable.sortedBag.of("A", "B"); + ImmutableSortedBagMultimap toStringWithTwo = Multimaps.immutable.sortedBag.with("A", "B"); + TreeBagMultimap expectedStringOfTwo = TreeBagMultimap.newMultimap(Tuples.pair("A", "B")); + Assert.assertEquals(expectedStringOfTwo, toStringOfTwo); + Assert.assertNull(toStringOfTwo.comparator()); + Assert.assertEquals(expectedStringOfTwo, toStringWithTwo); + Assert.assertNull(toStringWithTwo.comparator()); + TreeBagMultimap expectedTwo = TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2)); + ImmutableSortedBagMultimap twoOf = Multimaps.immutable.sortedBag.of(1, 1, 2, 2); + ImmutableSortedBagMultimap twoWith = Multimaps.immutable.sortedBag.with(1, 1, 2, 2); + Assert.assertEquals(expectedTwo, twoOf); + Assert.assertNotNull(twoOf); + Assert.assertEquals(expectedTwo, twoWith); + Assert.assertNull(twoWith.comparator()); + ImmutableSortedBagMultimap threeOf = Multimaps.immutable.sortedBag.of(1, 1, 2, 2, 3, 3); + ImmutableSortedBagMultimap threeWith = Multimaps.immutable.sortedBag.with(1, 1, 2, 2, 3, 3); + SortedBagMultimap expectedThree = TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2), Tuples.pair(3, 3)); + Assert.assertEquals(expectedThree, threeOf); + Assert.assertNull(threeOf.comparator()); + Assert.assertEquals(expectedThree, threeWith); + Assert.assertNull(threeWith.comparator()); } @Test @@ -194,18 +254,85 @@ public void mutableSortedBagMultimap() { MutableSortedBagMultimap empty = Multimaps.mutable.sortedBag.empty(); MutableSortedBagMultimap emptyWith = Multimaps.mutable.sortedBag.with(); + MutableSortedBagMultimap emptyOf = Multimaps.mutable.sortedBag.of(); Verify.assertEmpty(empty); + Assert.assertNull(empty.comparator()); Verify.assertEmpty(emptyWith); - MutableSortedBagMultimap one = Multimaps.mutable.sortedBag.with(1, 1); - Assert.assertEquals(TreeBagMultimap.newMultimap(Tuples.pair(1, 1)), one); - MutableSortedBagMultimap two = Multimaps.mutable.sortedBag.with(1, 1, 2, 2); - MutableSortedBagMultimap toStringOffTwo = Multimaps.mutable.sortedBag.with("A", "B"); - Assert.assertEquals(TreeBagMultimap.newMultimap(Tuples.pair("A", "B")), toStringOffTwo); - Assert.assertEquals(TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2)), two); - MutableSortedBagMultimap three = Multimaps.mutable.sortedBag.with(1, 1, 2, 2, 3, 3); + Assert.assertNull(emptyWith.comparator()); + Verify.assertEmpty(emptyOf); + Assert.assertNull(emptyOf.comparator()); + TreeBagMultimap expectedOne = TreeBagMultimap.newMultimap(Tuples.pair(1, 1)); + MutableSortedBagMultimap withOne = Multimaps.mutable.sortedBag.with(1, 1); + Assert.assertEquals(expectedOne, withOne); + Assert.assertNull(withOne.comparator()); + MutableSortedBagMultimap ofOne = Multimaps.mutable.sortedBag.of(1, 1); + Assert.assertEquals(expectedOne, ofOne); + Assert.assertNull(ofOne.comparator()); + TreeBagMultimap expectedOneStrings = TreeBagMultimap.newMultimap(Tuples.pair("A", "B")); + MutableSortedBagMultimap withStrings = Multimaps.mutable.sortedBag.with("A", "B"); + Assert.assertEquals(expectedOneStrings, withStrings); + Assert.assertNull(withStrings.comparator()); + MutableSortedBagMultimap ofStrings = Multimaps.mutable.sortedBag.of("A", "B"); + Assert.assertEquals(expectedOneStrings, ofStrings); + Assert.assertNull(ofStrings.comparator()); + TreeBagMultimap expectedTwo = TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2)); + MutableSortedBagMultimap withTwoItems = Multimaps.mutable.sortedBag.with(1, 1, 2, 2); + Assert.assertEquals(expectedTwo, withTwoItems); + Assert.assertNull(withTwoItems.comparator()); + MutableSortedBagMultimap ofTwoItems = Multimaps.mutable.sortedBag.of(1, 1, 2, 2); + Assert.assertEquals(expectedTwo, ofTwoItems); + Assert.assertNull(ofTwoItems.comparator()); + MutableSortedBagMultimap threeWith = Multimaps.mutable.sortedBag.with(1, 1, 2, 2, 3, 3); + Assert.assertNull(threeWith.comparator()); + MutableSortedBagMultimap threeOf = Multimaps.mutable.sortedBag.of(1, 1, 2, 2, 3, 3); + Assert.assertNull(threeOf.comparator()); SortedBagMultimap expectedThree = TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2), Tuples.pair(3, 3)); - Assert.assertEquals(expectedThree, three); - Assert.assertEquals(expectedThree, Multimaps.mutable.sortedBag.withAll(three)); + Assert.assertEquals(expectedThree, threeWith); + Assert.assertEquals(expectedThree, threeOf); + Assert.assertEquals(expectedThree, Multimaps.mutable.sortedBag.withAll(threeWith)); + } + + @Test + public void mutableSortedBagMultimapWithComparator() + { + MutableSortedBagMultimap empty = Multimaps.mutable.sortedBag.empty(String::compareTo); + MutableSortedBagMultimap emptyWith = Multimaps.mutable.sortedBag.with(String::compareTo); + MutableSortedBagMultimap emptyOf = Multimaps.mutable.sortedBag.of(String::compareTo); + Verify.assertEmpty(empty); + Assert.assertNotNull(empty.comparator()); + Verify.assertEmpty(emptyWith); + Assert.assertNotNull(emptyWith.comparator()); + Verify.assertEmpty(emptyOf); + Assert.assertNotNull(emptyOf.comparator()); + TreeBagMultimap expectedOne = TreeBagMultimap.newMultimap(Tuples.pair(1, 1)); + MutableSortedBagMultimap withOne = Multimaps.mutable.sortedBag.with(Integer::compareTo, 1, 1); + Assert.assertEquals(expectedOne, withOne); + Assert.assertNotNull(withOne.comparator()); + MutableSortedBagMultimap ofOne = Multimaps.mutable.sortedBag.of(Integer::compareTo, 1, 1); + Assert.assertEquals(expectedOne, ofOne); + Assert.assertNotNull(ofOne.comparator()); + TreeBagMultimap expectedOneStrings = TreeBagMultimap.newMultimap(Tuples.pair("A", "B")); + MutableSortedBagMultimap withStrings = Multimaps.mutable.sortedBag.with(String::compareTo, "A", "B"); + Assert.assertEquals(expectedOneStrings, withStrings); + Assert.assertNotNull(withStrings.comparator()); + MutableSortedBagMultimap ofStrings = Multimaps.mutable.sortedBag.of(String::compareTo, "A", "B"); + Assert.assertEquals(expectedOneStrings, ofStrings); + Assert.assertNotNull(ofStrings.comparator()); + TreeBagMultimap expectedTwo = TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2)); + MutableSortedBagMultimap withTwoItems = Multimaps.mutable.sortedBag.with(Integer::compareTo, 1, 1, 2, 2); + Assert.assertEquals(expectedTwo, withTwoItems); + Assert.assertNotNull(withTwoItems.comparator()); + MutableSortedBagMultimap ofTwoItems = Multimaps.mutable.sortedBag.of(Integer::compareTo, 1, 1, 2, 2); + Assert.assertEquals(expectedTwo, ofTwoItems); + Assert.assertNotNull(ofTwoItems.comparator()); + MutableSortedBagMultimap threeWith = Multimaps.mutable.sortedBag.with(Integer::compareTo, 1, 1, 2, 2, 3, 3); + Assert.assertNotNull(threeWith.comparator()); + MutableSortedBagMultimap threeOf = Multimaps.mutable.sortedBag.of(Integer::compareTo, 1, 1, 2, 2, 3, 3); + Assert.assertNotNull(threeOf.comparator()); + SortedBagMultimap expectedThree = TreeBagMultimap.newMultimap(Tuples.pair(1, 1), Tuples.pair(2, 2), Tuples.pair(3, 3)); + Assert.assertEquals(expectedThree, threeWith); + Assert.assertEquals(expectedThree, threeOf); + Assert.assertEquals(expectedThree, Multimaps.mutable.sortedBag.withAll(threeWith)); } @Test