1 package org.exolab.castor.mapping.loader.collection.handler;
2
3 import java.util.Enumeration;
4 import java.util.PriorityQueue;
5 import java.util.Queue;
6
7 import org.exolab.castor.mapping.CollectionHandler;
8 import org.exolab.castor.mapping.loader.CollectionHandlers;
9 import org.exolab.castor.mapping.loader.J2CollectionHandlers.IteratorEnumerator;
10
11 public final class QueueCollectionHandler<T> implements CollectionHandler<T> {
12
13 @SuppressWarnings("unchecked")
14 public Object add(Object collection, T object) {
15 if (collection == null) {
16 collection = new PriorityQueue<T>();
17 ((Queue<T>) collection).add(object);
18 return collection;
19 }
20 ((Queue<T>) collection).add(object);
21 return null;
22 }
23
24 @SuppressWarnings("unchecked")
25 public Enumeration<T> elements(Object collection) {
26 if (collection == null)
27 return new CollectionHandlers.EmptyEnumerator<T>();
28 return new IteratorEnumerator<T>(((Queue<T>) collection).iterator());
29 }
30
31 @SuppressWarnings("unchecked")
32 public int size(Object collection) {
33 if (collection == null)
34 return 0;
35 return ((Queue<T>) collection).size();
36 }
37
38 @SuppressWarnings("unchecked")
39 public Object clear(Object collection) {
40 if (collection != null)
41 ((Queue<T>) collection).clear();
42 return null;
43 }
44
45 public String toString() {
46 return "Queue";
47 }
48 }