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