| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
package org.apache.tiles.request.collection; |
| 22 | |
|
| 23 | |
import static org.apache.tiles.request.collection.CollectionUtil.*; |
| 24 | |
|
| 25 | |
import java.util.ArrayList; |
| 26 | |
import java.util.Collection; |
| 27 | |
import java.util.Enumeration; |
| 28 | |
import java.util.HashSet; |
| 29 | |
import java.util.Iterator; |
| 30 | |
import java.util.List; |
| 31 | |
import java.util.Map; |
| 32 | |
import java.util.Set; |
| 33 | |
|
| 34 | |
import org.apache.tiles.request.attribute.HasKeys; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | 0 | public class ReadOnlyEnumerationMap<V> implements Map<String, V> { |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
protected HasKeys<V> request; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 64 | public ReadOnlyEnumerationMap(HasKeys<V> request) { |
| 55 | 64 | this.request = request; |
| 56 | 64 | } |
| 57 | |
|
| 58 | |
|
| 59 | |
public void clear() { |
| 60 | 1 | throw new UnsupportedOperationException(); |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public boolean containsKey(Object key) { |
| 66 | 2 | return (request.getValue(key(key)) != null); |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
@SuppressWarnings("unchecked") |
| 72 | |
public boolean containsValue(Object value) { |
| 73 | 4 | V realValue = (V) value; |
| 74 | 4 | for (Enumeration<String> keysIt = request.getKeys(); keysIt.hasMoreElements();) { |
| 75 | 8 | if (realValue.equals(request.getValue(keysIt.nextElement()))) { |
| 76 | 2 | return true; |
| 77 | |
} |
| 78 | |
} |
| 79 | 2 | return false; |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public Set<Map.Entry<String, V>> entrySet() { |
| 85 | 15 | return new ReadOnlyEnumerationMapEntrySet(); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
public V get(Object key) { |
| 91 | 5 | return (request.getValue(key(key))); |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public boolean isEmpty() { |
| 97 | 4 | return !request.getKeys().hasMoreElements(); |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
public Set<String> keySet() { |
| 103 | 1 | return new KeySet(request); |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public V put(String key, V value) { |
| 109 | 1 | throw new UnsupportedOperationException(); |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public void putAll(Map<? extends String, ? extends V> map) { |
| 115 | 1 | throw new UnsupportedOperationException(); |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public V remove(Object key) { |
| 121 | 1 | throw new UnsupportedOperationException(); |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public int size() { |
| 127 | 3 | return enumerationSize(request.getKeys()); |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public Collection<V> values() { |
| 133 | 16 | return new ReadOnlyEnumerationMapValuesCollection(); |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
@SuppressWarnings("unchecked") |
| 139 | |
@Override |
| 140 | |
public boolean equals(Object o) { |
| 141 | 2 | HasKeys<V> otherRequest = ((ReadOnlyEnumerationMap<V>) o).request; |
| 142 | 2 | boolean retValue = true; |
| 143 | 2 | Set<String> otherKeys = new HashSet<String>(); |
| 144 | 2 | for (Enumeration<String> attribs = otherRequest.getKeys(); attribs |
| 145 | 6 | .hasMoreElements();) { |
| 146 | 4 | otherKeys.add(attribs.nextElement()); |
| 147 | |
} |
| 148 | 2 | for (Enumeration<String> attribs = request.getKeys(); attribs |
| 149 | |
.hasMoreElements() |
| 150 | 6 | && retValue;) { |
| 151 | 4 | String parameterName = attribs.nextElement(); |
| 152 | 4 | retValue = request.getValue(parameterName).equals( |
| 153 | |
otherRequest.getValue(parameterName)); |
| 154 | 4 | otherKeys.remove(parameterName); |
| 155 | 4 | } |
| 156 | |
|
| 157 | 2 | return retValue && otherKeys.isEmpty(); |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
@Override |
| 162 | |
public int hashCode() { |
| 163 | 1 | int retValue = 0; |
| 164 | 1 | for (Enumeration<String> attribs = request.getKeys(); attribs |
| 165 | 3 | .hasMoreElements();) { |
| 166 | 2 | String parameterName = attribs.nextElement(); |
| 167 | 2 | V value = request.getValue(parameterName); |
| 168 | 2 | retValue += parameterName.hashCode() ^ (value == null ? 0 : value.hashCode()); |
| 169 | 2 | } |
| 170 | 1 | return retValue; |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | 26 | class ReadOnlyEnumerationMapEntrySet implements Set<Map.Entry<String, V>> { |
| 177 | |
|
| 178 | |
@Override |
| 179 | |
public boolean add(java.util.Map.Entry<String, V> e) { |
| 180 | 1 | throw new UnsupportedOperationException(); |
| 181 | |
} |
| 182 | |
|
| 183 | |
@Override |
| 184 | |
public boolean addAll( |
| 185 | |
Collection<? extends java.util.Map.Entry<String, V>> c) { |
| 186 | 1 | throw new UnsupportedOperationException(); |
| 187 | |
} |
| 188 | |
|
| 189 | |
@Override |
| 190 | |
public void clear() { |
| 191 | 1 | throw new UnsupportedOperationException(); |
| 192 | |
} |
| 193 | |
|
| 194 | |
@SuppressWarnings("unchecked") |
| 195 | |
@Override |
| 196 | |
public boolean contains(Object o) { |
| 197 | 1 | return containsEntry((java.util.Map.Entry<String, V>) o); |
| 198 | |
} |
| 199 | |
|
| 200 | |
@SuppressWarnings("unchecked") |
| 201 | |
@Override |
| 202 | |
public boolean containsAll(Collection<?> c) { |
| 203 | 2 | Collection<Map.Entry<String, V>> realCollection = |
| 204 | |
(Collection<Map.Entry<String, V>>) c; |
| 205 | 2 | for (Map.Entry<String, V> entry : realCollection) { |
| 206 | 3 | if (!containsEntry(entry)) { |
| 207 | 1 | return false; |
| 208 | |
} |
| 209 | 2 | } |
| 210 | 1 | return true; |
| 211 | |
} |
| 212 | |
|
| 213 | |
@Override |
| 214 | |
public boolean isEmpty() { |
| 215 | 1 | return ReadOnlyEnumerationMap.this.isEmpty(); |
| 216 | |
} |
| 217 | |
|
| 218 | |
@Override |
| 219 | |
public Iterator<java.util.Map.Entry<String, V>> iterator() { |
| 220 | 2 | return new ReadOnlyEnumerationMapEntrySetIterator(); |
| 221 | |
} |
| 222 | |
|
| 223 | |
@Override |
| 224 | |
public boolean remove(Object o) { |
| 225 | 1 | throw new UnsupportedOperationException(); |
| 226 | |
} |
| 227 | |
|
| 228 | |
@Override |
| 229 | |
public boolean removeAll(Collection<?> c) { |
| 230 | 1 | throw new UnsupportedOperationException(); |
| 231 | |
} |
| 232 | |
|
| 233 | |
@Override |
| 234 | |
public boolean retainAll(Collection<?> c) { |
| 235 | 1 | throw new UnsupportedOperationException(); |
| 236 | |
} |
| 237 | |
|
| 238 | |
@Override |
| 239 | |
public int size() { |
| 240 | 1 | return ReadOnlyEnumerationMap.this.size(); |
| 241 | |
} |
| 242 | |
|
| 243 | |
@Override |
| 244 | |
public Object[] toArray() { |
| 245 | 1 | return toList().toArray(); |
| 246 | |
} |
| 247 | |
|
| 248 | |
@Override |
| 249 | |
public <T> T[] toArray(T[] a) { |
| 250 | 1 | return toList().toArray(a); |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
protected boolean containsEntry(Map.Entry<String, V> entry) { |
| 260 | 4 | V storedValue = request.getValue(key(entry.getKey())); |
| 261 | 4 | return storedValue != null && storedValue.equals(entry.getValue()); |
| 262 | |
} |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
private List<Map.Entry<String, V>> toList() { |
| 270 | 2 | List<Map.Entry<String, V>> entries = new ArrayList<Map.Entry<String, V>>(); |
| 271 | 2 | Enumeration<String> names = request.getKeys(); |
| 272 | 6 | while (names.hasMoreElements()) { |
| 273 | 4 | entries.add(extractNextEntry(names)); |
| 274 | |
} |
| 275 | 2 | return entries; |
| 276 | |
} |
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
private MapEntry<String, V> extractNextEntry( |
| 285 | |
Enumeration<String> names) { |
| 286 | 5 | String name = names.nextElement(); |
| 287 | 5 | return new MapEntry<String, V>(name, request.getValue(name), |
| 288 | |
false); |
| 289 | |
} |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | 5 | private class ReadOnlyEnumerationMapEntrySetIterator implements Iterator<Map.Entry<String, V>> { |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | 2 | private Enumeration<String> namesEnumeration = request.getKeys(); |
| 300 | |
|
| 301 | |
@Override |
| 302 | |
public boolean hasNext() { |
| 303 | 1 | return namesEnumeration.hasMoreElements(); |
| 304 | |
} |
| 305 | |
|
| 306 | |
@Override |
| 307 | |
public java.util.Map.Entry<String, V> next() { |
| 308 | 1 | return extractNextEntry(namesEnumeration); |
| 309 | |
} |
| 310 | |
|
| 311 | |
@Override |
| 312 | |
public void remove() { |
| 313 | 1 | throw new UnsupportedOperationException(); |
| 314 | |
} |
| 315 | |
|
| 316 | |
} |
| 317 | |
} |
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | 32 | private class ReadOnlyEnumerationMapValuesCollection implements Collection<V> { |
| 323 | |
|
| 324 | |
@Override |
| 325 | |
public boolean add(V e) { |
| 326 | 1 | throw new UnsupportedOperationException(); |
| 327 | |
} |
| 328 | |
|
| 329 | |
@Override |
| 330 | |
public boolean addAll(Collection<? extends V> c) { |
| 331 | 1 | throw new UnsupportedOperationException(); |
| 332 | |
} |
| 333 | |
|
| 334 | |
@Override |
| 335 | |
public void clear() { |
| 336 | 1 | throw new UnsupportedOperationException(); |
| 337 | |
} |
| 338 | |
|
| 339 | |
@Override |
| 340 | |
public boolean contains(Object o) { |
| 341 | 2 | return containsValue(o); |
| 342 | |
} |
| 343 | |
|
| 344 | |
@SuppressWarnings("unchecked") |
| 345 | |
@Override |
| 346 | |
public boolean containsAll(Collection<?> c) { |
| 347 | 2 | Collection<String> realCollection = (Collection<String>) c; |
| 348 | 2 | List<String> valueList = new ArrayList<String>(realCollection); |
| 349 | 2 | for (Enumeration<String> keysEnum = request.getKeys(); keysEnum.hasMoreElements();) { |
| 350 | 4 | valueList.remove(request.getValue(keysEnum.nextElement())); |
| 351 | 4 | if (valueList.isEmpty()) { |
| 352 | 1 | return true; |
| 353 | |
} |
| 354 | |
} |
| 355 | 1 | return false; |
| 356 | |
} |
| 357 | |
|
| 358 | |
@Override |
| 359 | |
public boolean isEmpty() { |
| 360 | 1 | return ReadOnlyEnumerationMap.this.isEmpty(); |
| 361 | |
} |
| 362 | |
|
| 363 | |
@Override |
| 364 | |
public Iterator<V> iterator() { |
| 365 | 2 | return new ReadOnlyEnumerationMapValuesCollectionIterator(); |
| 366 | |
} |
| 367 | |
|
| 368 | |
@Override |
| 369 | |
public boolean remove(Object o) { |
| 370 | 1 | throw new UnsupportedOperationException(); |
| 371 | |
} |
| 372 | |
|
| 373 | |
@Override |
| 374 | |
public boolean removeAll(Collection<?> c) { |
| 375 | 1 | throw new UnsupportedOperationException(); |
| 376 | |
} |
| 377 | |
|
| 378 | |
@Override |
| 379 | |
public boolean retainAll(Collection<?> c) { |
| 380 | 1 | throw new UnsupportedOperationException(); |
| 381 | |
} |
| 382 | |
|
| 383 | |
@Override |
| 384 | |
public int size() { |
| 385 | 1 | return ReadOnlyEnumerationMap.this.size(); |
| 386 | |
} |
| 387 | |
|
| 388 | |
@Override |
| 389 | |
public Object[] toArray() { |
| 390 | 1 | return toList().toArray(); |
| 391 | |
} |
| 392 | |
|
| 393 | |
@Override |
| 394 | |
public <T> T[] toArray(T[] a) { |
| 395 | 1 | return toList().toArray(a); |
| 396 | |
} |
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
private List<V> toList() { |
| 404 | 2 | List<V> entries = new ArrayList<V>(); |
| 405 | 2 | Enumeration<String> names = request.getKeys(); |
| 406 | 6 | while (names.hasMoreElements()) { |
| 407 | 4 | entries.add(request.getValue(names.nextElement())); |
| 408 | |
} |
| 409 | 2 | return entries; |
| 410 | |
} |
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | 16 | private class ReadOnlyEnumerationMapValuesCollectionIterator implements Iterator<V> { |
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | 2 | private Enumeration<String> namesEnumeration = request.getKeys(); |
| 421 | |
|
| 422 | |
@Override |
| 423 | |
public boolean hasNext() { |
| 424 | 1 | return namesEnumeration.hasMoreElements(); |
| 425 | |
} |
| 426 | |
|
| 427 | |
@Override |
| 428 | |
public V next() { |
| 429 | 1 | return request.getValue(namesEnumeration.nextElement()); |
| 430 | |
} |
| 431 | |
|
| 432 | |
@Override |
| 433 | |
public void remove() { |
| 434 | 1 | throw new UnsupportedOperationException(); |
| 435 | |
} |
| 436 | |
} |
| 437 | |
} |
| 438 | |
} |