1 /* 2 * Copyright 2010 Daniel Kurka 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package com.googlecode.mgwt.collection.client; 15 16 import com.google.gwt.core.client.JavaScriptObject; 17 import com.googlecode.mgwt.collection.shared.LightArray; 18 19 /** 20 * This is a the javascript implementation of {@link LightArray}, which makes direct use of native 21 * javascript arrays 22 * 23 * @author Daniel Kurka 24 * 25 * @param <T> the Type of the object to put into the array 26 */ 27 public class JsLightArray<T> implements LightArray<T> { 28 29 private JavaScriptObject array; 30 31 /** 32 * Construct a {@link JsLightArray} 33 */ 34 public JsLightArray() { 35 this(JavaScriptObject.createArray()); 36 } 37 38 /** 39 * Construct a {@link JsLightArray} with a given javascript array 40 * 41 * @param array the array to use 42 */ 43 public JsLightArray(JavaScriptObject array) { 44 this.array = array; 45 } 46 47 @Override 48 public native T get(int index) /*-{ 49 return this.@com.googlecode.mgwt.collection.client.JsLightArray::array[index]; 50 }-*/; 51 52 /** 53 * returns the underlying javascript array 54 * 55 * @return the javascript array 56 */ 57 public JavaScriptObject getArray() { 58 return array; 59 } 60 61 @Override 62 public native int length()/*-{ 63 return this.@com.googlecode.mgwt.collection.client.JsLightArray::array.length; 64 }-*/; 65 66 @Override 67 public native void push(T value)/*-{ 68 this.@com.googlecode.mgwt.collection.client.JsLightArray::array[this.@com.googlecode.mgwt.collection.client.JsLightArray::array.length] = value; 69 }-*/; 70 71 @Override 72 public native void set(int index, T value) /*-{ 73 this.@com.googlecode.mgwt.collection.client.JsLightArray::array[index] = value; 74 }-*/; 75 76 @Override 77 public native T shift() /*-{ 78 return this.@com.googlecode.mgwt.collection.client.JsLightArray::array.shift(); 79 }-*/; 80 81 @Override 82 public native void unshift(T value)/*-{ 83 this.@com.googlecode.mgwt.collection.client.JsLightArray::array.unshift(value); 84 }-*/; 85 86 }