001 /** 002 * 003 * Copyright 2005 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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.Map; 020 import java.util.Collection; 021 import java.util.Iterator; 022 import java.util.HashMap; 023 import java.util.Set; 024 025 026 public class ReferenceMap implements Map, ReferenceCollectionListener { 027 private final ReferenceCollection collection; 028 private final Map map; 029 private final Key key; 030 031 /** 032 * Constructs the ReferenceMap using a new instance of 033 * HashMap as the internal map. 034 * 035 * @param collection Must be an instance of ReferenceCollection 036 * @param map The map instance to which references will be added/removed 037 * @param key 038 */ 039 public ReferenceMap(Collection collection, Map map, Key key) { 040 this.collection = (ReferenceCollection) collection; 041 this.map = map; 042 this.key = key; 043 for (Iterator iterator = this.collection.iterator(); iterator.hasNext();) { 044 Object object = iterator.next(); 045 map.put(key.getKey(object), object); 046 } 047 this.collection.addReferenceCollectionListener(this); 048 } 049 050 /** 051 * Constructs the ReferenceMap using a new instance of 052 * HashMap as the internal map. 053 * 054 * @param collection Must be an instance of ReferenceCollection 055 * @param key 056 */ 057 public ReferenceMap(Collection collection, Key key) { 058 this(collection, new HashMap(), key); 059 } 060 061 public void memberAdded(ReferenceCollectionEvent event) { 062 map.put(key.getKey(event.getMember()), event.getMember()); 063 } 064 065 public void memberRemoved(ReferenceCollectionEvent event) { 066 map.remove(key.getKey(event.getMember())); 067 } 068 069 public interface Key { 070 public Object getKey(Object object); 071 } 072 073 public int size() { 074 return map.size(); 075 } 076 077 public boolean isEmpty() { 078 return map.isEmpty(); 079 } 080 081 public boolean containsKey(Object key) { 082 return map.containsKey(key); 083 } 084 085 public boolean containsValue(Object value) { 086 return map.containsValue(value); 087 } 088 089 public Object get(Object key) { 090 return map.get(key); 091 } 092 093 public Object put(Object key, Object value) { 094 return map.put(key, value); 095 } 096 097 public Object remove(Object key) { 098 return map.remove(key); 099 } 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 }