Skip to content

Commit

Permalink
Update ImmutableMaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jun 7, 2024
1 parent 7b55f1d commit 7fda633
Showing 1 changed file with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;

@StaticClass
final class ImmutableMaps {
Expand Down Expand Up @@ -54,6 +55,11 @@ public int size() {
return MapIterator.empty();
}

@Override
public @NotNull ImmutableMap<K, V> putted(K key, V value) {
return new Map1<>(key, value);
}

@Serial
private Object readResolve() {
return INSTANCE;
Expand Down Expand Up @@ -81,6 +87,29 @@ public int size() {
public @NotNull MapIterator<K, V> 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<K, V> 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<K, V> extends MapN<K, V> implements Serializable {
Expand Down Expand Up @@ -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<K, V> 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<K, V> extends MapN<K, V> implements Serializable {
Expand Down Expand Up @@ -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<K, V> 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);
}
}
}

0 comments on commit 7fda633

Please sign in to comment.