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.LightArrayInt;
20  
21  /**
22   * An implementation of {@link LightArrayInt} that uses native javascript arrays
23   * 
24   * @author Daniel Kurka
25   * 
26   */
27  public class JsLightArrayInteger implements LightArrayInt {
28  
29  	private JavaScriptObject array;
30  
31    /**
32     * Construct a {@link JsLightArrayInteger}
33     */
34  	public JsLightArrayInteger() {
35  		this(JavaScriptObject.createArray());
36  	}
37  
38    /**
39     * Construct a {@link JsLightArrayInteger} with a given javascript array
40     * 
41     * @param array the array to use
42     * 
43     */
44  	public JsLightArrayInteger(JavaScriptObject array) {
45  		this.array = array;
46  	}
47  
48  	@Override
49  	public native void push(int value)/*-{
50  		this.@com.googlecode.mgwt.collection.client.JsLightArrayInteger::array[this.@com.googlecode.mgwt.collection.client.JsLightArrayInteger::array.length] = value;
51    }-*/;
52  
53  	@Override
54  	public native int shift() /*-{
55  		return this.@com.googlecode.mgwt.collection.client.JsLightArrayInteger::array
56  				.shift();
57    }-*/;
58  
59  	@Override
60  	public native int get(int index) /*-{
61  		return this.@com.googlecode.mgwt.collection.client.JsLightArrayInteger::array[index];
62    }-*/;
63  
64  	@Override
65  	public native void set(int index, int value) /*-{
66  		this.@com.googlecode.mgwt.collection.client.JsLightArrayInteger::array[index] = value;
67    }-*/;
68  
69  	@Override
70  	public native int length()/*-{
71  		return this.@com.googlecode.mgwt.collection.client.JsLightArrayInteger::array.length;
72    }-*/;
73  
74  	@Override
75  	public native void unshift(int value)/*-{
76  		this.@com.googlecode.mgwt.collection.client.JsLightArrayInteger::array
77  				.unshift(value);
78    }-*/;
79  
80    /**
81     * get the underlying javascript array
82     * 
83     * @return the underlying javascript array
84     */
85  	public JavaScriptObject getArray() {
86  		return array;
87  	}
88  
89  }