KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > impl > ContainerMap


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.impl;
17
18 import java.util.AbstractMap JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.directwebremoting.Container;
26
27 /**
28  * A {@link Map} that uses a {@link Container} as it's source of data.
29  * @author Joe Walker [joe at getahead dot ltd dot uk]
30  */

31 public class ContainerMap extends AbstractMap JavaDoc implements Map JavaDoc
32 {
33     /**
34      * Create a ContainerMap with a Container to proxy requests to
35      * @param container The Container that we proxy to
36      * @param filterNonStringValues Does the map include created beans?
37      */

38     public ContainerMap(Container container, boolean filterNonStringValues)
39     {
40         this.container = container;
41         this.filterNonStringValues = filterNonStringValues;
42     }
43
44     /**
45      * Read the {@link Container} and cache the values
46      */

47     private void init()
48     {
49         if (proxy == null)
50         {
51             proxy = new HashMap JavaDoc();
52             for (Iterator JavaDoc it = container.getBeanNames().iterator(); it.hasNext();)
53             {
54                 String JavaDoc name = (String JavaDoc) it.next();
55                 Object JavaDoc value = container.getBean(name);
56
57                 if (!filterNonStringValues || value instanceof String JavaDoc)
58                 {
59                     proxy.put(name, value);
60                 }
61             }
62         }
63     }
64
65     /* (non-Javadoc)
66      * @see java.util.AbstractMap#get(java.lang.Object)
67      */

68     public Object JavaDoc get(Object JavaDoc key)
69     {
70         init();
71         return proxy.get(key);
72     }
73
74     /* (non-Javadoc)
75      * @see java.util.AbstractMap#entrySet()
76      */

77     public Set JavaDoc entrySet()
78     {
79         init();
80         return proxy.entrySet();
81     }
82
83     /* (non-Javadoc)
84      * @see java.util.AbstractMap#containsKey(java.lang.Object)
85      */

86     public boolean containsKey(Object JavaDoc key)
87     {
88         init();
89         return proxy.containsKey(key);
90     }
91
92     /* (non-Javadoc)
93      * @see java.util.AbstractMap#containsValue(java.lang.Object)
94      */

95     public boolean containsValue(Object JavaDoc value)
96     {
97         init();
98         return proxy.containsValue(value);
99     }
100
101     /* (non-Javadoc)
102      * @see java.util.AbstractMap#equals(java.lang.Object)
103      */

104     public boolean equals(Object JavaDoc o)
105     {
106         init();
107         return proxy.equals(o);
108     }
109
110     /* (non-Javadoc)
111      * @see java.util.AbstractMap#hashCode()
112      */

113     public int hashCode()
114     {
115         init();
116         return proxy.hashCode();
117     }
118
119     /* (non-Javadoc)
120      * @see java.util.AbstractMap#isEmpty()
121      */

122     public boolean isEmpty()
123     {
124         init();
125         return proxy.isEmpty();
126     }
127
128     /* (non-Javadoc)
129      * @see java.util.AbstractMap#keySet()
130      */

131     public Set JavaDoc keySet()
132     {
133         init();
134         return proxy.keySet();
135     }
136
137     /* (non-Javadoc)
138      * @see java.util.AbstractMap#size()
139      */

140     public int size()
141     {
142         init();
143         return proxy.size();
144     }
145
146     /* (non-Javadoc)
147      * @see java.util.AbstractMap#values()
148      */

149     public Collection JavaDoc values()
150     {
151         init();
152         return proxy.values();
153     }
154
155     /* (non-Javadoc)
156      * @see java.util.AbstractMap#toString()
157      */

158     public String JavaDoc toString()
159     {
160         init();
161         return proxy.toString();
162     }
163
164     /* (non-Javadoc)
165      * @see java.util.AbstractMap#put(java.lang.Object, java.lang.Object)
166      */

167     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
168     {
169         throw new UnsupportedOperationException JavaDoc("ContainerMaps are read only");
170     }
171
172     /* (non-Javadoc)
173      * @see java.util.AbstractMap#putAll(java.util.Map)
174      */

175     public void putAll(Map JavaDoc t)
176     {
177         throw new UnsupportedOperationException JavaDoc("ContainerMaps are read only");
178     }
179
180     /* (non-Javadoc)
181      * @see java.util.AbstractMap#remove(java.lang.Object)
182      */

183     public Object JavaDoc remove(Object JavaDoc key)
184     {
185         throw new UnsupportedOperationException JavaDoc("ContainerMaps are read only");
186     }
187
188     /* (non-Javadoc)
189      * @see java.util.AbstractMap#clear()
190      */

191     public void clear()
192     {
193         throw new UnsupportedOperationException JavaDoc("ContainerMaps are read only");
194     }
195
196     /**
197      * Are we filtering non-string values from the container
198      */

199     private boolean filterNonStringValues;
200
201     /**
202      * The Container that we proxy to.
203      */

204     private Container container;
205
206     /**
207      * The cache of filtered values
208      */

209     protected Map JavaDoc proxy;
210 }
211
Popular Tags