From d682ccacebcb68aaff9575af022f25f8191155cd Mon Sep 17 00:00:00 2001 From: Zsombor Date: Mon, 16 Feb 2009 17:52:27 -0700 Subject: [PATCH] add synchronized map --- classpath/java/util/Collections.java | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/classpath/java/util/Collections.java b/classpath/java/util/Collections.java index 70639abd32..0d88bea008 100644 --- a/classpath/java/util/Collections.java +++ b/classpath/java/util/Collections.java @@ -106,6 +106,63 @@ public class Collections { return new SynchronizedIterator(lock, collection.iterator()); } } + + static class SynchronizedMap implements Map { + protected final Object lock; + protected final Map map; + + SynchronizedMap(Map map) { + this.map = map; + this.lock = this; + } + + SynchronizedMap(Object lock, Map 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> entrySet() { + synchronized (lock) { return new SynchronizedSet>(lock, map.entrySet()); } + } + public V get(K key) { + synchronized (lock) { return map.get(key); } + } + public boolean isEmpty() { + synchronized (lock) { return map.isEmpty(); } + } + public Set keySet() { + synchronized (lock) { return new SynchronizedSet(lock, map.keySet()); } + } + public V put(K key, V value) { + synchronized (lock) { return map.put(key, value); } + } + public void putAll(Map 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 values() { + synchronized (lock) { return new SynchronizedCollection(lock, map.values()); } + } + } + + public static Map synchronizedMap(Map map) { + return new SynchronizedMap (map); + } + static class SynchronizedSet extends SynchronizedCollection