1 /** 2 * 3 * Copyright 2005 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.gbean; 18 19 import java.util.Map; 20 import java.util.Collection; 21 import java.util.Iterator; 22 import java.util.HashMap; 23 import java.util.Set; 24 25 26 public class ReferenceMap implements Map, ReferenceCollectionListener { 27 private final ReferenceCollection collection; 28 private final Map map; 29 private final Key key; 30 31 /** 32 * Constructs the ReferenceMap using a new instance of 33 * HashMap as the internal map. 34 * 35 * @param collection Must be an instance of ReferenceCollection 36 * @param map The map instance to which references will be added/removed 37 * @param key 38 */ 39 public ReferenceMap(Collection collection, Map map, Key key) { 40 this.collection = (ReferenceCollection) collection; 41 this.map = map; 42 this.key = key; 43 for (Iterator iterator = this.collection.iterator(); iterator.hasNext();) { 44 Object object = iterator.next(); 45 map.put(key.getKey(object), object); 46 } 47 this.collection.addReferenceCollectionListener(this); 48 } 49 50 /** 51 * Constructs the ReferenceMap using a new instance of 52 * HashMap as the internal map. 53 * 54 * @param collection Must be an instance of ReferenceCollection 55 * @param key 56 */ 57 public ReferenceMap(Collection collection, Key key) { 58 this(collection, new HashMap(), key); 59 } 60 61 public void memberAdded(ReferenceCollectionEvent event) { 62 map.put(key.getKey(event.getMember()), event.getMember()); 63 } 64 65 public void memberRemoved(ReferenceCollectionEvent event) { 66 map.remove(key.getKey(event.getMember())); 67 } 68 69 public interface Key { 70 public Object getKey(Object object); 71 } 72 73 public int size() { 74 return map.size(); 75 } 76 77 public boolean isEmpty() { 78 return map.isEmpty(); 79 } 80 81 public boolean containsKey(Object key) { 82 return map.containsKey(key); 83 } 84 85 public boolean containsValue(Object value) { 86 return map.containsValue(value); 87 } 88 89 public Object get(Object key) { 90 return map.get(key); 91 } 92 93 public Object put(Object key, Object value) { 94 return map.put(key, value); 95 } 96 97 public Object remove(Object key) { 98 return map.remove(key); 99 } 100 101 public void putAll(Map t) { 102 map.putAll(t); 103 } 104 105 public void clear() { 106 map.clear(); 107 } 108 109 public Set keySet() { 110 return map.keySet(); 111 } 112 113 public Collection values() { 114 return map.values(); 115 } 116 117 public Set entrySet() { 118 return map.entrySet(); 119 } 120 121 public boolean equals(Object o) { 122 return map.equals(o); 123 } 124 125 public int hashCode() { 126 return map.hashCode(); 127 } 128 }