KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > FastStringMap


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package com.google.gwt.user.client.ui;
18
19 import com.google.gwt.core.client.GWT;
20 import com.google.gwt.core.client.JavaScriptObject;
21
22 import java.util.AbstractMap JavaDoc;
23 import java.util.AbstractSet JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  * Special-case Map implementation which imposes limits on the types of keys
33  * that can be used in return for much faster speed. In specific, only strings
34  * that could be added to a JavaScript object as keys are valid.
35  */

36
37 class FastStringMap extends AbstractMap JavaDoc {
38   private static class ImplMapEntry implements Map.Entry JavaDoc {
39
40     private Object JavaDoc key;
41
42     private Object JavaDoc value;
43
44     ImplMapEntry(String JavaDoc key, Object JavaDoc value) {
45       this.key = key;
46       this.value = value;
47     }
48
49     public boolean equals(Object JavaDoc a) {
50       if (a instanceof Map.Entry JavaDoc) {
51         Map.Entry JavaDoc s = (Map.Entry JavaDoc) a;
52         if (equalsWithNullCheck(key, s.getKey())
53             && equalsWithNullCheck(value, s.getValue())) {
54           return true;
55         }
56       }
57       return false;
58     }
59
60     // strip prefix from key
61
public Object JavaDoc getKey() {
62       return key;
63     }
64
65     public Object JavaDoc getValue() {
66       return value;
67     }
68
69     public int hashCode() {
70       int keyHash = 0;
71       int valueHash = 0;
72       if (key != null) {
73         keyHash = key.hashCode();
74       }
75       if (value != null) {
76         valueHash = value.hashCode();
77       }
78       return keyHash ^ valueHash;
79     }
80
81     public Object JavaDoc setValue(Object JavaDoc object) {
82       Object JavaDoc old = value;
83       value = object;
84       return old;
85     }
86
87     private boolean equalsWithNullCheck(Object JavaDoc a, Object JavaDoc b) {
88       if (a == b) {
89         return true;
90       } else if (a == null) {
91         return false;
92       } else {
93         return a.equals(b);
94       }
95     }
96   }
97
98   /*
99    * Accesses need to be prefixed with ':' to prevent conflict with built-in
100    * JavaScript properties.
101    */

102   private JavaScriptObject map;
103
104   public FastStringMap() {
105     init();
106   }
107
108   public void clear() {
109     init();
110   }
111
112   public boolean containsKey(Object JavaDoc key) {
113     return containsKey(keyMustBeString(key), map);
114   }
115
116   public boolean containsValue(Object JavaDoc arg0) {
117     return values().contains(arg0);
118   }
119
120   public Set JavaDoc entrySet() {
121     return new AbstractSet JavaDoc() {
122
123       public boolean contains(Object JavaDoc key) {
124         Map.Entry JavaDoc s = (Map.Entry JavaDoc) key;
125         Object JavaDoc value = get(s.getKey());
126         if (value == null) {
127           return value == s.getValue();
128         } else {
129           return value.equals(s.getValue());
130         }
131       }
132
133       public Iterator JavaDoc iterator() {
134
135         Iterator JavaDoc custom = new Iterator JavaDoc() {
136           Iterator JavaDoc keys = keySet().iterator();
137
138           public boolean hasNext() {
139             return keys.hasNext();
140           }
141
142           public Object JavaDoc next() {
143             String JavaDoc key = (String JavaDoc) keys.next();
144             return new ImplMapEntry(key, get(key));
145           }
146
147           public void remove() {
148             keys.remove();
149           }
150         };
151         return custom;
152       }
153
154       public int size() {
155         return FastStringMap.this.size();
156       }
157
158     };
159   }
160
161   public Object JavaDoc get(Object JavaDoc key) {
162     return get(keyMustBeString(key));
163   }
164
165   // Prepend ':' to avoid conflicts with built-in Object properties.
166
public native Object JavaDoc get(String JavaDoc key) /*-{
167     var value
168         = this.@com.google.gwt.user.client.ui.FastStringMap::map[':' + key];
169     return (value == null) ? null : value;
170   }-*/
;
171
172   public boolean isEmpty() {
173     return size() == 0;
174   }
175
176   public Set JavaDoc keySet() {
177     return new AbstractSet JavaDoc() {
178       public boolean contains(Object JavaDoc key) {
179         return containsKey(key);
180       }
181
182       public Iterator JavaDoc iterator() {
183         List JavaDoc l = new ArrayList JavaDoc();
184         addAllKeysFromJavascriptObject(l, map);
185         return l.iterator();
186       }
187
188       public int size() {
189         return FastStringMap.this.size();
190       }
191     };
192   }
193
194   public Object JavaDoc put(Object JavaDoc key, Object JavaDoc widget) {
195     return put(keyMustBeString(key), widget);
196   }
197
198   // Prepend ':' to avoid conflicts with built-in Object properties.
199
public native Object JavaDoc put(String JavaDoc key, Object JavaDoc widget) /*-{
200     var previous
201         = this.@com.google.gwt.user.client.ui.FastStringMap::map[':' + key];
202     this.@com.google.gwt.user.client.ui.FastStringMap::map[':' + key] = widget;
203     return (previous == null) ? null : previous;
204   }-*/
;
205
206   public void putAll(Map JavaDoc arg0) {
207     Iterator JavaDoc iter = arg0.entrySet().iterator();
208     while (iter.hasNext()) {
209       Map.Entry JavaDoc entry = (Entry) iter.next();
210       put(entry.getKey(), entry.getValue());
211     }
212   }
213
214   public Object JavaDoc remove(Object JavaDoc key) {
215     return remove(keyMustBeString(key));
216   }
217
218   // only count keys with ':' prefix
219
public native int size() /*-{
220     var value = this.@com.google.gwt.user.client.ui.FastStringMap::map;
221     var count = 0;
222     for(var key in value) {
223       if (key.charAt(0) == ':') ++count;
224     }
225     return count;
226   }-*/
;
227
228   public Collection JavaDoc values() {
229     List JavaDoc values = new ArrayList JavaDoc();
230     addAllValuesFromJavascriptObject(values, map);
231     return values;
232   }
233
234   // only count keys with ':' prefix
235
private native void addAllKeysFromJavascriptObject(Collection JavaDoc s,
236       JavaScriptObject javaScriptObject) /*-{
237     for(var key in javaScriptObject) {
238       if (key.charAt(0) != ':') continue;
239       s.@java.util.Collection::add(Ljava/lang/Object;)(key.substring(1));
240     }
241   }-*/
;
242
243   // only count keys with ':' prefix
244
private native void addAllValuesFromJavascriptObject(Collection JavaDoc s,
245       JavaScriptObject javaScriptObject) /*-{
246     for(var key in javaScriptObject) {
247       if (key.charAt(0) != ':') continue;
248       var value = javaScriptObject[key];
249       s.@java.util.Collection::add(Ljava/lang/Object;)(value);
250     }
251   }-*/
;
252
253   // Prepend ':' to avoid conflicts with built-in Object properties.
254
private native boolean containsKey(String JavaDoc key, JavaScriptObject obj)/*-{
255     return obj[':' + key] !== undefined;
256   }-*/
;
257
258   private native void init() /*-{
259     this.@com.google.gwt.user.client.ui.FastStringMap::map = [];
260   }-*/
;
261
262   private String JavaDoc keyMustBeString(Object JavaDoc key) {
263     if (key instanceof String JavaDoc) {
264       return (String JavaDoc) key;
265     } else {
266       throw new IllegalArgumentException JavaDoc(GWT.getTypeName(this)
267           + " can only have Strings as keys, not" + key);
268     }
269   }
270
271   // Prepend ':' to avoid conflicts with built-in Object properties.
272
private native Object JavaDoc remove(String JavaDoc key) /*-{
273     var previous
274         = this.@com.google.gwt.user.client.ui.FastStringMap::map[':' + key];
275     delete this.@com.google.gwt.user.client.ui.FastStringMap::map[':' + key];
276     return (previous == null) ? null : previous;
277   }-*/
;
278 }
279
Popular Tags