KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > util > Collections


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.util;
22
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ListIterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 /**
31  * Utility class for casting untyped collections into typed collections.
32  *
33  * @author Paul Ferraro
34  * @since 1.1
35  */

36 public final class Collections
37 {
38     /**
39      * Casts the specified map into a typed map using the specified type.
40      * @param <K> target type for map keys
41      * @param <V> target type for map values
42      * @param map
43      * @param targetKeyClass
44      * @param targetValueClass
45      * @return a Map parameterized by the specified key anv value types
46      */

47     public static <K, V> Map JavaDoc<K, V> cast(Map JavaDoc map, Class JavaDoc<K> targetKeyClass, Class JavaDoc<V> targetValueClass)
48     {
49         return new TypedMap<K, V>(map, targetKeyClass, targetValueClass);
50     }
51     
52     /**
53      * Casts the specified collection into a typed collection using the specified type.
54      * @param <T> target type for collection elements
55      * @param collection
56      * @param targetClass
57      * @return a Collection parameterized by the specified type
58      */

59     public static <T> Collection JavaDoc<T> cast(Collection JavaDoc collection, Class JavaDoc<T> targetClass)
60     {
61         return new TypedCollection<T>(collection, targetClass);
62     }
63     
64     /**
65      * Casts the specified set into a typed set using the specified type.
66      * @param <T> target type for set elements
67      * @param set
68      * @param targetClass
69      * @return a Set parameterized by the specified type
70      */

71     public static <T> Set JavaDoc<T> cast(Set JavaDoc set, Class JavaDoc<T> targetClass)
72     {
73         return new TypedSet<T>(set, targetClass);
74     }
75     
76     /**
77      * Casts the specified list into a typed list using the specified type.
78      * @param <T> target type for list elements
79      * @param list
80      * @param targetClass
81      * @return a List parameterized by the specified type
82      */

83     public static <T> List JavaDoc<T> cast(List JavaDoc list, Class JavaDoc<T> targetClass)
84     {
85         return new TypedList<T>(list, targetClass);
86     }
87     
88     private Collections()
89     {
90         // Hidden
91
}
92     
93     private static class TypedIterator<E> implements Iterator JavaDoc<E>
94     {
95         private Iterator JavaDoc iterator;
96         protected Class JavaDoc<E> targetClass;
97         
98         /**
99          * Constructs a new TypedIterator.
100          * @param iterator
101          * @param targetClass
102          */

103         public TypedIterator(Iterator JavaDoc iterator, Class JavaDoc<E> targetClass)
104         {
105             this.iterator = iterator;
106             this.targetClass = targetClass;
107         }
108
109         /**
110          * @see java.util.Iterator#hasNext()
111          */

112         public boolean hasNext()
113         {
114             return this.iterator.hasNext();
115         }
116
117         /**
118          * @see java.util.Iterator#next()
119          */

120         public E next()
121         {
122             return this.targetClass.cast(this.iterator.next());
123         }
124
125         /**
126          * @see java.util.Iterator#remove()
127          */

128         public void remove()
129         {
130             this.iterator.remove();
131         }
132     }
133     
134     private static class TypedCollection<E> implements Collection JavaDoc<E>
135     {
136         private Collection JavaDoc collection;
137         protected Class JavaDoc<E> targetClass;
138         
139         /**
140          * Constructs a new TypedCollection.
141          * @param collection
142          * @param targetClass
143          */

144         public TypedCollection(Collection JavaDoc collection, Class JavaDoc<E> targetClass)
145         {
146             this.collection = collection;
147             this.targetClass = targetClass;
148         }
149         
150         /**
151          * @param element
152          * @return boolean
153          * @see java.util.Collection#add(Object)
154          */

155         public boolean add(E element)
156         {
157             return this.collection.add(element);
158         }
159
160         /**
161          * @see java.util.Collection#addAll(java.util.Collection)
162          */

163         public boolean addAll(Collection JavaDoc<? extends E> c)
164         {
165             return this.collection.addAll(c);
166         }
167
168         /**
169          * @see java.util.Collection#clear()
170          */

171         public void clear()
172         {
173             this.collection.clear();
174         }
175
176         /**
177          * @see java.util.Collection#contains(java.lang.Object)
178          */

179         public boolean contains(Object JavaDoc element)
180         {
181             return this.collection.contains(element);
182         }
183
184         /**
185          * @see java.util.Collection#containsAll(java.util.Collection)
186          */

187         public boolean containsAll(Collection JavaDoc<?> c)
188         {
189             return this.collection.containsAll(c);
190         }
191
192         /**
193          * @see java.util.Collection#isEmpty()
194          */

195         public boolean isEmpty()
196         {
197             return this.collection.isEmpty();
198         }
199
200         /**
201          * @see java.util.Collection#iterator()
202          */

203         public Iterator JavaDoc<E> iterator()
204         {
205             return new TypedIterator(this.collection.iterator(), this.targetClass);
206         }
207
208         /**
209          * @see java.util.Collection#remove(java.lang.Object)
210          */

211         public boolean remove(Object JavaDoc element)
212         {
213             return this.collection.remove(element);
214         }
215
216         /**
217          * @see java.util.Collection#removeAll(java.util.Collection)
218          */

219         public boolean removeAll(Collection JavaDoc<?> c)
220         {
221             return this.collection.removeAll(c);
222         }
223
224         /**
225          * @see java.util.Collection#retainAll(java.util.Collection)
226          */

227         public boolean retainAll(Collection JavaDoc<?> c)
228         {
229             return this.collection.retainAll(c);
230         }
231
232         /**
233          * @see java.util.Collection#size()
234          */

235         public int size()
236         {
237             return this.collection.size();
238         }
239
240         /**
241          * @see java.util.Collection#toArray()
242          */

243         public Object JavaDoc[] toArray()
244         {
245             return this.collection.toArray();
246         }
247
248         /**
249          * @param array
250          * @param <T>
251          * @return array
252          * @see java.util.Collection#toArray(Object[])
253          */

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 JavaDoc<E>
261     {
262         /**
263          * Constructs a new TypedSet.
264          * @param collection
265          * @param targetClass
266          */

267         public TypedSet(Set JavaDoc set, Class JavaDoc<E> targetClass)
268         {
269             super(set, targetClass);
270         }
271     }
272
273     private static class TypedList<E> extends TypedCollection<E> implements List JavaDoc<E>
274     {
275         private List JavaDoc list;
276         
277         /**
278          * Constructs a new TypedList.
279          * @param list
280          * @param targetClass
281          */

282         public TypedList(List JavaDoc list, Class JavaDoc<E> targetClass)
283         {
284             super(list, targetClass);
285             
286             this.list = list;
287         }
288         
289         /**
290          * @see java.util.List#addAll(int, java.util.Collection)
291          */

292         public boolean addAll(int index, Collection JavaDoc<? extends E> collection)
293         {
294             return this.list.addAll(index, collection);
295         }
296
297         /**
298          * @see java.util.List#get(int)
299          */

300         public E get(int index)
301         {
302             return this.targetClass.cast(this.list.get(index));
303         }
304
305         /**
306          * @param index
307          * @param value
308          * @return old value
309          * @see java.util.List#set(int, Object)
310          */

311         public E set(int index, E value)
312         {
313             return this.targetClass.cast(this.list.set(index, value));
314         }
315
316         /**
317          * @param index
318          * @param value
319          * @see java.util.List#add(int, Object)
320          */

321         public void add(int index, E value)
322         {
323             this.list.add(index, value);
324         }
325
326         /**
327          * @see java.util.List#remove(int)
328          */

329         public E remove(int index)
330         {
331             return this.targetClass.cast(this.list.remove(index));
332         }
333
334         /**
335          * @see java.util.List#indexOf(java.lang.Object)
336          */

337         public int indexOf(Object JavaDoc element)
338         {
339             return this.list.indexOf(element);
340         }
341
342         /**
343          * @see java.util.List#lastIndexOf(java.lang.Object)
344          */

345         public int lastIndexOf(Object JavaDoc element)
346         {
347             return this.lastIndexOf(element);
348         }
349
350         /**
351          * @see java.util.List#listIterator()
352          */

353         public ListIterator JavaDoc<E> listIterator()
354         {
355             return new TypedListIterator(this.list.listIterator());
356         }
357
358         /**
359          * @see java.util.List#listIterator(int)
360          */

361         public ListIterator JavaDoc<E> listIterator(int index)
362         {
363             return new TypedListIterator(this.list.listIterator(index));
364         }
365
366         /**
367          * @see java.util.List#subList(int, int)
368          */

369         public List JavaDoc<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 JavaDoc<E>
375         {
376             private ListIterator JavaDoc iterator;
377             
378             /**
379              * Constructs a new TypedListIterator.
380              * @param iterator
381              */

382             public TypedListIterator(ListIterator JavaDoc iterator)
383             {
384                 super(iterator, TypedList.this.targetClass);
385                 
386                 this.iterator = iterator;
387             }
388
389             /**
390              * @see java.util.ListIterator#hasPrevious()
391              */

392             public boolean hasPrevious()
393             {
394                 return this.iterator.hasPrevious();
395             }
396
397             /**
398              * @see java.util.ListIterator#previous()
399              */

400             public E previous()
401             {
402                 return this.targetClass.cast(this.iterator.previous());
403             }
404
405             /**
406              * @see java.util.ListIterator#nextIndex()
407              */

408             public int nextIndex()
409             {
410                 return this.iterator.nextIndex();
411             }
412
413             /**
414              * @see java.util.ListIterator#previousIndex()
415              */

416             public int previousIndex()
417             {
418                 return this.iterator.previousIndex();
419             }
420
421             /**
422              * @param element
423              * @see java.util.ListIterator#set(Object)
424              */

425             public void set(E element)
426             {
427                 this.iterator.set(element);
428             }
429
430             /**
431              * @param element
432              * @see java.util.ListIterator#add(Object)
433              */

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 JavaDoc<K, V>
442     {
443         private Map JavaDoc map;
444         private Class JavaDoc<K> targetKeyClass;
445         private Class JavaDoc<V> targetValueClass;
446         
447         /**
448          * Constructs a new TypedMap.
449          * @param map
450          * @param targetKeyClass
451          * @param targetValueClass
452          */

453         public TypedMap(Map JavaDoc map, Class JavaDoc<K> targetKeyClass, Class JavaDoc<V> targetValueClass)
454         {
455             this.map = map;
456             this.targetKeyClass = targetKeyClass;
457             this.targetValueClass = targetValueClass;
458         }
459         
460         /**
461          * @see java.util.Map#size()
462          */

463         public int size()
464         {
465             return this.map.size();
466         }
467
468         /**
469          * @see java.util.Map#isEmpty()
470          */

471         public boolean isEmpty()
472         {
473             return this.map.isEmpty();
474         }
475
476         /**
477          * @see java.util.Map#containsKey(java.lang.Object)
478          */

479         public boolean containsKey(Object JavaDoc key)
480         {
481             return this.map.containsKey(key);
482         }
483
484         /**
485          * @see java.util.Map#containsValue(java.lang.Object)
486          */

487         public boolean containsValue(Object JavaDoc value)
488         {
489             return this.map.containsValue(value);
490         }
491
492         /**
493          * @see java.util.Map#get(java.lang.Object)
494          */

495         public V get(Object JavaDoc key)
496         {
497             return this.targetValueClass.cast(this.map.get(key));
498         }
499
500         /**
501          * @param key
502          * @param value
503          * @return old value
504          * @see java.util.Map#put(Object, Object)
505          */

506         public V put(K key, V value)
507         {
508             return this.targetValueClass.cast(this.map.put(key, value));
509         }
510
511         /**
512          * @see java.util.Map#remove(java.lang.Object)
513          */

514         public V remove(Object JavaDoc key)
515         {
516             return this.targetValueClass.cast(this.map.remove(key));
517         }
518
519         /**
520          * @see java.util.Map#putAll(java.util.Map)
521          */

522         public void putAll(Map JavaDoc<? extends K, ? extends V> m)
523         {
524             this.map.putAll(m);
525         }
526
527         /**
528          * @see java.util.Map#clear()
529          */

530         public void clear()
531         {
532             this.map.clear();
533         }
534
535         /**
536          * @see java.util.Map#keySet()
537          */

538         public Set JavaDoc<K> keySet()
539         {
540             return Collections.cast(this.map.keySet(), this.targetKeyClass);
541         }
542         
543         /**
544          * @see java.util.Map#values()
545          */

546         public Collection JavaDoc<V> values()
547         {
548             return Collections.cast(this.map.values(), this.targetValueClass);
549         }
550         
551         /**
552          * @see java.util.Map#entrySet()
553          */

554         public Set JavaDoc<Map.Entry JavaDoc<K, V>> entrySet()
555         {
556             return this.map.entrySet();
557         }
558     }
559 }
560
Popular Tags