1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
30
31
32
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
58
59
60
61 public SetProxy(Set set, CacheGroup group) {
62 this.set=set;
63 this.group=group;
64 }
65
66
67
68
69
70 public boolean add(Object o) {
71 throw new UnsupportedOperationException();
72 }
73
74
75
76
77
78
79 public boolean addAll(Collection c) {
80 throw new UnsupportedOperationException();
81 }
82
83
84
85
86
87
88 public void clear() {
89 group.invalidate();
90 }
91
92
93
94
95
96
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
144
145
146
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
162
163
164 public int size() {
165 return set.size();
166 }
167
168
169
170
171 public Object[] toArray() {
172 return set.toArray();
173 }
174
175
176
177
178
179 public Object[] toArray(Object[] a) {
180 return set.toArray(a);
181 }
182
183
184
185
186 public String toString() {
187 return set.toString();
188 }
189
190 }