diff --git a/kala-collection/src/main/java/kala/collection/immutable/ImmutableMaps.java b/kala-collection/src/main/java/kala/collection/immutable/ImmutableMaps.java index 4db544a2..c5473ddb 100644 --- a/kala-collection/src/main/java/kala/collection/immutable/ImmutableMaps.java +++ b/kala-collection/src/main/java/kala/collection/immutable/ImmutableMaps.java @@ -23,6 +23,7 @@ import java.io.Serial; import java.io.Serializable; +import java.util.Objects; @StaticClass final class ImmutableMaps { @@ -54,6 +55,11 @@ public int size() { return MapIterator.empty(); } + @Override + public @NotNull ImmutableMap putted(K key, V value) { + return new Map1<>(key, value); + } + @Serial private Object readResolve() { return INSTANCE; @@ -81,6 +87,29 @@ public int size() { public @NotNull MapIterator iterator() { return MapIterator.ofIterator(Iterators.of(Tuple.of(k0, v0))); } + + @Override + public boolean containsKey(K key) { + return Objects.equals(key, k0); + } + + @Override + public boolean containsValue(Object value) { + return Objects.equals(value, v0); + } + + @Override + public @NotNull ImmutableMap putted(K key, V value) { + if (Objects.equals(key, k0)) { + if (value == v0) { + return this; + } else { + return new Map1<>(key, value); + } + } + + return new Map2<>(k0, v0, key, value); + } } static final class Map2 extends MapN implements Serializable { @@ -111,6 +140,45 @@ public int size() { Tuple.of(k1, v1) )); } + + @Override + public boolean containsKey(K key) { + if (key != null) { + return key.equals(k0) || key.equals(k1); + } else { + return k0 == null || k1 == null; + } + } + + @Override + public boolean containsValue(Object value) { + if (value != null) { + return value.equals(v0) || value.equals(v1); + } else { + return v0 == null || v1 == null; + } + } + + @Override + public @NotNull ImmutableMap putted(K key, V value) { + if (Objects.equals(key, k0)) { + if (value == v0) { + return this; + } else { + return new Map2<>(key, value, k1, v1); + } + } + + if (Objects.equals(key, k1)) { + if (value == v1) { + return this; + } else { + return new Map2<>(k0, v0, key, value); + } + } + + return new Map3<>(k0, v0, k1, v1, key, value); + } } static final class Map3 extends MapN implements Serializable { @@ -146,5 +214,52 @@ public int size() { Tuple.of(k2, v2) )); } + + @Override + public boolean containsKey(K key) { + if (key != null) { + return key.equals(k0) || key.equals(k1) || key.equals(k2); + } else { + return k0 == null || k1 == null || k2 == null; + } + } + + @Override + public boolean containsValue(Object value) { + if (value != null) { + return value.equals(v0) || value.equals(v1) || value.equals(v2); + } else { + return v0 == null || v1 == null || v2 == null; + } + } + + @Override + public @NotNull ImmutableMap putted(K key, V value) { + if (Objects.equals(key, k0)) { + if (value == v0) { + return this; + } else { + return new Map3<>(key, value, k1, v1, k2, v2); + } + } + + if (Objects.equals(key, k1)) { + if (value == v1) { + return this; + } else { + return new Map3<>(k0, v0, key, value, k2, v2); + } + } + + if (Objects.equals(key, k2)) { + if (value == v2) { + return this; + } else { + return new Map3<>(k0, v0, k1, v1, key, value); + } + } + + return super.putted(key, value); + } } }