KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > ReferenceMap


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Set JavaDoc;
24
25
26 public class ReferenceMap implements Map JavaDoc, ReferenceCollectionListener {
27     private final ReferenceCollection collection;
28     private final Map JavaDoc 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 JavaDoc collection, Map JavaDoc map, Key key) {
40         this.collection = (ReferenceCollection) collection;
41         this.map = map;
42         this.key = key;
43         for (Iterator JavaDoc iterator = this.collection.iterator(); iterator.hasNext();) {
44             Object JavaDoc 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 JavaDoc collection, Key key) {
58         this(collection, new HashMap JavaDoc(), 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 JavaDoc getKey(Object JavaDoc 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 JavaDoc key) {
82         return map.containsKey(key);
83     }
84
85     public boolean containsValue(Object JavaDoc value) {
86         return map.containsValue(value);
87     }
88
89     public Object JavaDoc get(Object JavaDoc key) {
90         return map.get(key);
91     }
92
93     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
94         return map.put(key, value);
95     }
96
97     public Object JavaDoc remove(Object JavaDoc key) {
98         return map.remove(key);
99     }
100
101     public void putAll(Map JavaDoc t) {
102         map.putAll(t);
103     }
104
105     public void clear() {
106         map.clear();
107     }
108
109     public Set JavaDoc keySet() {
110         return map.keySet();
111     }
112
113     public Collection JavaDoc values() {
114         return map.values();
115     }
116
117     public Set JavaDoc entrySet() {
118         return map.entrySet();
119     }
120
121     public boolean equals(Object JavaDoc o) {
122         return map.equals(o);
123     }
124
125     public int hashCode() {
126         return map.hashCode();
127     }
128 }
129
Popular Tags