1 7 package org.jboss.cache.aop; 8 9 import org.jboss.cache.Fqn; 10 import org.jboss.cache.Node; 11 import org.jboss.logging.Logger; 12 import org.jboss.util.NestedRuntimeException; 13 14 import java.util.*; 15 16 22 23 public class CachedListInterceptor 24 implements List, CachedCollectionInterceptor 25 { 26 27 protected static final Logger log = Logger.getLogger(CachedListInterceptor.class); 28 protected static final Map managedMethods = 29 CollectionInterceptorUtil.getManagedMethods(List.class); 30 31 protected TreeCacheAop cache; 32 protected Fqn fqn; 33 protected Map methodMap; 34 35 public CachedListInterceptor(TreeCacheAop cache, Fqn fqn, Class clazz) 36 { 37 this.cache = cache; 38 this.fqn = fqn; 39 methodMap = CollectionInterceptorUtil.getMethodMap(clazz); 40 } 41 42 public String getName() 43 { 44 return "CachedListInterceptor"; 45 } 46 47 public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable 48 { 49 return CollectionInterceptorUtil.invoke(invocation, 50 this, 51 methodMap, 52 managedMethods); 53 } 54 55 protected Node getNode() 57 { 58 try { 59 return cache.get(fqn); 60 } catch (Exception e) { 61 throw new NestedRuntimeException(e); 62 } 63 } 64 65 public Object get(int index) 66 { 67 checkIndex(index); 68 try { 69 return cache.getObject(new Fqn(fqn, new Integer (index))); 71 } catch (Exception e) { 72 throw new NestedRuntimeException(e); 73 } 74 } 75 76 private void checkIndex(int i) { 77 if(size() == 0) return; if( i < 0 || i >= size() ) { 79 throw new IndexOutOfBoundsException ("Index out of bound at CachedListInterceptor(). Index is " +i 80 + " but size is " +size()); 81 } 82 } 83 84 private void checkArgument(Object o) { 85 if(o == null) 86 throw new NullPointerException ("Object is null"); 87 } 88 89 public int size() 90 { 91 Node node = getNode(); 92 if(node == null) { 93 return 0; 94 } 95 96 Map children = node.getChildren(); 97 return children == null ? 0 : children.size(); 98 } 99 100 public void clear() 101 { 102 for(int i=size()-1; i >= 0; i--) { 104 remove(i); 105 } 106 } 107 108 public boolean isEmpty() 109 { 110 return size() == 0 ? true : false; 111 } 112 113 public Object [] toArray() 114 { 115 Object [] objs = new Object [size()]; 116 for(int i=0; i < size(); i++) { 117 objs[i] = get(i); 118 } 119 return objs; 120 } 121 122 public Object set(int index, Object element) 123 { 124 try { 125 if(index != 0) 126 checkIndex(index-1); Object oldValue = get(index); 128 return cache.putObject(new Fqn(fqn, new Integer (index)), element); 130 } catch (Exception e) { 131 throw new NestedRuntimeException(e); 132 } 133 } 134 135 public Object [] toArray(Object a[]) 136 { 137 throw new AopOperationNotSupportedException("CachedListInterceptor: List.toArray(a) operation not supported"); 138 } 139 140 public void add(int index, Object element) 141 { 142 try { 143 if(index != 0) 144 checkIndex(index-1); for (int i = size(); i > index; i--) { 146 Object obj = cache.removeObject(new Fqn(fqn, new Integer (i - 1))); 148 cache.putObject(new Fqn(fqn, new Integer (i)), obj); 150 } 151 cache.putObject(new Fqn(fqn, new Integer (index)), element); 153 } catch (Exception e) { 154 throw new NestedRuntimeException(e); 155 } 156 } 157 158 public int indexOf(Object o) 159 { 160 checkArgument(o); 161 for(int i=0; i < size(); i++) { 162 if(o.equals(get(i))) return i; 163 } 164 return -1; 165 } 166 167 public int lastIndexOf(Object o) 168 { 169 checkArgument(o); 170 int index = -1; 171 for(int i=0; i < size(); i++) { 172 if(o.equals(get(i))) index = i; 173 } 174 return index; 175 } 176 177 public boolean add(Object o) 178 { 179 add(size(), o); 180 return true; 181 } 182 183 public boolean contains(Object o) 184 { 185 if (indexOf(o) != -1) return true; 186 return false; 187 } 188 189 public Object remove(int index) 190 { 191 try { 192 checkIndex(index); 193 Object result = cache.removeObject(new Fqn(fqn, new Integer (index))); 195 int size = size(); 196 for (int i = index; i < size; i++) { 197 Object obj = cache.removeObject(new Fqn(fqn, new Integer (i + 1))); 199 cache.putObject(new Fqn(fqn, new Integer (i)), obj); 201 } 202 return result; 203 } catch (Exception e) { 204 throw new NestedRuntimeException(e); 205 } 206 } 207 208 public boolean remove(Object o) 209 { 210 int i = indexOf(o); 211 if(i == -1) 212 return false; 213 214 remove(i); 215 return true; 216 } 217 218 public boolean addAll(int index, Collection c) 219 { 220 throw new AopOperationNotSupportedException("CachedListInterceptor: List.addAll() operation not supported"); 221 } 222 223 public boolean addAll(Collection c) 224 { 225 Iterator i = c.iterator(); 226 while(i.hasNext()) { 227 Object o = i.next(); 228 add(o); 229 } 230 return true; 231 } 232 233 public boolean containsAll(Collection c) 234 { 235 throw new AopOperationNotSupportedException("CachedListInterceptor: List.containsAll() operation not supported"); 236 } 237 238 public boolean removeAll(Collection c) 239 { 240 Iterator i = c.iterator(); 241 while(i.hasNext()) { 242 Object o = i.next(); 243 remove(o); 244 } 245 return true; 246 } 247 248 public int hashCode() 249 { 250 int result = 0; 251 for (int i =0; i < size(); i++) { 252 result += get(i).hashCode(); 253 } 254 return result; 255 } 256 257 public boolean equals(Object object) 258 { 259 if (object == this) 260 return true; 261 if (!(object instanceof List)) 262 return false; 263 List list = (List) object; 264 if (size() != list.size()) 265 return false; 266 for (int i=0; i < list.size(); i++) { 267 Object value = list.get(i); 268 if (value != null) { 269 if( !contains(value) ) 270 return false; 271 } else { return false; 273 } 274 } 275 return true; 276 } 277 278 public String toString() { 279 StringBuffer buf = new StringBuffer (); 280 int size = size(); 281 for (int i =0; i < size; i++) { 282 Object key = get(i); 283 buf.append("[" +key).append("]"); 284 if(i <= size) buf.append(", "); 285 } 286 287 return buf.toString(); 288 } 289 290 public boolean retainAll(Collection c) 291 { 292 throw new AopOperationNotSupportedException("CachedListInterceptor: List.retainAll() operation not supported"); 293 } 294 295 public Iterator iterator() 296 { 297 return new Iterator() 299 { 300 protected int current = 0; 301 302 public boolean hasNext() 303 { 304 return current < size(); 305 } 306 307 public Object next() 308 { 309 try { 310 return cache.getObject(new Fqn(fqn, new Integer (current++))); 311 } catch (Exception e) { 312 throw new NestedRuntimeException(e); 313 } 314 } 315 316 public void remove() 317 { 318 try { 320 int size = size(); 321 if (current < size) { 322 Object last = cache.removeObject(new Fqn(fqn, new Integer (size - 1))); 325 cache.putObject(new Fqn(fqn, new Integer (--current)), last); 327 } else { 328 cache.removeObject(new Fqn(fqn, new Integer (--current))); 330 } 331 } catch (Exception e) { 332 throw new NestedRuntimeException(e); 333 } 334 } 335 }; 336 } 337 338 public List subList(int fromIndex, int toIndex) 339 { 340 throw new AopOperationNotSupportedException("CachedListInterceptor: List.subList() operation not supported"); 341 } 342 343 public ListIterator listIterator() 344 { 345 return new MyListIterator(0); 346 } 347 348 public ListIterator listIterator(int index) 349 { 350 return new MyListIterator(index); 351 } 352 353 protected class MyListIterator implements ListIterator { 354 protected int index = 0; 355 356 public MyListIterator(int index) { 357 if(index < 0 || index >= size()) { 358 throw new AopOperationNotSupportedException("CachedListInterceptor: MyListIterator construction. " + 359 " Index is out of bound : " +index); 360 } 361 this.index = index; 362 } 363 364 public int nextIndex() 365 { 366 return index; 367 } 368 369 public int previousIndex() 370 { 371 return index-1; 372 } 373 374 public void remove() 375 { 376 throw new AopOperationNotSupportedException("CachedListInterceptor: ListIterator.remove() optional operation not supported"); 377 } 378 379 public boolean hasNext() 380 { 381 return (index == size()-1) ? false : true; 382 } 383 384 public boolean hasPrevious() 385 { 386 return (index == 0) ? false : true; 387 } 388 389 public Object next() 390 { 391 if( index == size() ) 392 throw new NoSuchElementException(); 393 394 index++; 395 return get(index-1); 396 } 397 398 public Object previous() 399 { 400 if( index == 0 ) 401 throw new NoSuchElementException(); 402 403 index--; 404 return get(index); 405 } 406 407 public void add(Object o) 408 { 409 throw new AopOperationNotSupportedException("CachedListInterceptor: ListIterator.add() optional operation not supported"); 410 } 411 412 public void set(Object o) 413 { 414 throw new AopOperationNotSupportedException("CachedListInterceptor: ListIterator.set() optional operation not supported"); 415 } 416 } 417 } 418 | Popular Tags |