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.Collection ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 25 public class CachedSetInterceptor implements Set , 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 methodMap; 33 34 protected static Map managedMethods = 35 CollectionInterceptorUtil.getManagedMethods(Set .class); 36 37 public CachedSetInterceptor(TreeCacheAop cache, Fqn fqn, Class clazz) 38 { 39 this.cache = cache; 40 this.fqn = fqn; 41 methodMap = CollectionInterceptorUtil.getMethodMap(clazz); 42 } 43 44 public String getName() 45 { 46 return "CachedSetInterceptor"; 47 } 48 49 public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable 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 e) { 62 throw new NestedRuntimeException(e); 63 } 64 } 65 66 public int size() 68 { 69 Node node = getNode(); 70 if(node == null) { 71 return 0; 72 } 73 74 Map children = node.getChildren(); 75 return children == null ? 0 : children.size(); 76 } 77 78 public void clear() 79 { 80 for(Iterator 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 [] toArray() 91 { 92 Object [] objs = new Object [size()]; 93 94 int ind = 0; 95 for(Iterator i = iterator(); i.hasNext(); ) { 96 objs[ind++] = i.next(); 97 } 98 return objs; 99 } 100 101 public Iterator iterator() 102 { 103 return new Iterator () 105 { 106 protected int current = 0; 107 108 public boolean hasNext() 109 { 110 return current < size(); 111 } 112 113 public Object next() 114 { 115 try { 116 return cache.getObject(new Fqn(fqn, new Integer (current++))); 117 } catch (Exception e) { 118 throw new NestedRuntimeException(e); 119 } 120 } 121 122 public void remove() 123 { 124 try { 126 int size = size(); 127 if (current < size) { 128 Object last = cache.removeObject(new Fqn(fqn, new Integer (size - 1))); 131 cache.putObject(new Fqn(fqn, new Integer (--current)), last); 133 } else { 134 cache.removeObject(new Fqn(fqn, new Integer (--current))); 136 } 137 } catch (Exception e) { 138 throw new NestedRuntimeException(e); 139 } 140 } 141 }; 142 } 143 144 public Object [] toArray(Object a[]) 145 { 146 throw new AopOperationNotSupportedException("CachedSetInterceptor: set.toArray() operation not supported"); 147 } 148 149 public boolean add(Object o) 150 { 151 if( contains(o) ) return false; 152 153 try { 154 cache.putObject(new Fqn(fqn, new Integer (size())), o); 156 return true; 157 } catch (Exception e) { 158 throw new NestedRuntimeException(e); 159 } 160 } 161 162 public boolean contains(Object o) 163 { 164 for (Iterator i = iterator(); i.hasNext();) { 165 Object n = i.next(); 166 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 o) 175 { 176 for (Iterator i = iterator(); i.hasNext();) { 177 Object 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 c) 187 { 188 Iterator it = c.iterator(); 189 while(it.hasNext()) { 190 add(it.next()); 191 } 192 193 return true; 194 } 195 196 public boolean containsAll(Collection c) 197 { 198 throw new AopOperationNotSupportedException("CachedSetInterceptor: set.containsAll() operation not supported"); 199 } 200 201 public boolean removeAll(Collection c) 202 { 203 Iterator it = c.iterator(); 204 while(it.hasNext()) { 205 remove(it.next()); 206 } 207 208 return true; 209 } 210 211 public boolean retainAll(Collection 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 i = iterator(); i.hasNext();) { 221 result += i.next().hashCode(); 222 } 223 return result; 224 } 225 226 public boolean equals(Object object) 227 { 228 if (object == this) 229 return true; 230 if (!(object instanceof Set )) 231 return false; 232 Set list = (Set ) object; 233 if (size() != list.size()) 234 return false; 235 for (Iterator i = iterator(); i.hasNext();) { 236 Object value = i.next(); 237 if (value != null) { 238 if( !contains(value) ) 239 return false; 240 } else { return false; 242 } 243 } 244 return true; 245 } 246 247 public String toString() { 248 StringBuffer buf = new StringBuffer (); 249 for(Iterator it = iterator(); it.hasNext();) { 250 Object 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 |