1 9 package org.picocontainer.gems.util; 10 11 import org.picocontainer.ComponentAdapter; 12 import org.picocontainer.MutablePicoContainer; 13 import org.picocontainer.defaults.DefaultPicoContainer; 14 import org.picocontainer.defaults.InstanceComponentAdapter; 15 16 import java.util.Collection ; 17 import java.util.Collections ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 24 public class PicoMap implements Map { 25 26 private final MutablePicoContainer mutablePicoContainer; 27 28 public PicoMap(MutablePicoContainer mutablePicoContainer) { 29 this.mutablePicoContainer = mutablePicoContainer; 30 } 31 32 public PicoMap() { 33 mutablePicoContainer = new DefaultPicoContainer(); 34 } 35 36 public int size() { 37 return mutablePicoContainer.getComponentAdapters().size(); 38 } 39 40 public boolean isEmpty() { 41 return mutablePicoContainer.getComponentAdapters().size() == 0; 42 } 43 44 public boolean containsKey(Object o) { 45 if (o instanceof Class ) { 46 return mutablePicoContainer.getComponentInstanceOfType((Class )o) != null; 47 } else { 48 return mutablePicoContainer.getComponentInstance(o) != null; 49 } 50 } 51 52 public boolean containsValue(Object o) { 53 return false; 54 } 55 56 public Object get(Object o) { 57 if (o instanceof Class ) { 58 return mutablePicoContainer.getComponentInstanceOfType((Class )o); 59 } else { 60 return mutablePicoContainer.getComponentInstance(o); 61 } 62 } 63 64 public Object put(Object o, Object o1) { 65 Object object = remove(o); 66 if (o1 instanceof Class ) { 67 mutablePicoContainer.registerComponentImplementation(o, (Class )o1); 68 } else { 69 mutablePicoContainer.registerComponentInstance(o, o1); 70 } 71 return object; 72 } 73 74 public Object remove(Object o) { 75 ComponentAdapter adapter = mutablePicoContainer.unregisterComponent(o); 76 if (adapter != null) { 77 return adapter instanceof InstanceComponentAdapter ? adapter 79 .getComponentInstance(mutablePicoContainer) : adapter 80 .getComponentImplementation(); 81 } else { 82 return null; 83 } 84 } 85 86 public void putAll(Map map) { 87 for (final Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 88 final Map.Entry entry = (Map.Entry )iter.next(); 89 put(entry.getKey(), entry.getValue()); 90 } 91 } 92 93 public void clear() { 94 Set adapters = keySet(); 95 for (final Iterator iter = adapters.iterator(); iter.hasNext();) { 96 mutablePicoContainer.unregisterComponent(iter.next()); 97 } 98 } 99 100 public Set keySet() { 101 Set set = new HashSet (); 102 Collection adapters = mutablePicoContainer.getComponentAdapters(); 103 for (final Iterator iter = adapters.iterator(); iter.hasNext();) { 104 final ComponentAdapter adapter = (ComponentAdapter)iter.next(); 105 set.add(adapter.getComponentKey()); 106 } 107 return Collections.unmodifiableSet(set); 108 } 109 110 public Collection values() { 111 return Collections.unmodifiableCollection(mutablePicoContainer.getComponentInstances()); 112 } 113 114 public Set entrySet() { 115 Set set = new HashSet (); 116 Collection adapters = mutablePicoContainer.getComponentAdapters(); 117 for (final Iterator iter = adapters.iterator(); iter.hasNext();) { 118 final Object key = ((ComponentAdapter)iter.next()).getComponentKey(); 119 final Object component = mutablePicoContainer.getComponentInstance(key); 120 set.add(new Map.Entry () { 121 public Object getKey() { 122 return key; 123 } 124 125 public Object getValue() { 126 return component; 127 } 128 129 public Object setValue(Object value) { 130 throw new UnsupportedOperationException ("Cannot set component"); 131 } 132 }); 133 } 134 return Collections.unmodifiableSet(set); 135 } 136 } 137 | Popular Tags |