1 21 package net.sf.hajdbc.util; 22 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.ListIterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 36 public final class Collections 37 { 38 47 public static <K, V> Map <K, V> cast(Map map, Class <K> targetKeyClass, Class <V> targetValueClass) 48 { 49 return new TypedMap<K, V>(map, targetKeyClass, targetValueClass); 50 } 51 52 59 public static <T> Collection <T> cast(Collection collection, Class <T> targetClass) 60 { 61 return new TypedCollection<T>(collection, targetClass); 62 } 63 64 71 public static <T> Set <T> cast(Set set, Class <T> targetClass) 72 { 73 return new TypedSet<T>(set, targetClass); 74 } 75 76 83 public static <T> List <T> cast(List list, Class <T> targetClass) 84 { 85 return new TypedList<T>(list, targetClass); 86 } 87 88 private Collections() 89 { 90 } 92 93 private static class TypedIterator<E> implements Iterator <E> 94 { 95 private Iterator iterator; 96 protected Class <E> targetClass; 97 98 103 public TypedIterator(Iterator iterator, Class <E> targetClass) 104 { 105 this.iterator = iterator; 106 this.targetClass = targetClass; 107 } 108 109 112 public boolean hasNext() 113 { 114 return this.iterator.hasNext(); 115 } 116 117 120 public E next() 121 { 122 return this.targetClass.cast(this.iterator.next()); 123 } 124 125 128 public void remove() 129 { 130 this.iterator.remove(); 131 } 132 } 133 134 private static class TypedCollection<E> implements Collection <E> 135 { 136 private Collection collection; 137 protected Class <E> targetClass; 138 139 144 public TypedCollection(Collection collection, Class <E> targetClass) 145 { 146 this.collection = collection; 147 this.targetClass = targetClass; 148 } 149 150 155 public boolean add(E element) 156 { 157 return this.collection.add(element); 158 } 159 160 163 public boolean addAll(Collection <? extends E> c) 164 { 165 return this.collection.addAll(c); 166 } 167 168 171 public void clear() 172 { 173 this.collection.clear(); 174 } 175 176 179 public boolean contains(Object element) 180 { 181 return this.collection.contains(element); 182 } 183 184 187 public boolean containsAll(Collection <?> c) 188 { 189 return this.collection.containsAll(c); 190 } 191 192 195 public boolean isEmpty() 196 { 197 return this.collection.isEmpty(); 198 } 199 200 203 public Iterator <E> iterator() 204 { 205 return new TypedIterator(this.collection.iterator(), this.targetClass); 206 } 207 208 211 public boolean remove(Object element) 212 { 213 return this.collection.remove(element); 214 } 215 216 219 public boolean removeAll(Collection <?> c) 220 { 221 return this.collection.removeAll(c); 222 } 223 224 227 public boolean retainAll(Collection <?> c) 228 { 229 return this.collection.retainAll(c); 230 } 231 232 235 public int size() 236 { 237 return this.collection.size(); 238 } 239 240 243 public Object [] toArray() 244 { 245 return this.collection.toArray(); 246 } 247 248 254 public <T> T[] toArray(T[] array) 255 { 256 return (T[]) this.collection.toArray(array); 257 } 258 } 259 260 private static class TypedSet<E> extends TypedCollection<E> implements Set <E> 261 { 262 267 public TypedSet(Set set, Class <E> targetClass) 268 { 269 super(set, targetClass); 270 } 271 } 272 273 private static class TypedList<E> extends TypedCollection<E> implements List <E> 274 { 275 private List list; 276 277 282 public TypedList(List list, Class <E> targetClass) 283 { 284 super(list, targetClass); 285 286 this.list = list; 287 } 288 289 292 public boolean addAll(int index, Collection <? extends E> collection) 293 { 294 return this.list.addAll(index, collection); 295 } 296 297 300 public E get(int index) 301 { 302 return this.targetClass.cast(this.list.get(index)); 303 } 304 305 311 public E set(int index, E value) 312 { 313 return this.targetClass.cast(this.list.set(index, value)); 314 } 315 316 321 public void add(int index, E value) 322 { 323 this.list.add(index, value); 324 } 325 326 329 public E remove(int index) 330 { 331 return this.targetClass.cast(this.list.remove(index)); 332 } 333 334 337 public int indexOf(Object element) 338 { 339 return this.list.indexOf(element); 340 } 341 342 345 public int lastIndexOf(Object element) 346 { 347 return this.lastIndexOf(element); 348 } 349 350 353 public ListIterator <E> listIterator() 354 { 355 return new TypedListIterator(this.list.listIterator()); 356 } 357 358 361 public ListIterator <E> listIterator(int index) 362 { 363 return new TypedListIterator(this.list.listIterator(index)); 364 } 365 366 369 public List <E> subList(int startIndex, int endIndex) 370 { 371 return new TypedList(this.list.subList(startIndex, endIndex), this.targetClass); 372 } 373 374 private class TypedListIterator extends TypedIterator<E> implements ListIterator <E> 375 { 376 private ListIterator iterator; 377 378 382 public TypedListIterator(ListIterator iterator) 383 { 384 super(iterator, TypedList.this.targetClass); 385 386 this.iterator = iterator; 387 } 388 389 392 public boolean hasPrevious() 393 { 394 return this.iterator.hasPrevious(); 395 } 396 397 400 public E previous() 401 { 402 return this.targetClass.cast(this.iterator.previous()); 403 } 404 405 408 public int nextIndex() 409 { 410 return this.iterator.nextIndex(); 411 } 412 413 416 public int previousIndex() 417 { 418 return this.iterator.previousIndex(); 419 } 420 421 425 public void set(E element) 426 { 427 this.iterator.set(element); 428 } 429 430 434 public void add(E element) 435 { 436 this.iterator.add(element); 437 } 438 } 439 } 440 441 private static class TypedMap<K, V> implements Map <K, V> 442 { 443 private Map map; 444 private Class <K> targetKeyClass; 445 private Class <V> targetValueClass; 446 447 453 public TypedMap(Map map, Class <K> targetKeyClass, Class <V> targetValueClass) 454 { 455 this.map = map; 456 this.targetKeyClass = targetKeyClass; 457 this.targetValueClass = targetValueClass; 458 } 459 460 463 public int size() 464 { 465 return this.map.size(); 466 } 467 468 471 public boolean isEmpty() 472 { 473 return this.map.isEmpty(); 474 } 475 476 479 public boolean containsKey(Object key) 480 { 481 return this.map.containsKey(key); 482 } 483 484 487 public boolean containsValue(Object value) 488 { 489 return this.map.containsValue(value); 490 } 491 492 495 public V get(Object key) 496 { 497 return this.targetValueClass.cast(this.map.get(key)); 498 } 499 500 506 public V put(K key, V value) 507 { 508 return this.targetValueClass.cast(this.map.put(key, value)); 509 } 510 511 514 public V remove(Object key) 515 { 516 return this.targetValueClass.cast(this.map.remove(key)); 517 } 518 519 522 public void putAll(Map <? extends K, ? extends V> m) 523 { 524 this.map.putAll(m); 525 } 526 527 530 public void clear() 531 { 532 this.map.clear(); 533 } 534 535 538 public Set <K> keySet() 539 { 540 return Collections.cast(this.map.keySet(), this.targetKeyClass); 541 } 542 543 546 public Collection <V> values() 547 { 548 return Collections.cast(this.map.values(), this.targetValueClass); 549 } 550 551 554 public Set <Map.Entry <K, V>> entrySet() 555 { 556 return this.map.entrySet(); 557 } 558 } 559 } 560 | Popular Tags |