View Javadoc
1   package org.exolab.castor.mapping.loader.collection.handler;
2   
3   import java.util.Enumeration;
4   import java.util.Set;
5   import java.util.TreeSet;
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 SortedSetCollectionHandler<T> implements CollectionHandler<T> {
12  
13    @SuppressWarnings("unchecked")
14    public Object add(Object collection, final T object) {
15      if (collection == null) {
16        collection = new TreeSet<T>();
17        ((Set<T>) collection).add(object);
18        return collection;
19      }
20      // if (!((Set) collection).contains(object))
21      ((Set<T>) collection).add(object);
22      return null;
23  
24    }
25  
26    @SuppressWarnings("unchecked")
27    public Enumeration<T> elements(final Object collection) {
28      if (collection == null) {
29        return new CollectionHandlers.EmptyEnumerator<T>();
30      }
31      return new IteratorEnumerator<T>(((Set<T>) collection).iterator());
32    }
33  
34    @SuppressWarnings("unchecked")
35    public int size(final Object collection) {
36      if (collection == null) {
37        return 0;
38      }
39      return ((Set<T>) collection).size();
40    }
41  
42    @SuppressWarnings("unchecked")
43    public Object clear(final Object collection) {
44      if (collection != null) {
45        ((Set<T>) collection).clear();
46      }
47      return null;
48    }
49  
50    public String toString() {
51      return "SortedSet";
52    }
53  }