001 package railo.commons.util.mod; 002 003 import java.io.IOException; 004 import java.io.ObjectOutputStream; 005 import java.io.Serializable; 006 import java.util.Collection; 007 import java.util.Map; 008 import java.util.Set; 009 010 import railo.runtime.exp.PageException; 011 012 013 public class SyncMap<K,V> 014 implements MapPro<K,V>, Serializable { 015 private static final long serialVersionUID = 1978198479659022715L; 016 017 private final MapPro<K,V> m; // Backing Map 018 final Serializable mutex; // Object on which to synchronize 019 020 public SyncMap() { 021 this(null); 022 } 023 024 public SyncMap(MapPro<K,V> m) { 025 if (m==null) this.m = new HashMapPro<K, V>(); 026 else this.m = m; 027 mutex = this; 028 } 029 030 SyncMap(MapPro<K,V> m, Serializable mutex) { 031 this.m = m; 032 this.mutex = mutex; 033 } 034 035 public MapPro<K, V> getMap(){ 036 return m; 037 } 038 039 public int size() { 040 synchronized (mutex) {return m.size();} 041 } 042 public boolean isEmpty() { 043 synchronized (mutex) {return m.isEmpty();} 044 } 045 public boolean containsKey(Object key) { 046 synchronized (mutex) {return m.containsKey(key);} 047 } 048 public boolean containsValue(Object value) { 049 synchronized (mutex) {return m.containsValue(value);} 050 } 051 public V get(Object key) { 052 synchronized (mutex) {return m.get(key);} 053 } 054 055 056 @Override 057 public V g(K key) throws PageException { 058 synchronized (mutex) {return m.g(key);} 059 } 060 061 @Override 062 public V g(K key, V defaultValue) { 063 synchronized (mutex) {return m.g(key,defaultValue);} 064 } 065 066 @Override 067 public V r(K key) throws PageException { 068 synchronized (mutex) {return m.r(key);} 069 } 070 071 @Override 072 public V r(K key, V defaultValue) { 073 synchronized (mutex) {return m.r(key,defaultValue);} 074 } 075 076 077 078 public V put(K key, V value) { 079 synchronized (mutex) {return m.put(key, value);} 080 } 081 public V remove(Object key) { 082 synchronized (mutex) {return m.remove(key);} 083 } 084 085 public void putAll(Map<? extends K, ? extends V> map) { 086 synchronized (mutex) {m.putAll(map);} 087 } 088 public void clear() { 089 synchronized (mutex) {m.clear();} 090 } 091 092 private transient Set<K> keySet = null; 093 private transient Set<MapPro.Entry<K,V>> entrySet = null; 094 private transient Collection<V> values = null; 095 096 public Set<K> keySet() { 097 synchronized (mutex) { 098 if (keySet==null) 099 keySet = new SyncSet<K>(m.keySet(), mutex); 100 return keySet; 101 } 102 } 103 104 public Set<Map.Entry<K,V>> entrySet() { 105 synchronized (mutex) { 106 if (entrySet==null) 107 entrySet = new SyncSet<Map.Entry<K,V>>(m.entrySet(), mutex); 108 return entrySet; 109 } 110 } 111 112 public Collection<V> values() { 113 synchronized (mutex) { 114 if (values==null) 115 values = new SyncCollection<V>(m.values(), mutex); 116 return values; 117 } 118 } 119 120 public boolean equals(Object o) { 121 if (this == o) 122 return true; 123 synchronized (mutex) {return m.equals(o);} 124 } 125 public int hashCode() { 126 synchronized (mutex) {return m.hashCode();} 127 } 128 public String toString() { 129 synchronized (mutex) {return m.toString();} 130 } 131 private void writeObject(ObjectOutputStream s) throws IOException { 132 synchronized (mutex) {s.defaultWriteObject();} 133 } 134 }