View Javadoc
1   /*
2    * Copyright 2010 Daniel Kurka
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  package com.googlecode.mgwt.collection.client;
17  
18  import com.google.gwt.core.client.JavaScriptObject;
19  import com.googlecode.mgwt.collection.shared.LightArray;
20  import com.googlecode.mgwt.collection.shared.LightMap;
21  
22  /**
23   * An implementation of {@link LightMap} that uses native javascript objects as dictionaries
24   * 
25   * @author Daniel Kurka
26   * 
27   * @param <V> the tyope of object to store
28   */
29  public class JsLightMap<V> implements LightMap<V> {
30  	private JavaScriptObject map;
31  
32    /**
33     * Construct a {@link JsLightMap}
34     */
35  	public JsLightMap() {
36  		this(JavaScriptObject.createObject());
37  	}
38  
39    /**
40     * Construct a {@link JsLightMap} using a given javascript object
41     * 
42     * @param data the javascript object to use
43     */
44  	public JsLightMap(JavaScriptObject data) {
45  		if (data == null) {
46  			throw new IllegalArgumentException("data must not be null");
47  		}
48  		this.map = data;
49  	}
50  
51  	@Override
52  	public final void clear() {
53  		clearData();
54  	}
55  
56  	private final native void clearData() /*-{
57  		this.@com.googlecode.mgwt.collection.client.JsLightMap::map = {};
58    }-*/;
59  
60  	@Override
61  	public final native boolean containsKey(String key) /*-{
62  		return (this.@com.googlecode.mgwt.collection.client.JsLightMap::map)[key] != null;
63    }-*/;
64  
65  	@Override
66  	public void remove(String key) {
67  		nativeDelete(key);
68  	}
69  
70  	private native V nativeDelete(String key) /*-{
71  		delete (this.@com.googlecode.mgwt.collection.client.JsLightMap::map)[key];
72    }-*/;
73  
74  	@Override
75  	public V get(String key) {
76  		return nativeGet(key);
77  	}
78  
79  	private native V nativeGet(String key) /*-{
80  		return (this.@com.googlecode.mgwt.collection.client.JsLightMap::map)[key];
81    }-*/;
82  
83  	@Override
84  	public void put(String key, V value) {
85  		nativePut(key, value);
86  
87  	}
88  
89  	private native V nativePut(String key, V value) /*-{
90  		(this.@com.googlecode.mgwt.collection.client.JsLightMap::map)[key] = value;
91    }-*/;
92  
93  	@Override
94  	public LightArray<String> getKeys() {
95  		JavaScriptObject array = getNativeKeyArray();
96  		return new JsLightArray<String>(array);
97  	}
98  
99  	private native JavaScriptObject getNativeKeyArray()/*-{
100 		var array = [];
101 		for ( var key in this.@com.googlecode.mgwt.collection.client.JsLightMap::map) {
102 			array.push(key);
103 		}
104 		return array;
105   }-*/;
106 
107   /**
108    * get the underlying javascript object
109    * 
110    * @return the underlying javascript object
111    */
112 	public JavaScriptObject getMap() {
113 		return map;
114 	}
115 
116 }