1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.util; |
20 | |
|
21 | |
import java.util.AbstractMap; |
22 | |
import java.util.AbstractSet; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Collection; |
25 | |
import java.util.Enumeration; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
import java.util.Map; |
29 | |
import java.util.NoSuchElementException; |
30 | |
import java.util.Set; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public abstract class AbstractAttributeMap<V> extends AbstractMap<String, V> |
39 | |
{ |
40 | |
private Set<String> _keySet; |
41 | |
private Collection<V> _values; |
42 | |
private Set<Entry<String, V>> _entrySet; |
43 | |
|
44 | |
public void clear() |
45 | |
{ |
46 | 0 | final List<String> names = new ArrayList<String>(); |
47 | 0 | for (final Enumeration<String> e = getAttributeNames(); e.hasMoreElements();) |
48 | |
{ |
49 | 0 | names.add(e.nextElement()); |
50 | |
} |
51 | |
|
52 | 0 | for (String name : names) { |
53 | 0 | removeAttribute(name); |
54 | |
} |
55 | 0 | } |
56 | |
|
57 | |
public final boolean containsKey(final Object key) |
58 | |
{ |
59 | 0 | return getAttribute(key.toString()) != null; |
60 | |
} |
61 | |
|
62 | |
public boolean containsValue(final Object findValue) |
63 | |
{ |
64 | 0 | if (findValue == null) |
65 | |
{ |
66 | 0 | return false; |
67 | |
} |
68 | |
|
69 | 0 | for (final Enumeration<String> e = getAttributeNames(); e.hasMoreElements();) |
70 | |
{ |
71 | 0 | final Object value = getAttribute(e.nextElement()); |
72 | 0 | if (findValue.equals(value)) |
73 | |
{ |
74 | 0 | return true; |
75 | |
} |
76 | 0 | } |
77 | |
|
78 | 0 | return false; |
79 | |
} |
80 | |
|
81 | |
@Override |
82 | |
public Set<Entry<String, V>> entrySet() |
83 | |
{ |
84 | 0 | return (_entrySet != null) ? _entrySet : (_entrySet = new EntrySet()); |
85 | |
} |
86 | |
|
87 | |
public V get(final Object key) |
88 | |
{ |
89 | 0 | return getAttribute(key.toString()); |
90 | |
} |
91 | |
|
92 | |
public boolean isEmpty() |
93 | |
{ |
94 | 0 | return !getAttributeNames().hasMoreElements(); |
95 | |
} |
96 | |
|
97 | |
public Set<String> keySet() |
98 | |
{ |
99 | 0 | return (_keySet != null) ? _keySet : (_keySet = new KeySet()); |
100 | |
} |
101 | |
|
102 | |
public final V put(final String key, final V value) |
103 | |
{ |
104 | 0 | final V retval = getAttribute(key); |
105 | 0 | setAttribute(key, value); |
106 | 0 | return retval; |
107 | |
} |
108 | |
|
109 | |
public void putAll(final Map<? extends String, ? extends V> t) |
110 | |
{ |
111 | 0 | for (final Entry<? extends String, ? extends V> entry : t.entrySet()) { |
112 | 0 | setAttribute(entry.getKey(), entry.getValue()); |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
@Override |
117 | |
public final V remove(final Object key) |
118 | |
{ |
119 | 0 | final String key_ = key.toString(); |
120 | 0 | final V retval = getAttribute(key_); |
121 | 0 | removeAttribute(key_); |
122 | 0 | return retval; |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
public int size() |
127 | |
{ |
128 | 0 | int size = 0; |
129 | 0 | for (final Enumeration e = getAttributeNames(); e.hasMoreElements();) |
130 | |
{ |
131 | 0 | size++; |
132 | 0 | e.nextElement(); |
133 | |
} |
134 | 0 | return size; |
135 | |
} |
136 | |
|
137 | |
@Override |
138 | |
public Collection<V> values() |
139 | |
{ |
140 | 0 | return (_values != null) ? _values : (_values = new Values()); |
141 | |
} |
142 | |
|
143 | |
abstract protected V getAttribute(String key); |
144 | |
|
145 | |
abstract protected void setAttribute(String key, V value); |
146 | |
|
147 | |
abstract protected void removeAttribute(String key); |
148 | |
|
149 | |
abstract protected Enumeration<String> getAttributeNames(); |
150 | |
|
151 | 0 | private abstract class AbstractAttributeSet<E> extends AbstractSet<E> |
152 | |
{ |
153 | |
@Override |
154 | |
public boolean isEmpty() |
155 | |
{ |
156 | 0 | return AbstractAttributeMap.this.isEmpty(); |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public int size() |
161 | |
{ |
162 | 0 | return AbstractAttributeMap.this.size(); |
163 | |
} |
164 | |
|
165 | |
@Override |
166 | |
public void clear() |
167 | |
{ |
168 | 0 | AbstractAttributeMap.this.clear(); |
169 | 0 | } |
170 | |
} |
171 | |
|
172 | 0 | private final class KeySet extends AbstractAttributeSet<String> |
173 | |
{ |
174 | |
@Override |
175 | |
public Iterator<String> iterator() |
176 | |
{ |
177 | 0 | return new KeyIterator(); |
178 | |
} |
179 | |
|
180 | |
@Override |
181 | |
public boolean contains(final Object o) |
182 | |
{ |
183 | 0 | return AbstractAttributeMap.this.containsKey(o); |
184 | |
} |
185 | |
|
186 | |
@Override |
187 | |
public boolean remove(final Object o) |
188 | |
{ |
189 | 0 | return AbstractAttributeMap.this.remove(o) != null; |
190 | |
} |
191 | |
|
192 | |
} |
193 | |
|
194 | 0 | private abstract class AbstractAttributeIterator<E> implements Iterator<E> |
195 | |
{ |
196 | 0 | protected final Enumeration<String> _e = getAttributeNames(); |
197 | |
protected String _currentKey; |
198 | |
|
199 | |
public void remove() |
200 | |
{ |
201 | |
|
202 | |
|
203 | |
|
204 | 0 | if (_currentKey == null) |
205 | |
{ |
206 | 0 | throw new NoSuchElementException("You must call next() at least once"); |
207 | |
} |
208 | 0 | AbstractAttributeMap.this.remove(_currentKey); |
209 | 0 | } |
210 | |
|
211 | |
public boolean hasNext() |
212 | |
{ |
213 | 0 | return _e.hasMoreElements(); |
214 | |
} |
215 | |
|
216 | |
public E next() |
217 | |
{ |
218 | 0 | return getValue(_currentKey = _e.nextElement()); |
219 | |
} |
220 | |
|
221 | |
protected abstract E getValue(String attributeName); |
222 | |
} |
223 | |
|
224 | 0 | private final class KeyIterator extends AbstractAttributeIterator<String> |
225 | |
{ |
226 | |
@Override |
227 | |
protected String getValue(final String attributeName) |
228 | |
{ |
229 | 0 | return attributeName; |
230 | |
} |
231 | |
} |
232 | |
|
233 | 0 | private class Values extends AbstractAttributeSet<V> |
234 | |
{ |
235 | |
@Override |
236 | |
public Iterator<V> iterator() |
237 | |
{ |
238 | 0 | return new ValuesIterator(); |
239 | |
} |
240 | |
|
241 | |
@Override |
242 | |
public boolean contains(final Object o) |
243 | |
{ |
244 | 0 | if (o == null) |
245 | |
{ |
246 | 0 | return false; |
247 | |
} |
248 | |
|
249 | 0 | for (final Iterator it = iterator(); it.hasNext();) |
250 | |
{ |
251 | 0 | if (o.equals(it.next())) |
252 | |
{ |
253 | 0 | return true; |
254 | |
} |
255 | |
} |
256 | |
|
257 | 0 | return false; |
258 | |
} |
259 | |
|
260 | |
@Override |
261 | |
public boolean remove(final Object o) |
262 | |
{ |
263 | 0 | if (o == null) |
264 | |
{ |
265 | 0 | return false; |
266 | |
} |
267 | |
|
268 | 0 | for (final Iterator it = iterator(); it.hasNext();) |
269 | |
{ |
270 | 0 | if (o.equals(it.next())) |
271 | |
{ |
272 | 0 | it.remove(); |
273 | 0 | return true; |
274 | |
} |
275 | |
} |
276 | |
|
277 | 0 | return false; |
278 | |
} |
279 | |
} |
280 | |
|
281 | 0 | private class ValuesIterator extends AbstractAttributeIterator<V> |
282 | |
{ |
283 | |
@Override |
284 | |
protected V getValue(final String attributeName) |
285 | |
{ |
286 | 0 | return AbstractAttributeMap.this.get(attributeName); |
287 | |
} |
288 | |
} |
289 | |
|
290 | 0 | private final class EntrySet extends AbstractAttributeSet<Entry<String, V>> |
291 | |
{ |
292 | |
@Override |
293 | |
public Iterator<Entry<String, V>> iterator() |
294 | |
{ |
295 | 0 | return new EntryIterator(); |
296 | |
} |
297 | |
|
298 | |
@Override |
299 | |
public boolean contains(final Object o) |
300 | |
{ |
301 | 0 | if (!(o instanceof Entry)) |
302 | |
{ |
303 | 0 | return false; |
304 | |
} |
305 | |
|
306 | 0 | final Entry entry = (Entry) o; |
307 | 0 | final Object key = entry.getKey(); |
308 | 0 | final Object value = entry.getValue(); |
309 | 0 | if (key == null || value == null) |
310 | |
{ |
311 | 0 | return false; |
312 | |
} |
313 | |
|
314 | 0 | return value.equals(AbstractAttributeMap.this.get(key)); |
315 | |
} |
316 | |
|
317 | |
@Override |
318 | |
public boolean remove(final Object o) |
319 | |
{ |
320 | 0 | if (!(o instanceof Entry)) |
321 | |
{ |
322 | 0 | return false; |
323 | |
} |
324 | |
|
325 | 0 | final Entry entry = (Entry) o; |
326 | 0 | final Object key = entry.getKey(); |
327 | 0 | final Object value = entry.getValue(); |
328 | 0 | if (key == null || value == null || !value.equals(AbstractAttributeMap.this.get(key))) |
329 | |
{ |
330 | 0 | return false; |
331 | |
} |
332 | |
|
333 | 0 | return AbstractAttributeMap.this.remove(((Entry) o).getKey()) != null; |
334 | |
} |
335 | |
} |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | 0 | private final class EntryIterator extends AbstractAttributeIterator<Entry<String, V>> |
342 | |
{ |
343 | |
@Override |
344 | |
protected Entry<String, V> getValue(final String attributeName) |
345 | |
{ |
346 | |
|
347 | |
|
348 | 0 | return new EntrySetEntry(attributeName); |
349 | |
} |
350 | |
} |
351 | |
|
352 | 0 | private final class EntrySetEntry implements Entry<String, V> |
353 | |
{ |
354 | |
private final String _currentKey; |
355 | |
|
356 | |
public EntrySetEntry(final String currentKey) |
357 | 0 | { |
358 | 0 | _currentKey = currentKey; |
359 | 0 | } |
360 | |
|
361 | |
public String getKey() |
362 | |
{ |
363 | 0 | return _currentKey; |
364 | |
} |
365 | |
|
366 | |
public V getValue() |
367 | |
{ |
368 | 0 | return AbstractAttributeMap.this.get(_currentKey); |
369 | |
} |
370 | |
|
371 | |
public V setValue(final V value) |
372 | |
{ |
373 | 0 | return AbstractAttributeMap.this.put(_currentKey, value); |
374 | |
} |
375 | |
|
376 | |
@Override |
377 | |
public int hashCode() |
378 | |
{ |
379 | 0 | int result = 1; |
380 | 0 | result = 31 * result + ((_currentKey == null) ? 0 : _currentKey.hashCode()); |
381 | 0 | return result; |
382 | |
} |
383 | |
|
384 | |
@Override |
385 | |
public boolean equals(final Object obj) |
386 | |
{ |
387 | 0 | if (this == obj) |
388 | 0 | return true; |
389 | 0 | if (obj == null) |
390 | 0 | return false; |
391 | 0 | if (getClass() != obj.getClass()) |
392 | 0 | return false; |
393 | 0 | final EntrySetEntry other = (EntrySetEntry) obj; |
394 | 0 | if (_currentKey == null) |
395 | |
{ |
396 | 0 | if (other._currentKey != null) |
397 | 0 | return false; |
398 | |
} |
399 | 0 | else if (!_currentKey.equals(other._currentKey)) |
400 | 0 | return false; |
401 | 0 | return true; |
402 | |
} |
403 | |
|
404 | |
} |
405 | |
} |