Hi,
I have some questions and proposals around the JsArray-classes because
they are handy when working with JSNI but I believe they could be
improved a bit.
a) Why are not all native methods provided through the wrapper.
Missing methods are:
* concat
* pop
* reverse
* slice
* splice
* sort
b) It would be nice if one could use them in a for-loop but because an
interface can only be implemented by one native class it can't
implement Iterable directly but provide a method like iterable().
An implementation for JsArrayString would look like this:
### Eclipse Workspace Patch 1.0
#P gwt-toolkit
Index: user/src/com/google/gwt/core/client/JsArrayString.java
===================================================================
--- user/src/com/google/gwt/core/client/JsArrayString.java (revision
7519)
+++ user/src/com/google/gwt/core/client/JsArrayString.java (working
copy)
@@ -15,6 +15,11 @@
*/
package com.google.gwt.core.client;
+import java.util.Iterator;
+
+import com.google.gwt.emul.java.util.AbstractList;
+import com.google.gwt.emul.java.util.NoSuchElementException;
+
/**
* A simple wrapper around a homogeneous native array of string
values.
*
@@ -116,4 +121,92 @@
public final native void unshift(String value) /*-{
this.unshift(value);
}-*/;
+
+ /**
+ * Concatenate the receiver and parameter arrays and returns the
resulting array
+ *
+ * @param concat the array to append
+ * @return the concatenated array
+ */
+ public final native JsArrayString concat(JsArrayString concat) /*-{
+ return this.concat(concat);
+ }-*/;
+
+ /**
+ * Removes the last element in the array
+ *
+ * @return the remove value
+ */
+ public final native String pop() /*-{
+ return this.pop();
+ }-*/;
+
+ /**
+ * Extracts items starting from the given index to the end of the
array
+ *
+ * @param startIndex the start index
+ * @return the resulting array
+ */
+ public final native JsArrayString slice(int startIndex) /*-{
+ return this.slice(startIndex);
+ }-*/;
+
+ /**
+ * Extracts items starting from the startIndex (inclusive) to the
endIndex (exclusive)
+ *
+ * @param startIndex the startIndex
+ * @param endIndex the endIndex (negative values also allowed)
+ * @return the resulting array
+ */
+ public final native void slice(int startIndex, int endIndex) /*-{
+ this.slice(startIndex, endIndex);
+ }-*/;
+
+ /**
+ * Removes a given amount of items from the array starting with the
given index
+ *
+ * @param startIndex the start index
+ * @param removedItems the number of items to remove
+ */
+ public final native void splice(int startIndex, int removedItems) /
*-{
+ this.splice(startIndex,removedItems);
+ }-*/;
+
+ public final Iterable<String> iterable() {
+ return new Iterable<String>() {
+
+ public Iterator<String> iterator() {
+ return new IteratorImpl();
+ }
+ };
+ }
+
+ private class IteratorImpl implements Iterator<String> {
+ /*
+ * i is the index of the item that will be returned on the next
call to
+ * next() last is the index of the item that was returned on the
previous
+ * call to next() or previous (for ListIterator), -1 if no such
item exists.
+ */
+ int i = 0, last = -1;
+
+ public boolean hasNext() {
+ return i < JsArrayString.this.length();
+ }
+
+ public String next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+ return JsArrayString.this.get(last = i++);
+ }
+
+ public void remove() {
+ if (last < 0) {
+ throw new IllegalStateException();
+ }
+ JsArrayString.this.splice(last, 1);
+ i = last;
+ last = -1;
+ }
+ }
}
I don't know if it is a good idea to implement the iterable directly
and whether it's making sense for the 2 JsArray with primitive values
(Integer,Number) but that's why I'm posting here before filing an
enhancement request.
Tom
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors