KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > aop > CachedList


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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 JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * CachedList.java. Currently not used.
18  *
19  * @author <a HREF="mailto:harald@gliebe.de">Harald Gliebe</a>
20  */

21
22 public class CachedList extends AbstractList JavaDoc
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 JavaDoc e) {
39          throw new NestedRuntimeException(e);
40       }
41    }
42
43    public Object JavaDoc get(int index)
44    {
45       try {
46          // return cache.getObject(((Fqn) fqn.clone()).add(new Integer(index)));
47
return cache.getObject(new Fqn(fqn, new Integer JavaDoc(index)));
48       } catch (Exception JavaDoc e) {
49          throw new NestedRuntimeException(e);
50       }
51    }
52
53    public int size()
54    {
55       Map JavaDoc children = getNode().getChildren();
56       return children == null ? 0 : children.size();
57    }
58
59    public Object JavaDoc set(int index, Object JavaDoc element)
60    {
61       try {
62          if (index < 0 || index >= size())
63             throw new IndexOutOfBoundsException JavaDoc();
64          Object JavaDoc oldValue = get(index);
65          //return cache.putObject(((Fqn) fqn.clone()).add(new Integer(index)), element);
66
return cache.putObject(new Fqn(fqn, new Integer JavaDoc(index)), element);
67       } catch (Exception JavaDoc e) {
68          throw new NestedRuntimeException(e);
69       }
70    }
71
72    public void add(int index, Object JavaDoc element)
73    {
74       try {
75          if (index < 0 || index > size())
76             throw new IndexOutOfBoundsException JavaDoc();
77          for (int i = size(); i > index; i--) {
78             // Object obj = cache.removeObject(((Fqn) fqn.clone()).add(new Integer(i - 1)));
79
Object JavaDoc obj = cache.removeObject(new Fqn(fqn, new Integer JavaDoc(i - 1)));
80             // cache.putObject(((Fqn) fqn.clone()).add(new Integer(i)), obj);
81
cache.putObject(new Fqn(fqn, new Integer JavaDoc(i)), obj);
82          }
83          // cache.putObject(((Fqn) fqn.clone()).add(new Integer(index)), element);
84
cache.putObject(new Fqn(fqn, new Integer JavaDoc(index)), element);
85       } catch (Exception JavaDoc e) {
86          throw new NestedRuntimeException(e);
87       }
88
89    }
90
91    public boolean remove(Object JavaDoc o)
92    {
93       return super.remove(o);
94    }
95
96    public Object JavaDoc remove(int index)
97    {
98       try {
99          if (index < 0 || index >= size())
100             throw new IndexOutOfBoundsException JavaDoc();
101          // Object result = cache.removeObject(((Fqn) fqn.clone()).add(new Integer(index)));
102
Object JavaDoc result = cache.removeObject(new Fqn(fqn, new Integer JavaDoc(index)));
103          int size = size() - 1;
104          for (int i = index; i < size; i++) {
105             // Object obj = cache.removeObject(((Fqn) fqn.clone()).add(new Integer(i + 1)));
106
Object JavaDoc obj = cache.removeObject(new Fqn(fqn, new Integer JavaDoc(i + 1)));
107             // cache.putObject(((Fqn) fqn.clone()).add(new Integer(i)), obj);
108
cache.putObject(new Fqn(fqn, new Integer JavaDoc(i)), obj);
109          }
110          return result;
111       } catch (Exception JavaDoc e) {
112          throw new NestedRuntimeException(e);
113       }
114
115    }
116
117 }
118
Popular Tags