KickJava   Java API By Example, From Geeks To Geeks.

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


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.logging.Logger;
12 import org.jboss.util.NestedRuntimeException;
13
14 import java.util.*;
15
16 /**
17  * List interceptor to perform list cache storage and retrieval
18  *
19  * @author <a HREF="mailto:harald@gliebe.de">Harald Gliebe</a>
20  * @author Ben Wang
21  */

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 JavaDoc clazz)
36    {
37       this.cache = cache;
38       this.fqn = fqn;
39       methodMap = CollectionInterceptorUtil.getMethodMap(clazz);
40    }
41
42    public String JavaDoc getName()
43    {
44       return "CachedListInterceptor";
45    }
46
47    public Object JavaDoc invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable JavaDoc
48    {
49       return CollectionInterceptorUtil.invoke(invocation,
50             this,
51             methodMap,
52             managedMethods);
53    }
54
55    // implementation of the java.util.List interface
56
protected Node getNode()
57    {
58       try {
59          return cache.get(fqn);
60       } catch (Exception JavaDoc e) {
61          throw new NestedRuntimeException(e);
62       }
63    }
64
65    public Object JavaDoc get(int index)
66    {
67       checkIndex(index);
68       try {
69          // return cache.getObject(((Fqn) fqn.clone()).add(new Integer(index)));
70
return cache.getObject(new Fqn(fqn, new Integer JavaDoc(index)));
71       } catch (Exception JavaDoc e) {
72          throw new NestedRuntimeException(e);
73       }
74    }
75
76    private void checkIndex(int i) {
77       if(size() == 0) return; // No need to check here.
78
if( i < 0 || i >= size() ) {
79          throw new IndexOutOfBoundsException JavaDoc("Index out of bound at CachedListInterceptor(). Index is " +i
80          + " but size is " +size());
81       }
82    }
83
84    private void checkArgument(Object JavaDoc o) {
85       if(o == null)
86          throw new NullPointerException JavaDoc("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       // TODO Can use optimization here
103
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 JavaDoc[] toArray()
114    {
115       Object JavaDoc[] objs = new Object JavaDoc[size()];
116       for(int i=0; i < size(); i++) {
117          objs[i] = get(i);
118       }
119       return objs;
120    }
121
122    public Object JavaDoc set(int index, Object JavaDoc element)
123    {
124       try {
125          if(index != 0)
126             checkIndex(index-1); // Since index can be size().
127
Object JavaDoc oldValue = get(index);
128          // return cache.putObject(((Fqn) fqn.clone()).add(new Integer(index)), element);
129
return cache.putObject(new Fqn(fqn, new Integer JavaDoc(index)), element);
130       } catch (Exception JavaDoc e) {
131          throw new NestedRuntimeException(e);
132       }
133    }
134
135    public Object JavaDoc[] toArray(Object JavaDoc a[])
136    {
137       throw new AopOperationNotSupportedException("CachedListInterceptor: List.toArray(a) operation not supported");
138    }
139
140    public void add(int index, Object JavaDoc element)
141    {
142       try {
143          if(index != 0)
144             checkIndex(index-1); // Since index can be size().
145
for (int i = size(); i > index; i--) {
146             // Object obj = cache.removeObject(((Fqn) fqn.clone()).add(new Integer(i - 1)));
147
Object JavaDoc obj = cache.removeObject(new Fqn(fqn, new Integer JavaDoc(i - 1)));
148             // cache.putObject(((Fqn) fqn.clone()).add(new Integer(i)), obj);
149
cache.putObject(new Fqn(fqn, new Integer JavaDoc(i)), obj);
150          }
151          // cache.putObject(((Fqn) fqn.clone()).add(new Integer(index)), element);
152
cache.putObject(new Fqn(fqn, new Integer JavaDoc(index)), element);
153       } catch (Exception JavaDoc e) {
154          throw new NestedRuntimeException(e);
155       }
156    }
157
158    public int indexOf(Object JavaDoc 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 JavaDoc 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 JavaDoc o)
178    {
179       add(size(), o);
180       return true;
181    }
182
183    public boolean contains(Object JavaDoc o)
184    {
185       if (indexOf(o) != -1) return true;
186       return false;
187    }
188
189    public Object JavaDoc remove(int index)
190    {
191       try {
192          checkIndex(index);
193          // Object result = cache.removeObject(((Fqn) fqn.clone()).add(new Integer(index)));
194
Object JavaDoc result = cache.removeObject(new Fqn(fqn, new Integer JavaDoc(index)));
195          int size = size();
196          for (int i = index; i < size; i++) {
197             // Object obj = cache.removeObject(((Fqn) fqn.clone()).add(new Integer(i + 1)));
198
Object JavaDoc obj = cache.removeObject(new Fqn(fqn, new Integer JavaDoc(i + 1)));
199             // cache.putObject(((Fqn) fqn.clone()).add(new Integer(i)), obj);
200
cache.putObject(new Fqn(fqn, new Integer JavaDoc(i)), obj);
201          }
202          return result;
203       } catch (Exception JavaDoc e) {
204          throw new NestedRuntimeException(e);
205       }
206    }
207
208    public boolean remove(Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc value = list.get(i);
268          if (value != null) {
269             if( !contains(value) )
270                return false;
271          } else { // Null key is not allowed
272
return false;
273          }
274       }
275       return true;
276    }
277
278    public String JavaDoc toString() {
279       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
280       int size = size();
281       for (int i =0; i < size; i++) {
282          Object JavaDoc 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       // TODO: check for concurrent modification
298
return new Iterator()
299       {
300          protected int current = 0;
301
302          public boolean hasNext()
303          {
304             return current < size();
305          }
306
307          public Object JavaDoc next()
308          {
309             try {
310                return cache.getObject(new Fqn(fqn, new Integer JavaDoc(current++)));
311             } catch (Exception JavaDoc e) {
312                throw new NestedRuntimeException(e);
313             }
314          }
315
316          public void remove()
317          {
318             // TODO Need optimization here since set does not care about index
319
try {
320                int size = size();
321                if (current < size) {
322                   // replace Object with last one
323
// Object last = cache.removeObject(((Fqn) fqn.clone()).add(new Integer(size - 1)));
324
Object JavaDoc last = cache.removeObject(new Fqn(fqn, new Integer JavaDoc(size - 1)));
325                   // cache.putObject(((Fqn) fqn.clone()).add(new Integer(--current)), last);
326
cache.putObject(new Fqn(fqn, new Integer JavaDoc(--current)), last);
327                } else {
328                   // cache.removeObject(((Fqn) fqn.clone()).add(new Integer(--current)));
329
cache.removeObject(new Fqn(fqn, new Integer JavaDoc(--current)));
330                }
331             } catch (Exception JavaDoc 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 JavaDoc next()
390       {
391          if( index == size() )
392             throw new NoSuchElementException();
393
394          index++;
395          return get(index-1);
396       }
397
398       public Object JavaDoc previous()
399       {
400          if( index == 0 )
401             throw new NoSuchElementException();
402
403          index--;
404          return get(index);
405       }
406
407       public void add(Object JavaDoc o)
408       {
409          throw new AopOperationNotSupportedException("CachedListInterceptor: ListIterator.add() optional operation not supported");
410       }
411
412       public void set(Object JavaDoc o)
413       {
414          throw new AopOperationNotSupportedException("CachedListInterceptor: ListIterator.set() optional operation not supported");
415       }
416    }
417 }
418
Popular Tags