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 */
017 package org.apache.geronimo.gbean;
018
019 import java.util.Collection;
020 import java.util.HashMap;
021 import java.util.Map;
022 import java.util.Set;
023
024
025 public class ReferenceMap implements Map, ReferenceCollectionListener {
026 private final Map map;
027 private final Key key;
028
029 /**
030 * Constructs the ReferenceMap using a new instance of
031 * HashMap as the internal map.
032 *
033 * @param collection Must be an instance of ReferenceCollection
034 * @param map The map instance to which references will be added/removed
035 * @param key
036 */
037 public ReferenceMap(Collection collection, Map map, Key key) {
038 this.map = map;
039 this.key = key;
040 for (Object object : collection) {
041 map.put(key.getKey(object), object);
042 }
043 if (collection instanceof ReferenceCollection) {
044 ((ReferenceCollection)collection).addReferenceCollectionListener(this);
045 }
046 }
047
048 /**
049 * Constructs the ReferenceMap using a new instance of
050 * HashMap as the internal map.
051 *
052 * @param collection Must be an instance of ReferenceCollection
053 * @param key
054 */
055 public ReferenceMap(Collection collection, Key key) {
056 this(collection, new HashMap(), key);
057 }
058
059 public void memberAdded(ReferenceCollectionEvent event) {
060 map.put(key.getKey(event.getMember()), event.getMember());
061 }
062
063 public void memberRemoved(ReferenceCollectionEvent event) {
064 map.remove(key.getKey(event.getMember()));
065 }
066
067 public interface Key {
068 public Object getKey(Object object);
069 }
070
071 public int size() {
072 return map.size();
073 }
074
075 public boolean isEmpty() {
076 return map.isEmpty();
077 }
078
079 public boolean containsKey(Object key) {
080 return map.containsKey(key);
081 }
082
083 public boolean containsValue(Object value) {
084 return map.containsValue(value);
085 }
086
087 public Object get(Object key) {
088 return map.get(key);
089 }
090
091 public Object put(Object key, Object value) {
092 return map.put(key, value);
093 }
094
095 public Object remove(Object key) {
096 return map.remove(key);
097 }
098
099 public void putAll(Map t) {
100 map.putAll(t);
101 }
102
103 public void clear() {
104 map.clear();
105 }
106
107 public Set keySet() {
108 return map.keySet();
109 }
110
111 public Collection values() {
112 return map.values();
113 }
114
115 public Set entrySet() {
116 return map.entrySet();
117 }
118
119 public boolean equals(Object o) {
120 return map.equals(o);
121 }
122
123 public int hashCode() {
124 return map.hashCode();
125 }
126 }