View Javadoc
1   package org.exolab.castor.mapping.loader.collection.handler;
2   
3   import java.util.Enumeration;
4   import java.util.Hashtable;
5   
6   import org.exolab.castor.mapping.CollectionHandler;
7   import org.exolab.castor.mapping.MapItem;
8   import org.exolab.castor.mapping.loader.CollectionHandlers;
9   
10  public final class HashtableCollectionHandler<T> implements CollectionHandler<T> {
11  
12    @SuppressWarnings("unchecked")
13    public Object add(Object collection, T object) {
14  
15      T key = object;
16      T value = object;
17  
18      if (object instanceof org.exolab.castor.mapping.MapItem) {
19        MapItem<T, T> mapItem = (MapItem<T, T>) object;
20        key = mapItem.getKey();
21        value = mapItem.getValue();
22        if (value == null) {
23          value = object;
24        }
25        if (key == null) {
26          key = value;
27        }
28      }
29  
30      if (collection == null) {
31        collection = new Hashtable<T, T>();
32        ((Hashtable<T, T>) collection).put(key, value);
33        return collection;
34      }
35      ((Hashtable<T, T>) collection).put(key, value);
36      return null;
37    }
38  
39    @SuppressWarnings("unchecked")
40    public Enumeration<T> elements(Object collection) {
41      if (collection == null)
42        return new CollectionHandlers.EmptyEnumerator<T>();
43      return ((Hashtable<T, T>) collection).elements();
44    }
45  
46    @SuppressWarnings("unchecked")
47    public int size(Object collection) {
48      if (collection == null)
49        return 0;
50      return ((Hashtable<T, T>) collection).size();
51    }
52  
53    @SuppressWarnings("unchecked")
54    public Object clear(Object collection) {
55      if (collection != null)
56        ((Hashtable<T, T>) collection).clear();
57      return null;
58    }
59  
60    public String toString() {
61      return "Hashtable";
62    }
63  }