Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing Entry subclasses to Map factory methods #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private static int mask(int hash, int shift){
@SuppressWarnings("WeakerAccess")
public static <K,V> @NotNull PersistentHashMap<K,V> ofEq(
Equator<K> eq,
@Nullable Iterable<Map.Entry<K,V>> es) {
@Nullable Iterable<? extends Map.Entry<K,V>> es) {
if (es == null) { return empty(eq); }
MutHashMap<K,V> map = emptyMutable(eq);
for (Map.Entry<K,V> entry : es) {
Expand All @@ -179,7 +179,7 @@ private static int mask(int hash, int shift){

@return a new PersistentHashMap of the given key/value pairs
*/
public static <K,V> @NotNull PersistentHashMap<K,V> of(@Nullable Iterable<Map.Entry<K,V>> kvPairs) {
public static <K,V> @NotNull PersistentHashMap<K,V> of(@Nullable Iterable<? extends Map.Entry<K,V>> kvPairs) {
if (kvPairs == null) { return empty(); }
PersistentHashMap<K,V> m = empty();
MutHashMap<K,V> map = m.mutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static class Box<E> {
any null Entries.
*/
public static <K extends Comparable<K>,V> PersistentTreeMap<K,V>
of(Iterable<Map.Entry<K,V>> es) {
of(Iterable<? extends Map.Entry<K,V>> es) {
if (es == null) { return empty(); }
PersistentTreeMap<K,V> map = new PersistentTreeMap<>(Equator.defaultComparator(), null, 0);
for (Map.Entry<K,V> entry : es) {
Expand All @@ -87,7 +87,7 @@ one null key (if your comparator knows how to sort nulls) and any number of null
@return a new PersistentTreeMap of the specified comparator and the given key/value pairs
*/
public static <K,V> PersistentTreeMap<K,V>
ofComp(Comparator<? super K> comp, Iterable<Map.Entry<K,V>> kvPairs) {
ofComp(Comparator<? super K> comp, Iterable<? extends Map.Entry<K,V>> kvPairs) {
if (kvPairs == null) { return new PersistentTreeMap<>(comp, null, 0); }
PersistentTreeMap<K,V> map = new PersistentTreeMap<>(comp, null, 0);
for (Map.Entry<K,V> entry : kvPairs) {
Expand All @@ -104,7 +104,7 @@ items that implement Comparable (have a "natural ordering"). An attempt to use
items will blow up at runtime. Either a withComparator() method will be added, or this will
be removed.
*/
final static public PersistentTreeMap EMPTY =
final static public PersistentTreeMap<?, ?> EMPTY =
new PersistentTreeMap<>(Equator.defaultComparator(), null, 0);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.organicdesign.fp.oneOf.Option;
import org.organicdesign.fp.tuple.Tuple2;

import static java.util.Map.entry;
import static org.junit.jupiter.api.Assertions.*;
import static org.organicdesign.fp.FunctionUtils.ordinal;
import static org.organicdesign.fp.StaticImports.*;
Expand Down Expand Up @@ -1618,4 +1617,26 @@ null, tup(3, "three"), null, tup(4, "four"), null)
assertNotEquals(h2, h2.assoc(null, "nada"));
assertEquals(h2.size() + 1, h2.assoc(null, "nada").size());
}

@Test
public void testOfIterable() {
PersistentHashMap<String, String> map = PersistentHashMap.<String, String>empty()
.assoc("a", "a")
.assoc("b", "b")
.assoc("c", "c")
.assoc("d", "d");
PersistentHashMap<String, String> map2 = PersistentHashMap.<String, String>empty()
.assoc("e", "e")
.assoc("f", "f")
.assoc("g", "g")
.assoc("h", "h");

assertEquals(map.without("a").without("b"), PersistentHashMap.of(
map.filter(entry -> !(entry.getKey().equals("a") || entry.getKey().equals("b")))));

ImMap<String, String> combined = map.concat(map2).toImMap(x -> x);
assertEquals(combined, PersistentTreeMap.of(map.concat(map2)));

assertEquals(map, PersistentTreeMap.of(map));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -925,4 +925,24 @@ public void subMapEx() {
Fn1.identity()),
max);
}

@Test public void testOfIterable() {
PersistentTreeMap<String, String> map = PersistentTreeMap.<String, String>empty()
.assoc("a", "a")
.assoc("b", "b")
.assoc("c", "c")
.assoc("d", "d");
PersistentTreeMap<String, String> map2 = PersistentTreeMap.<String, String>empty()
.assoc("e", "e")
.assoc("f", "f")
.assoc("g", "g")
.assoc("h", "h");

assertEquals(map.without("a").without("b"), PersistentTreeMap.of(map.drop(2)));

ImSortedMap<String, String> combined = map.concat(map2).toImSortedMap(Equator.defaultComparator(), entry -> entry);
assertEquals(combined, PersistentTreeMap.of(map.concat(map2)));

assertEquals(map, PersistentTreeMap.of(map));
}
}