1 7 package org.jboss.cache.aop; 8 9 import org.jboss.cache.Fqn; 10 import org.jboss.cache.Node; 11 import org.jboss.util.NestedRuntimeException; 12 13 import java.util.AbstractList ; 14 import java.util.Map ; 15 16 21 22 public class CachedList extends AbstractList 23 { 24 25 protected TreeCacheAop cache; 26 protected Fqn fqn; 27 28 CachedList(TreeCacheAop cache, Fqn fqn) 29 { 30 this.cache = cache; 31 this.fqn = fqn; 32 } 33 34 protected Node getNode() 35 { 36 try { 37 return cache.get(fqn); 38 } catch (Exception e) { 39 throw new NestedRuntimeException(e); 40 } 41 } 42 43 public Object get(int index) 44 { 45 try { 46 return cache.getObject(new Fqn(fqn, new Integer (index))); 48 } catch (Exception e) { 49 throw new NestedRuntimeException(e); 50 } 51 } 52 53 public int size() 54 { 55 Map children = getNode().getChildren(); 56 return children == null ? 0 : children.size(); 57 } 58 59 public Object set(int index, Object element) 60 { 61 try { 62 if (index < 0 || index >= size()) 63 throw new IndexOutOfBoundsException (); 64 Object oldValue = get(index); 65 return cache.putObject(new Fqn(fqn, new Integer (index)), element); 67 } catch (Exception e) { 68 throw new NestedRuntimeException(e); 69 } 70 } 71 72 public void add(int index, Object element) 73 { 74 try { 75 if (index < 0 || index > size()) 76 throw new IndexOutOfBoundsException (); 77 for (int i = size(); i > index; i--) { 78 Object obj = cache.removeObject(new Fqn(fqn, new Integer (i - 1))); 80 cache.putObject(new Fqn(fqn, new Integer (i)), obj); 82 } 83 cache.putObject(new Fqn(fqn, new Integer (index)), element); 85 } catch (Exception e) { 86 throw new NestedRuntimeException(e); 87 } 88 89 } 90 91 public boolean remove(Object o) 92 { 93 return super.remove(o); 94 } 95 96 public Object remove(int index) 97 { 98 try { 99 if (index < 0 || index >= size()) 100 throw new IndexOutOfBoundsException (); 101 Object result = cache.removeObject(new Fqn(fqn, new Integer (index))); 103 int size = size() - 1; 104 for (int i = index; i < size; i++) { 105 Object obj = cache.removeObject(new Fqn(fqn, new Integer (i + 1))); 107 cache.putObject(new Fqn(fqn, new Integer (i)), obj); 109 } 110 return result; 111 } catch (Exception e) { 112 throw new NestedRuntimeException(e); 113 } 114 115 } 116 117 } 118 | Popular Tags |