2019-10-26 23:15:49 +02:00
|
|
|
package fr.pandacube.util;
|
2016-10-16 19:22:44 +02:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
2016-12-25 23:34:12 +01:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.function.BiConsumer;
|
2016-10-16 19:22:44 +02:00
|
|
|
|
2016-12-25 23:34:12 +01:00
|
|
|
public class BiMap<K, V> implements Iterable<Entry<K, V>> {
|
2016-10-16 19:22:44 +02:00
|
|
|
|
|
|
|
HashMap<K, V> map = new HashMap<>();
|
|
|
|
HashMap<V, K> inversedMap = new HashMap<>();
|
|
|
|
|
|
|
|
public synchronized void put(K k, V v) {
|
|
|
|
if (map.containsKey(k) || inversedMap.containsKey(v)) {
|
|
|
|
map.remove(k);
|
|
|
|
inversedMap.remove(v);
|
|
|
|
}
|
|
|
|
map.put(k, v);
|
|
|
|
inversedMap.put(v, k);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized V get(K k) {
|
|
|
|
return map.get(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized K getKey(V v) {
|
|
|
|
return inversedMap.get(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized boolean containsKey(K k) {
|
|
|
|
return map.containsKey(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized boolean containsValue(V v) {
|
|
|
|
return inversedMap.containsKey(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized V remove(K k) {
|
|
|
|
V v = map.remove(k);
|
|
|
|
inversedMap.remove(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized K removeValue(V v) {
|
|
|
|
K k = inversedMap.remove(v);
|
|
|
|
map.remove(k);
|
|
|
|
return k;
|
|
|
|
}
|
2016-12-25 23:34:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<Entry<K, V>> iterator() {
|
|
|
|
return map.entrySet().iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void forEach(BiConsumer<K, V> c) {
|
|
|
|
for(Entry<K, V> entry : this) {
|
|
|
|
c.accept(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
2017-07-05 00:17:41 +02:00
|
|
|
|
|
|
|
public int size() {
|
|
|
|
return map.size();
|
|
|
|
}
|
2016-10-16 19:22:44 +02:00
|
|
|
|
|
|
|
}
|