org.oscim.tiling.source.geojson.JsArrayCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vtm-web Show documentation
Show all versions of vtm-web Show documentation
OpenGL vector map library written in Java - running on Android, iOS, Desktop and within the browser.
package org.oscim.tiling.source.geojson;
import com.google.gwt.core.client.JavaScriptObject;
import java.util.AbstractCollection;
import java.util.Iterator;
/**
* a Java Friendly way of working with Js Arrays using the Java.util.Collection
* API
* https://groups.google.com/forum/#!topic/google-web-toolkit/_8X9WPL6qwM
*
* @param
* @author sg
*/
public class JsArrayCollection extends AbstractCollection {
private JsArr _data;
/**
* creates an empty array
*/
public JsArrayCollection() {
_data = JsArr.create().cast();
}
/**
* creates JsArrayCollection wrapping an existing js array
*/
public JsArrayCollection(JavaScriptObject data) {
this._data = data.cast();
}
public static JsArrayCollection create(JavaScriptObject data) {
return new JsArrayCollection(data);
}
@Override
public Iterator iterator() {
return new JsArrayIterator(this);
}
@Override
public int size() {
return _data.size();
}
public static class JsArrayIterator implements Iterator {
private JsArrayCollection arr;
int currentIndex;
public JsArrayIterator(JsArrayCollection arr) {
this.arr = arr;
currentIndex = 0;
}
@Override
public boolean hasNext() {
// System.out.println(currentIndex+" - "+arr.size());
return currentIndex < arr.size();
}
@Override
public T next() {
currentIndex++;
return arr._data.get(currentIndex - 1);
}
@Override
public void remove() {
arr._data.slice(currentIndex - 1, currentIndex);
}
}
/**
* untyped array
*/
private static class JsArr extends JavaScriptObject {
protected JsArr() {
}
public final native JsArr slice(int start, int end)/*-{
return this.slice(start, end);
}-*/;
public static final native JsArr create() /*-{
return [];
}-*/;
public final native int size() /*-{
return this.length;
}-*/;
public final native T get(int i) /*-{
return this[i];
}-*/;
}
}