1 52 53 package freemarker.template; 54 55 import java.util.*; 56 import java.io.Serializable ; 57 58 73 public class SimpleHash extends WrappingTemplateModel 74 implements TemplateHashModelEx, Serializable { 75 76 private Map map; 77 private boolean putFailed; 78 79 83 public SimpleHash() { 84 this((ObjectWrapper)null); 85 } 86 87 96 public SimpleHash(Map map) { 97 this(map, null); 98 } 99 100 107 public SimpleHash(ObjectWrapper wrapper) { 108 super(wrapper); 109 map = new HashMap(); 110 } 111 112 125 public SimpleHash(Map map, ObjectWrapper wrapper) { 126 super(wrapper); 127 try { 128 this.map = copyMap(map); 129 } catch (ConcurrentModificationException cme) { 130 try { 136 Thread.sleep(5); 137 } catch (InterruptedException ie) { 138 } 139 synchronized (map) { 140 this.map = copyMap(map); 141 } 142 } 143 } 144 145 protected Map copyMap(Map map) { 146 if (map instanceof HashMap) { 147 return (Map) ((HashMap) map).clone(); 148 } 149 if (map instanceof SortedMap) { 150 if (map instanceof TreeMap) { 151 return (Map) ((TreeMap) map).clone(); 152 } 153 else { 154 return new TreeMap((SortedMap) map); 155 } 156 } 157 return new HashMap(map); 158 } 159 160 167 public void put(String key, Object obj) { 168 map.put(key, obj); 169 } 170 171 178 public void put(String key, boolean b) { 179 put(key, b ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE); 180 } 181 182 public TemplateModel get(String key) throws TemplateModelException { 183 Object result = map.get(key); 184 if (result instanceof TemplateModel) { 185 return (TemplateModel) result; 186 } 187 TemplateModel tm = wrap(result); 188 if (!putFailed) try { 189 map.put(key, tm); 190 } catch (Exception e) { 191 putFailed = true; 193 } 194 return tm; 195 } 196 197 198 203 public void remove(String key) { 204 map.remove(key); 205 } 206 207 211 212 public void putAll(Map m) { 213 for (Iterator it = m.entrySet().iterator(); it.hasNext();) { 214 Map.Entry entry = (Map.Entry) it.next(); 215 this.put((String ) entry.getKey(), entry.getValue()); 216 } 217 } 218 219 223 public String toString() { 224 return map.toString(); 225 } 226 227 public int size() { 228 return map.size(); 229 } 230 231 public boolean isEmpty() { 232 return map == null || map.isEmpty(); 233 } 234 235 public TemplateCollectionModel keys() { 236 return new SimpleCollection(map.keySet(), getObjectWrapper()); 237 } 238 239 public TemplateCollectionModel values() { 240 return new SimpleCollection(map.values(), getObjectWrapper()); 241 } 242 243 public SimpleHash synchronizedWrapper() { 244 return new SynchronizedHash(); 245 } 246 247 248 private class SynchronizedHash extends SimpleHash { 249 250 public synchronized boolean isEmpty() { 251 return SimpleHash.this.isEmpty(); 252 } 253 254 public synchronized void put(String key, Object obj) { 255 SimpleHash.this.put(key, obj); 256 } 257 258 public synchronized TemplateModel get(String key) throws TemplateModelException { 259 return SimpleHash.this.get(key); 260 } 261 262 public synchronized void remove(String key) { 263 SimpleHash.this.remove(key); 264 } 265 266 public synchronized int size() { 267 return SimpleHash.this.size(); 268 } 269 270 public synchronized TemplateCollectionModel keys() { 271 return SimpleHash.this.keys(); 272 } 273 274 public synchronized TemplateCollectionModel values() { 275 return SimpleHash.this.values(); 276 } 277 } 278 } 279 | Popular Tags |