Skip to content

Commit

Permalink
Fixed reflected maps/sets of keymap/keyset being unmodifiable
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Jun 3, 2022
1 parent de81abe commit 64408b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
Expand Up @@ -146,7 +146,7 @@ public V getOrDefault(Object key, V defaultValue) {

@Override
public Map<Key, V> asMap() {
return Collections.unmodifiableMap(innerReflectedMap == null ? (innerReflectedMap = new InnerReflectedMap()) : innerReflectedMap);
return innerReflectedMap == null ? (innerReflectedMap = new InnerReflectedMap()) : innerReflectedMap;
}

private final class InnerReflectedMap implements Map<Key, V> {
Expand Down Expand Up @@ -182,24 +182,22 @@ public V get(Object key) {
@Nullable
@Override
public V put(Key key, V value) {
// No implementation
return null;
return KeyMapImpl.this.innerMap.put(key.toString(), value);
}

@Override
public V remove(Object key) {
// No implementation
return null;
return KeyMapImpl.this.innerMap.remove(key);
}

@Override
public void putAll(@NotNull Map<? extends Key, ? extends V> m) {
// No implementation
m.forEach(this::put);
}

@Override
public void clear() {
// No implementation
KeyMapImpl.this.innerMap.clear();
}

@NotNull
Expand Down
Expand Up @@ -8,7 +8,6 @@
import javax.annotation.Nullable;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
Expand All @@ -17,7 +16,7 @@

public final class KeySetImpl extends AbstractSet<Key> implements KeySet {

private final Set<String> set;
private final Set<String> innerSet;

private Set<Key> innerReflectedSet;

Expand All @@ -39,11 +38,11 @@ public static KeySetImpl createHashSet(Collection<Key> keys) {

private KeySetImpl(Supplier<Set<String>> setCreator, Collection<String> keys) {
this(setCreator);
this.set.addAll(keys);
this.innerSet.addAll(keys);
}

private KeySetImpl(Supplier<Set<String>> setCreator) {
this.set = setCreator.get();
this.innerSet = setCreator.get();
}

@Override
Expand All @@ -54,22 +53,22 @@ public Iterator<Key> iterator() {

@Override
public int size() {
return set.size();
return innerSet.size();
}

@Override
public boolean contains(Object o) {
return o instanceof Key && (set.contains(o.toString()) || (!((Key) o).getSubKey().isEmpty() && set.contains(((Key) o).getGlobalKey())));
return o instanceof Key && (innerSet.contains(o.toString()) || (!((Key) o).getSubKey().isEmpty() && innerSet.contains(((Key) o).getGlobalKey())));
}

@Override
public boolean add(Key key) {
return set.add(key.toString());
return innerSet.add(key.toString());
}

@Override
public boolean remove(Object o) {
return o instanceof Key ? set.remove(o.toString()) : set.remove(o);
return o instanceof Key ? innerSet.remove(o.toString()) : innerSet.remove(o);
}

@Override
Expand All @@ -79,34 +78,34 @@ public Key getKey(Key original) {

@Override
public Key getKey(Key original, @Nullable Key def) {
if (set.contains(original.toString()))
if (innerSet.contains(original.toString()))
return original;
else if (set.contains(original.getGlobalKey()))
else if (innerSet.contains(original.getGlobalKey()))
return KeyImpl.of(original.getGlobalKey(), "");
else
return def;
}

@Override
public Set<Key> asSet() {
return Collections.unmodifiableSet(innerReflectedSet == null ? (innerReflectedSet = new InnerReflectedSet()) : innerReflectedSet);
return innerReflectedSet == null ? (innerReflectedSet = new InnerReflectedSet()) : innerReflectedSet;
}

private final class InnerReflectedSet implements Set<Key> {

@Override
public int size() {
return KeySetImpl.this.set.size();
return KeySetImpl.this.innerSet.size();
}

@Override
public boolean isEmpty() {
return KeySetImpl.this.set.isEmpty();
return KeySetImpl.this.innerSet.isEmpty();
}

@Override
public boolean contains(Object o) {
return KeySetImpl.this.set.contains(o);
return KeySetImpl.this.innerSet.contains(o);
}

@NotNull
Expand All @@ -117,58 +116,57 @@ public Iterator<Key> iterator() {

@Override
public Object @NotNull [] toArray() {
return KeySetImpl.this.set.toArray();
return KeySetImpl.this.innerSet.toArray();
}

@Override
public <T> T @NotNull [] toArray(T @NotNull [] a) {
return KeySetImpl.this.set.toArray(a);
return KeySetImpl.this.innerSet.toArray(a);
}

@Override
public boolean add(Key key) {
// No implementation
return false;
return KeySetImpl.this.innerSet.add(key.toString());
}

@Override
public boolean remove(Object o) {
// No implementation
return false;
return KeySetImpl.this.innerSet.remove(o);
}

@Override
public boolean containsAll(@NotNull Collection<?> c) {
return KeySetImpl.this.set.containsAll(c);
return KeySetImpl.this.innerSet.containsAll(c);
}

@Override
public boolean addAll(@NotNull Collection<? extends Key> c) {
// No implementation
return false;
boolean added = false;
for (Key key : c) {
added |= add(key);
}
return added;
}

@Override
public boolean retainAll(@NotNull Collection<?> c) {
// No implementation
return false;
return KeySetImpl.this.innerSet.retainAll(c);
}

@Override
public boolean removeAll(@NotNull Collection<?> c) {
// No implementation
return false;
return KeySetImpl.this.innerSet.removeAll(c);
}

@Override
public void clear() {
// No implementation
KeySetImpl.this.innerSet.clear();
}
}

private final class SetIterator implements Iterator<Key> {

final Iterator<String> innerSetIterator = KeySetImpl.this.set.iterator();
final Iterator<String> innerSetIterator = KeySetImpl.this.innerSet.iterator();

@Override
public boolean hasNext() {
Expand Down

0 comments on commit 64408b8

Please sign in to comment.