Skip to content

Commit

Permalink
fix removeAll in ImmutableMap and ImmutableTreeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
tonivade committed Oct 13, 2024
1 parent ec1153d commit f12c744
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public ImmutableMap<K, V> remove(K key) {

@Override
public ImmutableMap<K, V> removeAll(Sequence<? extends K> keys) {
return new PImmutableMap<>(backend.minus(keys));
return new PImmutableMap<>(backend.minusAll(keys.toCollection()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public ImmutableTreeMap<K, V> remove(K key) {

@Override
public ImmutableTreeMap<K, V> removeAll(Sequence<? extends K> keys) {
return new PImmutableTreeMap<>(backend.minus(keys));
return new PImmutableTreeMap<>(backend.minusAll(keys.toCollection()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.github.tonivade.purefun.data;

import static com.github.tonivade.purefun.data.ImmutableMap.entry;
import static com.github.tonivade.purefun.data.Sequence.arrayOf;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -43,6 +44,7 @@ public void nonEmptyMap() {
() -> assertTrue(map.values().contains("bbb")),
() -> assertTrue(map.values().contains("ccc")),
() -> assertEquals(ImmutableMap.of(entry("c", "ccc")), map.remove("a").remove("b")),
() -> assertEquals(ImmutableMap.of(entry("c", "ccc")), map.removeAll(arrayOf("a", "b"))),
() -> assertEquals(ImmutableSet.of(entry("a", "aaa"),
entry("b", "bbb"),
entry("c", "ccc")), map.entries()),
Expand Down Expand Up @@ -71,7 +73,7 @@ public void empty() {
map.put("a", "aaa").mapValues(String::toUpperCase))
);
}

@Test
void serialization() throws IOException, ClassNotFoundException {
ImmutableMap<Integer, String> map = ImmutableMap.of(entry(1, "uno"), entry(2, "dos"), entry(3, "tres"));
Expand All @@ -81,14 +83,14 @@ void serialization() throws IOException, ClassNotFoundException {
objectOutputStream.writeObject(map);
objectOutputStream.writeObject(ImmutableMap.empty());
}

Object result = null;
Object empty = null;
try (var objectInputStream = new ObjectInputStream(new ByteArrayInputStream(output.toByteArray()))) {
result = objectInputStream.readObject();
empty = objectInputStream.readObject();
}

assertEquals(map, result);
assertSame(ImmutableMap.empty(), empty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.github.tonivade.purefun.data;

import static com.github.tonivade.purefun.data.ImmutableTreeMap.entry;
import static com.github.tonivade.purefun.data.Sequence.arrayOf;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -43,6 +44,7 @@ public void nonEmptyTree() {
() -> assertTrue(treeMap.values().contains("bbb")),
() -> assertTrue(treeMap.values().contains("ccc")),
() -> assertEquals(ImmutableTreeMap.of(entry("c", "ccc")), treeMap.remove("a").remove("b")),
() -> assertEquals(ImmutableTreeMap.of(entry("c", "ccc")), treeMap.removeAll(arrayOf("a", "b"))),
() -> assertEquals(ImmutableSet.of(entry("a", "aaa"),
entry("b", "bbb"),
entry("c", "ccc")), treeMap.entries()),
Expand Down Expand Up @@ -99,7 +101,7 @@ public void empty() {
() -> assertEquals(Option.none(), treeMap.ceilingKey("c"))
);
}

@Test
void serialization() throws IOException, ClassNotFoundException {
ImmutableTreeMap<Integer, String> treeMap = ImmutableTreeMap.of(entry(1, "uno"), entry(2, "dos"), entry(3, "tres"));
Expand All @@ -109,14 +111,14 @@ void serialization() throws IOException, ClassNotFoundException {
objectOutputStream.writeObject(treeMap);
objectOutputStream.writeObject(ImmutableTreeMap.empty());
}

Object result = null;
Object empty = null;
try (var objectInputStream = new ObjectInputStream(new ByteArrayInputStream(output.toByteArray()))) {
result = objectInputStream.readObject();
empty = objectInputStream.readObject();
}

assertEquals(treeMap, result);
assertSame(ImmutableTreeMap.empty(), empty);
}
Expand Down

0 comments on commit f12c744

Please sign in to comment.