1 19 package org.fjank.jcache.collection; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import javax.util.jcache.Attributes; 26 import javax.util.jcache.CacheException; 27 import javax.util.jcache.CacheFullException; 28 import javax.util.jcache.CacheMap; 29 import javax.util.jcache.NotARetrievableObjectException; 30 import org.fjank.jcache.CacheAccessImpl2; 31 import org.fjank.jcache.CacheGroup; 32 import org.fjank.jcache.CacheObject; 33 import org.fjank.jcache.CacheRegion; 34 35 39 public class MapAdapter implements CacheMap { 40 private final CacheAccessImpl2 acc; 41 public MapAdapter(CacheAccessImpl2 originalAccess) { 42 this.acc=originalAccess; 43 } 44 51 public int size() { 52 return acc.getRegion().getObjectCount(); 53 } 54 55 public boolean isEmpty() { 56 return acc.getRegion().getObjectCount()==0; 57 } 58 59 69 public boolean containsKey(Object key) { 70 if(key==null) throw new NullPointerException ("This Map does not permit null keys."); 71 return acc.isPresent(key); 72 } 73 74 public boolean containsValue(Object value) { 75 CacheRegion region = acc.getRegion(); 76 return region.containsValue(value); 77 } 78 79 96 public Object get(Object key) { 97 if(key==null) throw new NullPointerException ("This Map does not permit null keys."); 98 Object object = acc.get(key); 99 if(object==null) { 100 CacheException ex = acc.getException(true); 101 if(ex instanceof NotARetrievableObjectException) { 102 throw new ClassCastException (ex.getMessage()); 103 } 104 if(ex!=null) { 105 throw new IllegalStateException (ex.getMessage()); 106 } 107 return null; 109 } 110 return object; 111 } 112 113 118 public Object put(Object key, Object value) { 119 Object prev=null; 120 if(acc.isPresent(key)) { 121 prev = acc.get(key); 122 } 123 boolean success = acc.put(key, value); 124 if(!success) { 125 CacheException ex = acc.getException(true); 126 if(ex instanceof CacheFullException) { 127 throw new IllegalArgumentException (ex.getMessage()); 128 } 129 if(ex!=null) { 130 throw new IllegalStateException (ex.getMessage()); 131 } 132 throw new IllegalStateException ("Unknown error."); 133 } 134 return prev; 135 } 136 141 public Object put(Object key, String group, Object value) { 142 Object prev = null; 143 144 if(!acc.isPresent("group")) { 145 acc.defineGroup(group); 146 } 147 if(acc.isPresent(key)) { 148 prev=acc.get(key, group, null); 149 } 150 acc.put(key, group, value); 151 152 return prev; 153 } 154 161 public Object remove(Object key) { 162 if (key==null) { 163 throw new NullPointerException ("This Map does not permit null keys."); 164 } 165 CacheObject obj = (CacheObject) acc.getRegion().get(key); 166 if(obj==null) { 167 return null; 168 } 169 obj.invalidate(); 170 return obj.get(); 171 } 172 public Object remove(Object key, String group) { 173 if (key==null || group==null) { 174 throw new NullPointerException ("This Map does not permit null keys."); 175 } 176 CacheRegion region = acc.getRegion(); 177 CacheGroup group2 = region.getGroup(group); 178 if (group2 == null) { 179 return null; 180 } 181 CacheObject object = (CacheObject) group2.get(key); 182 if(object==null) { 183 return null; 184 } 185 object.invalidate(); 186 return object.get(); 187 } 188 194 public void putAll(Map t) { 195 for (Iterator iter = t.keySet().iterator(); iter.hasNext();) { 197 Object key = iter.next(); 198 acc.put(key, t.get(key)); 199 } 200 } 201 202 public void clear() { 203 acc.invalidate(); 204 } 205 206 public Set keySet() { 207 return acc.getRegion().keySet(); 208 } 209 210 public Collection values() { 211 return acc.getRegion().values(); 212 } 213 214 public Set entrySet() { 215 return acc.getRegion().entrySet(); 216 } 217 220 public Attributes getAttributes() { 221 return acc.getAttributes(); 222 } 223 226 public Attributes getAttributes(Object name) throws CacheException { 227 return acc.getAttributes(name); 228 } 229 232 public Object get(Object name, Object arguments) { 233 return acc.get(name, arguments); 234 } 235 238 public void defineObject(Object name, Attributes attributes) { 239 acc.defineObject(name, attributes); 240 } 241 } 242 | Popular Tags |