mirror of
https://github.com/corda/corda.git
synced 2025-01-23 04:48:09 +00:00
add synchronized map
This commit is contained in:
parent
9ba62e25b9
commit
d682ccaceb
@ -106,6 +106,63 @@ public class Collections {
|
||||
return new SynchronizedIterator(lock, collection.iterator());
|
||||
}
|
||||
}
|
||||
|
||||
static class SynchronizedMap<K,V> implements Map<K,V> {
|
||||
protected final Object lock;
|
||||
protected final Map<K,V> map;
|
||||
|
||||
SynchronizedMap(Map<K,V> map) {
|
||||
this.map = map;
|
||||
this.lock = this;
|
||||
}
|
||||
|
||||
SynchronizedMap(Object lock, Map<K,V> map) {
|
||||
this.lock = lock;
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
synchronized (lock) { map.clear(); }
|
||||
}
|
||||
public boolean containsKey(K key) {
|
||||
synchronized (lock) { return map.containsKey(key); }
|
||||
}
|
||||
public boolean containsValue(V value) {
|
||||
synchronized (lock) { return map.containsValue(value); }
|
||||
}
|
||||
public Set<java.util.Map.Entry<K, V>> entrySet() {
|
||||
synchronized (lock) { return new SynchronizedSet<java.util.Map.Entry<K, V>>(lock, map.entrySet()); }
|
||||
}
|
||||
public V get(K key) {
|
||||
synchronized (lock) { return map.get(key); }
|
||||
}
|
||||
public boolean isEmpty() {
|
||||
synchronized (lock) { return map.isEmpty(); }
|
||||
}
|
||||
public Set<K> keySet() {
|
||||
synchronized (lock) { return new SynchronizedSet<K>(lock, map.keySet()); }
|
||||
}
|
||||
public V put(K key, V value) {
|
||||
synchronized (lock) { return map.put(key, value); }
|
||||
}
|
||||
public void putAll(Map<? extends K, ? extends V> elts) {
|
||||
synchronized (lock) { map.putAll(elts); }
|
||||
}
|
||||
public V remove(K key) {
|
||||
synchronized (lock) { return map.remove(key); }
|
||||
}
|
||||
public int size() {
|
||||
synchronized (lock) { return map.size(); }
|
||||
}
|
||||
public Collection<V> values() {
|
||||
synchronized (lock) { return new SynchronizedCollection<V>(lock, map.values()); }
|
||||
}
|
||||
}
|
||||
|
||||
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> map) {
|
||||
return new SynchronizedMap<K, V> (map);
|
||||
}
|
||||
|
||||
|
||||
static class SynchronizedSet<T>
|
||||
extends SynchronizedCollection<T>
|
||||
|
Loading…
Reference in New Issue
Block a user