1 19 package org.fjank.jcache.collection; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import java.util.Map.Entry; 26 import org.fjank.jcache.CacheGroup; 27 import org.fjank.jcache.CacheObject; 28 29 33 public class SetProxy implements Set { 34 35 private class SetProxyIterator implements Iterator { 36 private SetProxyIterator(Iterator iter) { 37 this.wrappedIter=iter; 38 } 39 private final Iterator wrappedIter; 40 private Object currObj; 41 public boolean hasNext() { 42 return wrappedIter.hasNext(); 43 } 44 public Object next() { 45 this.currObj= wrappedIter.next(); 46 return currObj; 47 } 48 public void remove() { 49 rmv(currObj); 50 } 51 52 } 53 private CacheGroup group; 54 55 private Set set; 56 57 61 public SetProxy(Set set, CacheGroup group) { 62 this.set=set; 63 this.group=group; 64 } 65 70 public boolean add(Object o) { 71 throw new UnsupportedOperationException (); 72 } 73 74 79 public boolean addAll(Collection c) { 80 throw new UnsupportedOperationException (); 81 } 82 83 88 public void clear() { 89 group.invalidate(); 90 } 91 92 97 public boolean contains(Object o) { 98 return set.contains(o); 99 } 100 101 102 public boolean containsAll(Collection c) { 103 return set.containsAll(c); 104 } 105 106 public boolean isEmpty() { 107 return set.isEmpty(); 108 } 109 110 111 public Iterator iterator() { 112 return new SetProxyIterator(set.iterator()); 113 } 114 private void rmv(Object o) { 115 remove(o); 116 } 117 118 public boolean remove(Object o) { 119 if(!set.contains(o)) return false; 120 if(o instanceof Map.Entry ) { 121 Map.Entry entry = (Entry) o; 122 CacheObject obj = (CacheObject) entry.getValue(); 123 obj.invalidate(); 124 return true; 125 } 126 CacheObject obj = (CacheObject) group.get(o); 127 if(obj==null) return false; 128 obj.invalidate(); 129 return true; 130 } 131 132 public boolean removeAll(Collection c) { 133 134 boolean changed = false; 135 for (Iterator iter = c.iterator(); iter.hasNext();) { 136 if(remove(iter.next())) { 137 changed=true; 138 } 139 } 140 return changed; 141 } 142 143 147 public boolean retainAll(Collection c) { 148 149 boolean changed = false; 150 for (Iterator iter = set.iterator(); iter.hasNext();) { 151 Object tmp = iter.next(); 152 if(!c.contains(tmp)) { 153 if(remove(tmp)) { 154 changed=true; 155 } 156 } 157 } 158 return changed; 159 } 160 161 164 public int size() { 165 return set.size(); 166 } 167 168 171 public Object [] toArray() { 172 return set.toArray(); 173 } 174 175 179 public Object [] toArray(Object [] a) { 180 return set.toArray(a); 181 } 182 183 186 public String toString() { 187 return set.toString(); 188 } 189 190 } 191 | Popular Tags |