001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gbean;
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Set;
022:
023: public class ReferenceMap implements Map, ReferenceCollectionListener {
024: private final Map map;
025: private final Key key;
026:
027: /**
028: * Constructs the ReferenceMap using a new instance of
029: * HashMap as the internal map.
030: *
031: * @param collection Must be an instance of ReferenceCollection
032: * @param map The map instance to which references will be added/removed
033: * @param key
034: */
035: public ReferenceMap(Collection collection, Map map, Key key) {
036: this .map = map;
037: this .key = key;
038: for (Object object : collection) {
039: map.put(key.getKey(object), object);
040: }
041: if (collection instanceof ReferenceCollection) {
042: ((ReferenceCollection) collection)
043: .addReferenceCollectionListener(this );
044: }
045: }
046:
047: /**
048: * Constructs the ReferenceMap using a new instance of
049: * HashMap as the internal map.
050: *
051: * @param collection Must be an instance of ReferenceCollection
052: * @param key
053: */
054: public ReferenceMap(Collection collection, Key key) {
055: this (collection, new HashMap(), key);
056: }
057:
058: public void memberAdded(ReferenceCollectionEvent event) {
059: map.put(key.getKey(event.getMember()), event.getMember());
060: }
061:
062: public void memberRemoved(ReferenceCollectionEvent event) {
063: map.remove(key.getKey(event.getMember()));
064: }
065:
066: public interface Key {
067: public Object getKey(Object object);
068: }
069:
070: public int size() {
071: return map.size();
072: }
073:
074: public boolean isEmpty() {
075: return map.isEmpty();
076: }
077:
078: public boolean containsKey(Object key) {
079: return map.containsKey(key);
080: }
081:
082: public boolean containsValue(Object value) {
083: return map.containsValue(value);
084: }
085:
086: public Object get(Object key) {
087: return map.get(key);
088: }
089:
090: public Object put(Object key, Object value) {
091: return map.put(key, value);
092: }
093:
094: public Object remove(Object key) {
095: return map.remove(key);
096: }
097:
098: public void putAll(Map t) {
099: map.putAll(t);
100: }
101:
102: public void clear() {
103: map.clear();
104: }
105:
106: public Set keySet() {
107: return map.keySet();
108: }
109:
110: public Collection values() {
111: return map.values();
112: }
113:
114: public Set entrySet() {
115: return map.entrySet();
116: }
117:
118: public boolean equals(Object o) {
119: return map.equals(o);
120: }
121:
122: public int hashCode() {
123: return map.hashCode();
124: }
125: }
|