1 22 package org.jboss.util.collection; 23 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 36 public class LazyMap implements Map 37 { 38 39 private Map delegate = Collections.EMPTY_MAP; 40 41 public void clear() 42 { 43 delegate = Collections.EMPTY_MAP; 44 } 45 46 public boolean containsKey(Object key) 47 { 48 return delegate.containsKey(key); 49 } 50 51 public boolean containsValue(Object value) 52 { 53 return delegate.containsValue(value); 54 } 55 56 public Set entrySet() 57 { 58 return delegate.entrySet(); 59 } 60 61 public Object get(Object key) 62 { 63 return delegate.get(key); 64 } 65 66 public boolean isEmpty() 67 { 68 return delegate.isEmpty(); 69 } 70 71 public Set keySet() 72 { 73 return delegate.keySet(); 74 } 75 76 public Object put(Object key, Object value) 77 { 78 if (delegate == Collections.EMPTY_MAP) 79 { 80 delegate = Collections.singletonMap(key, value); 81 return null; 82 } 83 else 84 { 85 if (delegate instanceof HashMap == false) 86 delegate = new HashMap (delegate); 87 return delegate.put(key, value); 88 } 89 } 90 91 public void putAll(Map t) 92 { 93 if (delegate instanceof HashMap == false) 94 delegate = new HashMap (delegate); 95 delegate.putAll(t); 96 } 97 98 public Object remove(Object key) 99 { 100 if (delegate instanceof HashMap == false) 101 delegate = new HashMap (delegate); 102 return delegate.remove(key); 103 } 104 105 public int size() 106 { 107 return delegate.size(); 108 } 109 110 public Collection values() 111 { 112 return delegate.values(); 113 } 114 } 115 | Popular Tags |