org.reactfx.util.MapHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of richtextfx Show documentation
Show all versions of richtextfx Show documentation
FX-Text-Area for formatted text and other special effects.
package org.reactfx.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
public abstract class MapHelper {
public static MapHelper put(MapHelper mapHelper, K key, V value) {
if(mapHelper == null) {
return new SingleEntryHelper<>(key, value);
} else {
return mapHelper.put(key, value);
}
}
public static V get(MapHelper mapHelper, K key) {
if(mapHelper == null) {
return null;
} else {
return mapHelper.get(key);
}
}
public static MapHelper remove(MapHelper mapHelper, K key) {
if(mapHelper == null) {
return mapHelper;
} else {
return mapHelper.remove(key);
}
}
public static K chooseKey(MapHelper mapHelper) {
if(mapHelper == null) {
throw new NoSuchElementException("empty map");
} else {
return mapHelper.chooseKey();
}
}
public static void replaceAll(
MapHelper mapHelper,
BiFunction super K, ? super V, ? extends V> f) {
if(mapHelper != null) {
mapHelper.replaceAll(f);
}
}
public static void forEach(MapHelper mapHelper, BiConsumer f) {
if(mapHelper != null) {
mapHelper.forEach(f);
}
}
public static boolean isEmpty(MapHelper mapHelper) {
return mapHelper == null;
}
public static int size(MapHelper mapHelper) {
if(mapHelper == null) {
return 0;
} else {
return mapHelper.size();
}
}
public static boolean containsKey(MapHelper mapHelper, K key) {
if(mapHelper == null) {
return false;
} else {
return mapHelper.containsKey(key);
}
}
private MapHelper() {
// private constructor to prevent subclassing
};
protected abstract MapHelper put(K key, V value);
protected abstract V get(K key);
protected abstract MapHelper remove(K key);
protected abstract K chooseKey();
protected abstract void replaceAll(BiFunction super K, ? super V, ? extends V> f);
protected abstract boolean containsKey(K key);
protected abstract void forEach(BiConsumer f);
protected abstract int size();
private static class SingleEntryHelper extends MapHelper {
private final K key;
private V value;
public SingleEntryHelper(K key, V value) {
this.key = key;
this.value = value;
}
@Override
protected MapHelper put(K key, V value) {
if(Objects.equals(key, this.key)) {
return new SingleEntryHelper<>(key, value);
} else {
return new MultiEntryHelper<>(this.key, this.value, key, value);
}
}
@Override
protected V get(K key) {
return Objects.equals(key, this.key) ? value : null;
}
@Override
protected MapHelper remove(K key) {
if(Objects.equals(key, this.key)) {
return null;
} else {
return this;
}
}
@Override
protected K chooseKey() {
return key;
}
@Override
protected void replaceAll(BiFunction super K, ? super V, ? extends V> f) {
value = f.apply(key, value);
}
@Override
protected void forEach(BiConsumer f) {
f.accept(key, value);
}
@Override
protected int size() {
return 1;
}
@Override
protected boolean containsKey(K key) {
return Objects.equals(key, this.key);
}
}
private static class MultiEntryHelper extends MapHelper {
private final Map entries = new HashMap<>();
public MultiEntryHelper(K k1, V v1, K k2, V v2) {
entries.put(k1, v1);
entries.put(k2, v2);
}
@Override
protected MapHelper put(K key, V value) {
entries.put(key, value);
return this;
}
@Override
protected V get(K key) {
return entries.get(key);
}
@Override
protected MapHelper remove(K key) {
entries.remove(key);
switch(entries.size()) {
case 0:
return null;
case 1:
@SuppressWarnings("unchecked")
Entry entry = (Entry) entries.entrySet().toArray(new Entry, ?>[1])[0];
return new SingleEntryHelper<>(entry.getKey(), entry.getValue());
default:
return this;
}
}
@Override
protected K chooseKey() {
return entries.keySet().iterator().next();
}
@Override
protected void replaceAll(BiFunction super K, ? super V, ? extends V> f) {
entries.replaceAll(f);
}
@Override
protected void forEach(BiConsumer f) {
for(Object entry: entries.entrySet().toArray()) {
@SuppressWarnings("unchecked")
Entry e = (Entry) entry;
f.accept(e.getKey(), e.getValue());
}
}
@Override
protected int size() {
return entries.size();
}
@Override
protected boolean containsKey(K key) {
return entries.containsKey(key);
}
}
}