1 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 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 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 } 129 | Popular Tags |