add synchronized map

This commit is contained in:
Zsombor 2009-02-16 17:52:27 -07:00 committed by Joel Dice
parent 9ba62e25b9
commit d682ccaceb

View File

@ -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>