001 /*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * *
008 *****************************************************************************/
009 package org.picocontainer.gems.util;
010
011 import org.picocontainer.ComponentAdapter;
012 import org.picocontainer.MutablePicoContainer;
013 import org.picocontainer.defaults.DefaultPicoContainer;
014 import org.picocontainer.defaults.InstanceComponentAdapter;
015
016 import java.util.Collection;
017 import java.util.Collections;
018 import java.util.HashSet;
019 import java.util.Iterator;
020 import java.util.Map;
021 import java.util.Set;
022
023
024 public class PicoMap implements Map {
025
026 private final MutablePicoContainer mutablePicoContainer;
027
028 public PicoMap(MutablePicoContainer mutablePicoContainer) {
029 this.mutablePicoContainer = mutablePicoContainer;
030 }
031
032 public PicoMap() {
033 mutablePicoContainer = new DefaultPicoContainer();
034 }
035
036 public int size() {
037 return mutablePicoContainer.getComponentAdapters().size();
038 }
039
040 public boolean isEmpty() {
041 return mutablePicoContainer.getComponentAdapters().size() == 0;
042 }
043
044 public boolean containsKey(Object o) {
045 if (o instanceof Class) {
046 return mutablePicoContainer.getComponentInstanceOfType((Class)o) != null;
047 } else {
048 return mutablePicoContainer.getComponentInstance(o) != null;
049 }
050 }
051
052 public boolean containsValue(Object o) {
053 return false;
054 }
055
056 public Object get(Object o) {
057 if (o instanceof Class) {
058 return mutablePicoContainer.getComponentInstanceOfType((Class)o);
059 } else {
060 return mutablePicoContainer.getComponentInstance(o);
061 }
062 }
063
064 public Object put(Object o, Object o1) {
065 Object object = remove(o);
066 if (o1 instanceof Class) {
067 mutablePicoContainer.registerComponentImplementation(o, (Class)o1);
068 } else {
069 mutablePicoContainer.registerComponentInstance(o, o1);
070 }
071 return object;
072 }
073
074 public Object remove(Object o) {
075 ComponentAdapter adapter = mutablePicoContainer.unregisterComponent(o);
076 if (adapter != null) {
077 // if previously an instance was registered, return it, otherwise return the type
078 return adapter instanceof InstanceComponentAdapter ? adapter
079 .getComponentInstance(mutablePicoContainer) : adapter
080 .getComponentImplementation();
081 } else {
082 return null;
083 }
084 }
085
086 public void putAll(Map map) {
087 for (final Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
088 final Map.Entry entry = (Map.Entry)iter.next();
089 put(entry.getKey(), entry.getValue());
090 }
091 }
092
093 public void clear() {
094 Set adapters = keySet();
095 for (final Iterator iter = adapters.iterator(); iter.hasNext();) {
096 mutablePicoContainer.unregisterComponent(iter.next());
097 }
098 }
099
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 }