Skip to content

Commit 487fbf0

Browse files
committed
Adds JImmutableTemplateSetMap.
1 parent e1990ea commit 487fbf0

File tree

3 files changed

+201
-28
lines changed

3 files changed

+201
-28
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
///###////////////////////////////////////////////////////////////////////////
2+
//
3+
// Burton Computer Corporation
4+
// http://www.burton-computer.com
5+
//
6+
// Copyright (c) 2017, Burton Computer Corporation
7+
// All rights reserved.
8+
//
9+
// Redistribution and use in source and binary forms, with or without
10+
// modification, are permitted provided that the following conditions are met:
11+
//
12+
// Redistributions of source code must retain the above copyright
13+
// notice, this list of conditions and the following disclaimer.
14+
//
15+
// Redistributions in binary form must reproduce the above copyright
16+
// notice, this list of conditions and the following disclaimer in
17+
// the documentation and/or other materials provided with the
18+
// distribution.
19+
//
20+
// Neither the name of the Burton Computer Corporation nor the names
21+
// of its contributors may be used to endorse or promote products
22+
// derived from this software without specific prior written permission.
23+
//
24+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28+
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+
36+
package org.javimmutable.collections.setmap;
37+
38+
import org.javimmutable.collections.JImmutableMap;
39+
import org.javimmutable.collections.JImmutableSet;
40+
import org.javimmutable.collections.JImmutableSetMap;
41+
42+
import javax.annotation.Nonnull;
43+
44+
/**
45+
* JImmutableSetMap implementation that uses arbitrary Map and Set templates.
46+
* Allows mix and match of map and set types to support all possible combinations.
47+
*/
48+
public class JImmutableTemplateSetMap<K, V>
49+
extends AbstractJImmutableSetMap<K, V>
50+
{
51+
private final JImmutableMap<K, JImmutableSet<V>> emptyMap;
52+
private final JImmutableSet<V> emptySet;
53+
54+
private JImmutableTemplateSetMap(JImmutableMap<K, JImmutableSet<V>> contents,
55+
JImmutableMap<K, JImmutableSet<V>> emptyMap,
56+
JImmutableSet<V> emptySet)
57+
{
58+
super(contents);
59+
this.emptyMap = emptyMap;
60+
this.emptySet = emptySet;
61+
}
62+
63+
private JImmutableTemplateSetMap(JImmutableMap<K, JImmutableSet<V>> emptyMap,
64+
JImmutableSet<V> emptySet)
65+
{
66+
this(emptyMap, emptyMap, emptySet);
67+
}
68+
69+
/**
70+
* Creates a new empty JImmutableSetMap object using the specified template map and set implementations.
71+
* The provided templates are always emptied before use.
72+
*/
73+
public static <K, V> JImmutableSetMap<K, V> of(@Nonnull JImmutableMap<K, JImmutableSet<V>> emptyMap,
74+
@Nonnull JImmutableSet<V> emptySet)
75+
{
76+
return new JImmutableTemplateSetMap<>(emptyMap.deleteAll(), emptySet.deleteAll());
77+
}
78+
79+
@Override
80+
public void checkInvariants()
81+
{
82+
}
83+
84+
@Override
85+
protected JImmutableSetMap<K, V> create(JImmutableMap<K, JImmutableSet<V>> map)
86+
{
87+
return new JImmutableTemplateSetMap<>(map, emptyMap, emptySet);
88+
}
89+
90+
@Nonnull
91+
@Override
92+
protected JImmutableSet<V> emptySet()
93+
{
94+
return emptySet;
95+
}
96+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
///###////////////////////////////////////////////////////////////////////////
2+
//
3+
// Burton Computer Corporation
4+
// http://www.burton-computer.com
5+
//
6+
// Copyright (c) 2017, Burton Computer Corporation
7+
// All rights reserved.
8+
//
9+
// Redistribution and use in source and binary forms, with or without
10+
// modification, are permitted provided that the following conditions are met:
11+
//
12+
// Redistributions of source code must retain the above copyright
13+
// notice, this list of conditions and the following disclaimer.
14+
//
15+
// Redistributions in binary form must reproduce the above copyright
16+
// notice, this list of conditions and the following disclaimer in
17+
// the documentation and/or other materials provided with the
18+
// distribution.
19+
//
20+
// Neither the name of the Burton Computer Corporation nor the names
21+
// of its contributors may be used to endorse or promote products
22+
// derived from this software without specific prior written permission.
23+
//
24+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28+
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+
36+
package org.javimmutable.collections.setmap;
37+
38+
import org.javimmutable.collections.JImmutableSet;
39+
import org.javimmutable.collections.JImmutableSetMap;
40+
import org.javimmutable.collections.MapEntry;
41+
import org.javimmutable.collections.cursors.StandardCursorTest;
42+
import org.javimmutable.collections.tree.ComparableComparator;
43+
import org.javimmutable.collections.tree.JImmutableTreeMap;
44+
import org.javimmutable.collections.tree.JImmutableTreeSet;
45+
46+
import java.util.Arrays;
47+
import java.util.Comparator;
48+
import java.util.TreeMap;
49+
50+
import static java.util.stream.Collectors.toList;
51+
52+
public class JImmutableTemplateSetMapTest
53+
extends AbstractJImmutableSetMapTestCase
54+
{
55+
public void testVarious()
56+
{
57+
final Comparator<Integer> reverse = ComparableComparator.<Integer>of().reversed();
58+
final JImmutableTreeMap<Integer, JImmutableSet<Integer>> emptyMap = JImmutableTreeMap.of();
59+
final JImmutableTreeSet<Integer> emptySet = JImmutableTreeSet.of(reverse);
60+
final JImmutableSetMap<Integer, Integer> empty = JImmutableTemplateSetMap.of(emptyMap.assign(1, emptySet.insert(10)),
61+
emptySet.insert(8).insert(25));
62+
assertEquals(true, empty.isEmpty());
63+
assertEquals(0, empty.count());
64+
assertEquals(0, empty.keys().count());
65+
assertNull(empty.get(1));
66+
JImmutableSetMap<Integer, Integer> map = verifyOperations(JImmutableTreeSetMap.of());
67+
verifyRandom(JImmutableTreeSetMap.of(), new TreeMap<>());
68+
StandardCursorTest.listCursorTest(Arrays.asList(1, 2, 3), map.keysCursor());
69+
StandardCursorTest.listCursorTest(Arrays.asList(MapEntry.of(1, map.getSet(1)),
70+
MapEntry.of(2, map.getSet(2)),
71+
MapEntry.of(3, map.getSet(3))),
72+
map.cursor());
73+
StandardCursorTest.listIteratorTest(Arrays.asList(MapEntry.of(1, map.getSet(1)),
74+
MapEntry.of(2, map.getSet(2)),
75+
MapEntry.of(3, map.getSet(3))),
76+
map.iterator());
77+
78+
map = empty
79+
.insert(10, 100)
80+
.insert(10, 111)
81+
.insert(7, 5)
82+
.insert(3, 8)
83+
.insert(7, 12)
84+
.insert(3, 90);
85+
assertEquals(Arrays.asList(3, 7, 10), map.keys().stream().collect(toList()));
86+
assertEquals(Arrays.asList(90, 8), map.getSet(3).stream().collect(toList()));
87+
assertEquals(Arrays.asList(12, 5), map.getSet(7).stream().collect(toList()));
88+
assertEquals(Arrays.asList(111, 100), map.getSet(10).stream().collect(toList()));
89+
}
90+
}

src/test/java/org/javimmutable/collections/setmap/JImmutableTreeSetMapTest.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,56 +35,43 @@
3535

3636
package org.javimmutable.collections.setmap;
3737

38-
import org.javimmutable.collections.JImmutableMap;
39-
import org.javimmutable.collections.JImmutableSet;
4038
import org.javimmutable.collections.JImmutableSetMap;
4139
import org.javimmutable.collections.MapEntry;
4240
import org.javimmutable.collections.cursors.StandardCursorTest;
4341

4442
import java.util.Arrays;
4543
import java.util.Comparator;
46-
import java.util.Set;
4744
import java.util.TreeMap;
4845

4946
public class JImmutableTreeSetMapTest
5047
extends AbstractJImmutableSetMapTestCase
5148
{
52-
@SuppressWarnings("unchecked")
5349
public void testNormalOrder()
5450
{
55-
JImmutableSetMap<Integer, Integer> map = verifyOperations(JImmutableTreeSetMap.<Integer, Integer>of());
56-
verifyRandom(JImmutableTreeSetMap.<Integer, Integer>of(), new TreeMap<Integer, Set<Integer>>());
51+
JImmutableSetMap<Integer, Integer> map = verifyOperations(JImmutableTreeSetMap.of());
52+
verifyRandom(JImmutableTreeSetMap.of(), new TreeMap<>());
5753
StandardCursorTest.listCursorTest(Arrays.asList(1, 2, 3), map.keysCursor());
58-
StandardCursorTest.listCursorTest(Arrays.<JImmutableMap.Entry<Integer, JImmutableSet<Integer>>>asList(MapEntry.of(1, map.getSet(1)),
59-
MapEntry.of(2, map.getSet(2)),
60-
MapEntry.of(3, map.getSet(3))),
54+
StandardCursorTest.listCursorTest(Arrays.asList(MapEntry.of(1, map.getSet(1)),
55+
MapEntry.of(2, map.getSet(2)),
56+
MapEntry.of(3, map.getSet(3))),
6157
map.cursor());
62-
StandardCursorTest.listIteratorTest(Arrays.<JImmutableMap.Entry<Integer, JImmutableSet<Integer>>>asList(MapEntry.of(1, map.getSet(1)),
63-
MapEntry.of(2, map.getSet(2)),
64-
MapEntry.of(3, map.getSet(3))),
58+
StandardCursorTest.listIteratorTest(Arrays.asList(MapEntry.of(1, map.getSet(1)),
59+
MapEntry.of(2, map.getSet(2)),
60+
MapEntry.of(3, map.getSet(3))),
6561
map.iterator());
6662
}
6763

68-
@SuppressWarnings("unchecked")
6964
public void testReverseOrder()
7065
{
71-
JImmutableSetMap<Integer, Integer> map = verifyOperations(JImmutableTreeSetMap.<Integer, Integer>of(new Comparator<Integer>()
72-
{
73-
@Override
74-
public int compare(Integer a,
75-
Integer b)
76-
{
77-
return b.compareTo(a);
78-
}
79-
}));
66+
JImmutableSetMap<Integer, Integer> map = verifyOperations(JImmutableTreeSetMap.of(Comparator.<Integer>reverseOrder()));
8067
StandardCursorTest.listCursorTest(Arrays.asList(3, 2, 1), map.keysCursor());
81-
StandardCursorTest.listCursorTest(Arrays.<JImmutableMap.Entry<Integer, JImmutableSet<Integer>>>asList(MapEntry.of(3, map.getSet(3)),
82-
MapEntry.of(2, map.getSet(2)),
83-
MapEntry.of(1, map.getSet(1))),
68+
StandardCursorTest.listCursorTest(Arrays.asList(MapEntry.of(3, map.getSet(3)),
69+
MapEntry.of(2, map.getSet(2)),
70+
MapEntry.of(1, map.getSet(1))),
8471
map.cursor());
85-
StandardCursorTest.listIteratorTest(Arrays.<JImmutableMap.Entry<Integer, JImmutableSet<Integer>>>asList(MapEntry.of(3, map.getSet(3)),
86-
MapEntry.of(2, map.getSet(2)),
87-
MapEntry.of(1, map.getSet(1))),
72+
StandardCursorTest.listIteratorTest(Arrays.asList(MapEntry.of(3, map.getSet(3)),
73+
MapEntry.of(2, map.getSet(2)),
74+
MapEntry.of(1, map.getSet(1))),
8875
map.iterator());
8976
}
9077

0 commit comments

Comments
 (0)