1 package org.apache.fulcrum.security.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Serializable;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.TreeMap;
29
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.fulcrum.security.entity.SecurityEntity;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public abstract class SecuritySet<T extends SecurityEntity> implements Serializable, Set<T>, Iterable<T> {
49
50 private static final long serialVersionUID = 2251987059226422569L;
51
52
53 protected Map<String, T> nameMap = null;
54
55
56 protected Map<Object, T> idMap = null;
57
58
59
60
61 public SecuritySet()
62 {
63 nameMap = new TreeMap<String, T>(String.CASE_INSENSITIVE_ORDER);
64 idMap = new TreeMap<Object, T>();
65 }
66
67
68
69
70
71
72
73 public Set<T> getSet()
74 {
75 return new HashSet<T>(idMap.values());
76 }
77
78
79
80
81
82
83 public Set<String> getNames()
84 {
85 return nameMap.keySet();
86 }
87
88
89
90
91
92
93 public Set<Object> getIds()
94 {
95 return idMap.keySet();
96 }
97
98
99
100
101 @Override
102 public void clear()
103 {
104 nameMap.clear();
105 idMap.clear();
106 }
107
108
109
110
111
112
113
114 public boolean containsName(String name)
115 {
116 return StringUtils.isNotEmpty(name) ? nameMap.containsKey(name) : false;
117 }
118
119
120
121
122
123
124
125 public boolean containsId(Object id)
126 {
127 return (id == null) ? false : idMap.containsKey(id);
128 }
129
130
131
132
133
134
135 @Override
136 public Iterator<T> iterator()
137 {
138 return idMap.values().iterator();
139 }
140
141
142
143
144
145
146 @Override
147 public int size()
148 {
149 return idMap.size();
150 }
151
152
153
154
155
156
157 @Override
158 public String toString()
159 {
160 StringBuilder sbuf = new StringBuilder(12 * size());
161
162 for (Iterator<T> it = iterator(); it.hasNext();) {
163 T se = it.next();
164 sbuf.append('[');
165 sbuf.append(se.getName());
166 sbuf.append(" -> ");
167 sbuf.append(se.getId());
168 sbuf.append(']');
169 if (it.hasNext()) {
170 sbuf.append(", ");
171 }
172 }
173
174 return sbuf.toString();
175 }
176
177
178
179
180
181 @Override
182 public boolean add(T o)
183 {
184 if (contains(o))
185 {
186 return false;
187 }
188
189 if (o.getId() != null)
190 {
191 idMap.put(o.getId(), o);
192 }
193
194 if (o.getName() != null)
195 {
196 nameMap.put(o.getName(), o);
197 }
198
199 return true;
200 }
201
202
203
204
205
206
207
208
209 public boolean add(Collection<? extends T> collection)
210 {
211 return addAll(collection);
212 }
213
214 @Override
215 public boolean addAll(Collection<? extends T> collection)
216 {
217 boolean res = false;
218
219 for (T o : collection)
220 res |= add(o);
221
222 return res;
223 }
224
225 @Override
226 public boolean isEmpty()
227 {
228 return idMap.isEmpty();
229 }
230
231 @Override
232 public boolean containsAll(Collection<?> collection)
233 {
234 for (Object object : collection)
235 {
236 if (!contains(object))
237 {
238 return false;
239 }
240 }
241 return true;
242 }
243
244 @Override
245 public boolean removeAll(Collection<?> collection)
246 {
247 boolean changed = false;
248 for (Object object : collection)
249 {
250 boolean result = remove(object);
251 if (result)
252 {
253 changed = true;
254 }
255 }
256
257 return changed;
258 }
259
260 @Override
261 public boolean retainAll(Collection<?> collection)
262 {
263 throw new RuntimeException("not implemented");
264 }
265
266
267
268
269
270
271 @Override
272 public Object[] toArray()
273 {
274 return getSet().toArray();
275 }
276
277
278
279
280
281
282
283 @Override
284 public boolean contains(Object o)
285 {
286 if (o == null || !(o instanceof SecurityEntity))
287 {
288 return false;
289 }
290 else
291 {
292 return containsId(((SecurityEntity) o).getId());
293 }
294 }
295
296
297
298
299
300
301
302 @Override
303 public boolean remove(Object o)
304 {
305 if (o instanceof SecurityEntity)
306 {
307 boolean res = contains(o);
308 idMap.remove(((SecurityEntity) o).getId());
309 nameMap.remove(((SecurityEntity) o).getName());
310 return res;
311 }
312
313 return false;
314 }
315
316
317
318
319 @Override
320 public <A> A[] toArray(A[] a)
321 {
322 return getSet().toArray(a);
323 }
324
325
326
327
328
329
330
331
332 public T getByName(String name)
333 {
334 return nameMap.get(name);
335 }
336
337
338
339
340
341
342
343 public T getById(Object id)
344 {
345 return idMap.get(id);
346 }
347 }