Skip to content

Commit

Permalink
Removed duplicate code from KeyMap
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Jun 3, 2022
1 parent 98848d7 commit 6d1b04a
Showing 1 changed file with 138 additions and 132 deletions.
270 changes: 138 additions & 132 deletions src/main/java/com/bgsoftware/superiorskyblock/key/dataset/KeyMapImpl.java
Expand Up @@ -28,7 +28,6 @@ public final class KeyMapImpl<V> extends AbstractMap<Key, V> implements KeyMap<V

private final Map<String, V> innerMap;

private Set<Entry<Key, V>> entrySet;
private Map<Key, V> innerReflectedMap;

public static <V> KeyMapImpl<V> create(Supplier<Map<String, V>> mapCreator) {
Expand Down Expand Up @@ -105,7 +104,7 @@ public void clear() {
@Override
@NotNull
public Set<Entry<Key, V>> entrySet() {
return entrySet == null ? (entrySet = new EntrySet()) : entrySet;
return asMap().entrySet();
}

@Override
Expand Down Expand Up @@ -150,6 +149,79 @@ public Map<Key, V> asMap() {
return Collections.unmodifiableMap(innerReflectedMap == null ? (innerReflectedMap = new InnerReflectedMap()) : innerReflectedMap);
}

private final class InnerReflectedMap implements Map<Key, V> {

private Set<Key> keySet;
private Set<Entry<Key, V>> entrySet;

@Override
public int size() {
return KeyMapImpl.this.innerMap.size();
}

@Override
public boolean isEmpty() {
return KeyMapImpl.this.innerMap.isEmpty();
}

@Override
public boolean containsKey(Object key) {
return KeyMapImpl.this.innerMap.containsKey(key);
}

@Override
public boolean containsValue(Object value) {
return KeyMapImpl.this.innerMap.containsValue(value);
}

@Override
public V get(Object key) {
return KeyMapImpl.this.innerMap.get(key);
}

@Nullable
@Override
public V put(Key key, V value) {
// No implementation
return null;
}

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

@Override
public void putAll(@NotNull Map<? extends Key, ? extends V> m) {
// No implementation
}

@Override
public void clear() {
// No implementation
}

@NotNull
@Override
public Set<Key> keySet() {
return keySet == null ? (keySet = new KeySet()) : keySet;
}

@NotNull
@Override
public Collection<V> values() {
return KeyMapImpl.this.innerMap.values();
}

@NotNull
@Override
public Set<Entry<Key, V>> entrySet() {
return entrySet == null ? (entrySet = new EntrySet()) : entrySet;
}

}

private final class EntrySet extends AbstractSet<Entry<Key, V>> {
public int size() {
return KeyMapImpl.this.size();
Expand Down Expand Up @@ -191,6 +263,36 @@ public void forEach(Consumer<? super Entry<Key, V>> action) {
}
}

private final class KeySet extends AbstractSet<Key> {
public int size() {
return KeyMapImpl.this.size();
}

public void clear() {
KeyMapImpl.this.clear();
}

public @NotNull Iterator<Key> iterator() {
return new KeyIterator();
}

public boolean contains(Object o) {
return KeyMapImpl.this.innerMap.containsKey(o);
}

public boolean remove(Object key) {
return KeyMapImpl.this.innerMap.remove(key) != null;
}

public Spliterator<Key> spliterator() {
return new KeySpliterator();
}

public void forEach(Consumer<? super Key> action) {
iterator().forEachRemaining(action);
}
}

private final class EntryIterator implements Iterator<Entry<Key, V>> {

final Iterator<Entry<String, V>> innerMapIterator = KeyMapImpl.this.innerMap.entrySet().iterator();
Expand All @@ -211,6 +313,26 @@ public void remove() {
}
}

private final class KeyIterator implements Iterator<Key> {

final Iterator<String> innerMapIterator = KeyMapImpl.this.innerMap.keySet().iterator();

@Override
public boolean hasNext() {
return innerMapIterator.hasNext();
}

@Override
public Key next() {
return Key.of(innerMapIterator.next());
}

@Override
public void remove() {
innerMapIterator.remove();
}
}

private final class EntrySpliterator implements Spliterator<Entry<Key, V>> {

final Spliterator<Entry<String, V>> spliterator;
Expand Down Expand Up @@ -244,153 +366,37 @@ public int characteristics() {
}
}

private final class InnerReflectedMap implements Map<Key, V> {
private final class KeySpliterator implements Spliterator<Key> {

private Set<Key> keySet;
private Set<Entry<Key, V>> entrySet;
final Spliterator<String> spliterator;

@Override
public int size() {
return KeyMapImpl.this.innerMap.size();
KeySpliterator() {
this(KeyMapImpl.this.innerMap.keySet().spliterator());
}

@Override
public boolean isEmpty() {
return KeyMapImpl.this.innerMap.isEmpty();
}

@Override
public boolean containsKey(Object key) {
return KeyMapImpl.this.innerMap.containsKey(key);
}

@Override
public boolean containsValue(Object value) {
return KeyMapImpl.this.innerMap.containsValue(value);
}

@Override
public V get(Object key) {
return KeyMapImpl.this.innerMap.get(key);
}

@Nullable
@Override
public V put(Key key, V value) {
// No implementation
return null;
}

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

@Override
public void putAll(@NotNull Map<? extends Key, ? extends V> m) {
// No implementation
KeySpliterator(Spliterator<String> spliterator) {
this.spliterator = spliterator;
}

@Override
public void clear() {
// No implementation
public boolean tryAdvance(Consumer<? super Key> action) {
return spliterator.tryAdvance(entry -> action.accept(Key.of(entry)));
}

@NotNull
@Override
public Set<Key> keySet() {
Set<Key> keySet = this.keySet;
if (keySet == null) {
keySet = this.keySet = new AbstractSet<Key>() {
public Iterator<Key> iterator() {
return new Iterator<Key>() {
private final Iterator<Entry<String, V>> iterator = KeyMapImpl.this.innerMap.entrySet().iterator();

public boolean hasNext() {
return iterator.hasNext();
}

public Key next() {
return Key.of(iterator.next().getKey());
}

public void remove() {
iterator.remove();
}
};
}

public int size() {
return InnerReflectedMap.this.size();
}

public boolean isEmpty() {
return InnerReflectedMap.this.isEmpty();
}

public void clear() {
InnerReflectedMap.this.clear();
}

public boolean contains(Object k) {
return InnerReflectedMap.this.containsKey(k);
}
};
}
return keySet;
public Spliterator<Key> trySplit() {
return new KeySpliterator(spliterator.trySplit());
}

@NotNull
@Override
public Collection<V> values() {
return KeyMapImpl.this.innerMap.values();
public long estimateSize() {
return spliterator.estimateSize();
}

@NotNull
@Override
public Set<Entry<Key, V>> entrySet() {
Set<Entry<Key, V>> entrySet = this.entrySet;
if (entrySet == null) {
entrySet = this.entrySet = new AbstractSet<Entry<Key, V>>() {
public Iterator<Entry<Key, V>> iterator() {
return new Iterator<Entry<Key, V>>() {
private final Iterator<Entry<String, V>> iterator = KeyMapImpl.this.innerMap.entrySet().iterator();

public boolean hasNext() {
return iterator.hasNext();
}

public Entry<Key, V> next() {
return new KeyEntry(iterator.next());
}

public void remove() {
iterator.remove();
}
};
}

public int size() {
return InnerReflectedMap.this.size();
}

public boolean isEmpty() {
return InnerReflectedMap.this.isEmpty();
}

public void clear() {
InnerReflectedMap.this.clear();
}

public boolean contains(Object k) {
return InnerReflectedMap.this.containsKey(k);
}
};
}
return entrySet;
public int characteristics() {
return spliterator.characteristics();
}

}

private final class KeyEntry implements Entry<Key, V> {
Expand Down

0 comments on commit 6d1b04a

Please sign in to comment.