KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 /**
20  * Set interceptor to perform Set cache storage and retrieval
21  *
22  * @author <a HREF="mailto:harald@gliebe.de">Harald Gliebe</a>
23  * @author Ben Wang
24  */

25 public class CachedSetInterceptor implements Set JavaDoc, CachedCollectionInterceptor
26 {
27
28    protected static final Logger log = Logger.getLogger(CachedSetInterceptor.class);
29
30    protected TreeCacheAop cache;
31    protected Fqn fqn;
32    protected Map JavaDoc methodMap;
33
34    protected static Map JavaDoc managedMethods =
35          CollectionInterceptorUtil.getManagedMethods(Set JavaDoc.class);
36
37    public CachedSetInterceptor(TreeCacheAop cache, Fqn fqn, Class JavaDoc clazz)
38    {
39       this.cache = cache;
40       this.fqn = fqn;
41       methodMap = CollectionInterceptorUtil.getMethodMap(clazz);
42    }
43
44    public String JavaDoc getName()
45    {
46       return "CachedSetInterceptor";
47    }
48
49    public Object JavaDoc invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable JavaDoc
50    {
51       return CollectionInterceptorUtil.invoke(invocation,
52             this,
53             methodMap,
54             managedMethods);
55    }
56
57    protected Node getNode()
58    {
59       try {
60          return cache.get(fqn);
61       } catch (Exception JavaDoc e) {
62          throw new NestedRuntimeException(e);
63       }
64    }
65
66    // implementation of the java.util.Set interface
67
public int size()
68    {
69       Node node = getNode();
70       if(node == null) {
71          return 0;
72       }
73
74       Map JavaDoc children = node.getChildren();
75       return children == null ? 0 : children.size();
76    }
77
78    public void clear()
79    {
80       for(Iterator JavaDoc i = iterator(); i.hasNext(); ) {
81          remove(i.next());
82       }
83    }
84
85    public boolean isEmpty()
86    {
87       return size() == 0 ? true: false;
88    }
89
90    public Object JavaDoc[] toArray()
91    {
92       Object JavaDoc[] objs = new Object JavaDoc[size()];
93
94       int ind = 0;
95       for(Iterator JavaDoc i = iterator(); i.hasNext(); ) {
96          objs[ind++] = i.next();
97       }
98       return objs;
99    }
100
101    public Iterator JavaDoc iterator()
102    {
103       // TODO: check for concurrent modification
104
return new Iterator JavaDoc()
105       {
106          protected int current = 0;
107
108          public boolean hasNext()
109          {
110             return current < size();
111          }
112
113          public Object JavaDoc next()
114          {
115             try {
116                return cache.getObject(new Fqn(fqn, new Integer JavaDoc(current++)));
117             } catch (Exception JavaDoc e) {
118                throw new NestedRuntimeException(e);
119             }
120          }
121
122          public void remove()
123          {
124             // TODO Need optimization here since set does not care about index
125
try {
126                int size = size();
127                if (current < size) {
128                   // replace Object with last one
129
// Object last = cache.removeObject(((Fqn) fqn.clone()).add(new Integer(size - 1)));
130
Object JavaDoc last = cache.removeObject(new Fqn(fqn, new Integer JavaDoc(size - 1)));
131                   // cache.putObject(((Fqn) fqn.clone()).add(new Integer(--current)), last);
132
cache.putObject(new Fqn(fqn, new Integer JavaDoc(--current)), last);
133                } else {
134                   // cache.removeObject(((Fqn) fqn.clone()).add(new Integer(--current)));
135
cache.removeObject(new Fqn(fqn, new Integer JavaDoc(--current)));
136                }
137             } catch (Exception JavaDoc e) {
138                throw new NestedRuntimeException(e);
139             }
140          }
141       };
142    }
143
144    public Object JavaDoc[] toArray(Object JavaDoc a[])
145    {
146       throw new AopOperationNotSupportedException("CachedSetInterceptor: set.toArray() operation not supported");
147    }
148
149    public boolean add(Object JavaDoc o)
150    {
151       if( contains(o) ) return false;
152
153       try {
154          // cache.putObject(((Fqn) fqn.clone()).add(new Integer(size())), o);
155
cache.putObject(new Fqn(fqn, new Integer JavaDoc(size())), o);
156          return true;
157       } catch (Exception JavaDoc e) {
158          throw new NestedRuntimeException(e);
159       }
160    }
161
162    public boolean contains(Object JavaDoc o)
163    {
164       for (Iterator JavaDoc i = iterator(); i.hasNext();) {
165          Object JavaDoc n = i.next();
166          // TODO logic correct????
167
if ((o == null && n == null) || (o != null && o.equals(n))) {
168             return true;
169          }
170       }
171       return false;
172    }
173
174    public boolean remove(Object JavaDoc o)
175    {
176       for (Iterator JavaDoc i = iterator(); i.hasNext();) {
177          Object JavaDoc n = i.next();
178          if ((o == null && n == null) || (o != null && o.equals(n))) {
179             i.remove();
180             return true;
181          }
182       }
183       return false;
184    }
185
186    public boolean addAll(Collection JavaDoc c)
187    {
188       Iterator JavaDoc it = c.iterator();
189       while(it.hasNext()) {
190          add(it.next());
191       }
192
193       return true;
194    }
195
196    public boolean containsAll(Collection JavaDoc c)
197    {
198       throw new AopOperationNotSupportedException("CachedSetInterceptor: set.containsAll() operation not supported");
199    }
200
201    public boolean removeAll(Collection JavaDoc c)
202    {
203       Iterator JavaDoc it = c.iterator();
204       while(it.hasNext()) {
205          remove(it.next());
206       }
207
208       return true;
209    }
210
211    public boolean retainAll(Collection JavaDoc c)
212    {
213       throw new AopOperationNotSupportedException("CachedSetInterceptor: set.retainAll() operation not supported");
214    }
215
216
217    public int hashCode()
218    {
219       int result = 0;
220       for (Iterator JavaDoc i = iterator(); i.hasNext();) {
221          result += i.next().hashCode();
222       }
223       return result;
224    }
225
226    public boolean equals(Object JavaDoc object)
227    {
228       if (object == this)
229          return true;
230       if (!(object instanceof Set JavaDoc))
231          return false;
232       Set JavaDoc list = (Set JavaDoc) object;
233       if (size() != list.size())
234          return false;
235       for (Iterator JavaDoc i = iterator(); i.hasNext();) {
236          Object JavaDoc value = i.next();
237          if (value != null) {
238             if( !contains(value) )
239                return false;
240          } else { // Null key is not allowed
241
return false;
242          }
243       }
244       return true;
245    }
246
247    public String JavaDoc toString() {
248       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
249       for(Iterator JavaDoc it = iterator(); it.hasNext();) {
250          Object JavaDoc key = it.next();
251          buf.append("[" +key).append("]");
252          if(it.hasNext()) buf.append(", ");
253       }
254
255       return buf.toString();
256    }
257
258 }
259
Popular Tags