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: *****************************************************************************/package org.picocontainer.gems.util;
009:
010: import org.picocontainer.ComponentAdapter;
011: import org.picocontainer.MutablePicoContainer;
012: import org.picocontainer.adapters.InstanceAdapter;
013: import org.picocontainer.DefaultPicoContainer;
014:
015: import java.util.Collection;
016: import java.util.Collections;
017: import java.util.HashSet;
018: import java.util.Map;
019: import java.util.Set;
020:
021: public class PicoMap implements Map {
022:
023: private final MutablePicoContainer mutablePicoContainer;
024:
025: public PicoMap(MutablePicoContainer mutablePicoContainer) {
026: this .mutablePicoContainer = mutablePicoContainer;
027: }
028:
029: public PicoMap() {
030: mutablePicoContainer = new DefaultPicoContainer();
031: }
032:
033: public int size() {
034: return mutablePicoContainer.getComponentAdapters().size();
035: }
036:
037: public boolean isEmpty() {
038: return mutablePicoContainer.getComponentAdapters().size() == 0;
039: }
040:
041: public boolean containsKey(Object o) {
042: if (o instanceof Class) {
043: return mutablePicoContainer.getComponent((Class<?>) o) != null;
044: } else {
045: return mutablePicoContainer.getComponent(o) != null;
046: }
047: }
048:
049: public boolean containsValue(Object o) {
050: return false;
051: }
052:
053: public Object get(Object o) {
054: if (o instanceof Class) {
055: return mutablePicoContainer.getComponent((Class<?>) o);
056: } else {
057: return mutablePicoContainer.getComponent(o);
058: }
059: }
060:
061: public Object put(Object o, Object o1) {
062: Object object = remove(o);
063: mutablePicoContainer.addComponent(o, o1);
064: return object;
065: }
066:
067: public Object remove(Object o) {
068: ComponentAdapter adapter = mutablePicoContainer
069: .removeComponent(o);
070: if (adapter != null) {
071: // if previously an instance was registered, return it, otherwise return the type
072: return adapter instanceof InstanceAdapter ? adapter
073: .getComponentInstance(mutablePicoContainer)
074: : adapter.getComponentImplementation();
075: } else {
076: return null;
077: }
078: }
079:
080: public void putAll(Map map) {
081: for (Object o : map.entrySet()) {
082: final Entry entry = (Entry) o;
083: put(entry.getKey(), entry.getValue());
084: }
085: }
086:
087: public void clear() {
088: Set adapters = keySet();
089: for (Object adapter : adapters) {
090: mutablePicoContainer.removeComponent(adapter);
091: }
092: }
093:
094: public Set keySet() {
095: Set<Object> set = new HashSet<Object>();
096: Collection<ComponentAdapter<?>> adapters = mutablePicoContainer
097: .getComponentAdapters();
098: for (final ComponentAdapter<?> adapter : adapters) {
099: set.add(adapter.getComponentKey());
100: }
101: return Collections.unmodifiableSet(set);
102: }
103:
104: @SuppressWarnings({"unchecked"})
105: public Collection values() {
106: return Collections.unmodifiableCollection(mutablePicoContainer
107: .getComponents());
108: }
109:
110: public Set entrySet() {
111: Set<Entry> set = new HashSet<Entry>();
112: Collection<ComponentAdapter<?>> adapters = mutablePicoContainer
113: .getComponentAdapters();
114: for (ComponentAdapter<?> adapter : adapters) {
115: final Object key = adapter.getComponentKey();
116: final Object component = mutablePicoContainer
117: .getComponent(key);
118: set.add(new Entry() {
119: public Object getKey() {
120: return key;
121: }
122:
123: public Object getValue() {
124: return component;
125: }
126:
127: public Object setValue(Object value) {
128: throw new UnsupportedOperationException(
129: "Cannot set addComponent");
130: }
131: });
132: }
133: return Collections.unmodifiableSet(set);
134: }
135: }
|