Revision: 6398
Author: [email protected]
Date: Fri Oct 16 12:49:04 2009
Log: Add unshift(), join(), and setLength() methods to JsArray types.

Patch by: bobv
http://code.google.com/p/google-web-toolkit/source/detail?r=6398

Modified:
  /trunk/user/src/com/google/gwt/core/client/JsArray.java
  /trunk/user/src/com/google/gwt/core/client/JsArrayBoolean.java
  /trunk/user/src/com/google/gwt/core/client/JsArrayInteger.java
  /trunk/user/src/com/google/gwt/core/client/JsArrayNumber.java
  /trunk/user/src/com/google/gwt/core/client/JsArrayString.java
  /trunk/user/test/com/google/gwt/core/client/JsArrayTest.java

=======================================
--- /trunk/user/src/com/google/gwt/core/client/JsArray.java     Wed Apr  8  
17:12:14 2009
+++ /trunk/user/src/com/google/gwt/core/client/JsArray.java     Fri Oct 16  
12:49:04 2009
@@ -1,12 +1,12 @@
  /*
   * Copyright 2008 Google Inc.
- *
+ *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may  
not
   * use this file except in compliance with the License. You may obtain a  
copy of
   * the License at
- *
+ *
   * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -18,10 +18,10 @@
  /**
   * A simple wrapper around a homogeneous native array of
   * {...@link JavaScriptObject} values.
- *
+ *
   * This class may not be directly instantiated, and can only be returned  
from a
   * native method. For example,
- *
+ *
   * <code>
   * native JsArray<JavaScriptObject> getNativeArray() /*-{
   *   return [
@@ -31,7 +31,7 @@
   *   ];
   * }-* /;
   * </code>
- *
+ *
   * @param <T> the concrete type of object contained in this array
   */
  public class JsArray<T extends JavaScriptObject> extends JavaScriptObject {
@@ -41,18 +41,36 @@

    /**
     * Gets the object at a given index.
-   *
+   *
     * @param index the index to be retrieved
-   * @return the object at the given index, or <code>null</code> if none
-   *         exists
+   * @return the object at the given index, or <code>null</code> if none  
exists
     */
    public final native T get(int index) /*-{
      return this[index];
    }-*/;

+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final String join() {
+    // As per JS spec
+    return join(",");
+  }
+
+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final native String join(String separator) /*-{
+    return this.join(separator);
+  }-*/;
+
    /**
     * Gets the length of the array.
-   *
+   *
     * @return the array length
     */
    public final native int length() /*-{
@@ -68,10 +86,10 @@

    /**
     * Sets the object value at a given index.
-   *
+   *
     * If the index is out of bounds, the value will still be set. The  
array's
     * length will be updated to encompass the bounds implied by the added  
object.
-   *
+   *
     * @param index the index to be set
     * @param value the object to be stored
     */
@@ -79,12 +97,30 @@
      this[index] = value;
    }-*/;

+  /**
+   * Reset the length of the array.
+   *
+   * @param newLength the new length of the array
+   */
+  public final native void setLength(int newLength) /*-{
+    this.length = newLength;
+  }-*/;
+
    /**
     * Shifts the first value off the array.
+   *
     * @return the shifted value
     */
    public final native T shift() /*-{
      return this.shift();
    }-*/;
-
-}
+
+  /**
+   * Shifts a value onto the beginning of the array.
+   *
+   * @param value the value to the stored
+   */
+  public final native void unshift(T value) /*-{
+    this.unshift(value);
+  }-*/;
+}
=======================================
--- /trunk/user/src/com/google/gwt/core/client/JsArrayBoolean.java      Wed Apr 
  
8 17:12:14 2009
+++ /trunk/user/src/com/google/gwt/core/client/JsArrayBoolean.java      Fri Oct 
 
16 12:49:04 2009
@@ -1,12 +1,12 @@
  /*
   * Copyright 2008 Google Inc.
- *
+ *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may  
not
   * use this file except in compliance with the License. You may obtain a  
copy of
   * the License at
- *
+ *
   * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -17,10 +17,10 @@

  /**
   * A simple wrapper around a homogeneous native array of boolean values.
- *
+ *
   * This class may not be directly instantiated, and can only be returned  
from a
   * native method. For example,
- *
+ *
   * <code>
   * native JsArrayBoolean getNativeArray() /*-{
   *   return [true, false, true];
@@ -34,21 +34,40 @@

    /**
     * Gets the value at a given index.
-   *
+   *
     * If an undefined or non-boolean value exists at the given index, a
     * type-conversion error will occur in hosted mode and unpredictable  
behavior
     * may occur in web mode.
-   *
+   *
     * @param index the index to be retrieved
     * @return the value at the given index
     */
    public final native boolean get(int index) /*-{
      return this[index];
    }-*/;
-
+
+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final String join() {
+    // As per JS spec
+    return join(",");
+  }
+
+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final native String join(String separator) /*-{
+    return this.join(separator);
+  }-*/;
+
    /**
     * Gets the length of the array.
-   *
+   *
     * @return the array length
     */
    public final native int length() /*-{
@@ -64,10 +83,10 @@

    /**
     * Sets the value value at a given index.
-   *
+   *
     * If the index is out of bounds, the value will still be set. The  
array's
     * length will be updated to encompass the bounds implied by the added  
value.
-   *
+   *
     * @param index the index to be set
     * @param value the value to be stored
     */
@@ -75,12 +94,30 @@
      this[index] = value;
    }-*/;

+  /**
+   * Reset the length of the array.
+   *
+   * @param newLength the new length of the array
+   */
+  public final native void setLength(int newLength) /*-{
+    this.length = newLength;
+  }-*/;
+
    /**
     * Shifts the first value off the array.
+   *
     * @return the shifted value
     */
    public final native boolean shift() /*-{
      return this.shift();
    }-*/;

-}
+  /**
+   * Shifts a value onto the beginning of the array.
+   *
+   * @param value the value to the stored
+   */
+  public final native void unshift(boolean value) /*-{
+    this.unshift(value);
+  }-*/;
+}
=======================================
--- /trunk/user/src/com/google/gwt/core/client/JsArrayInteger.java      Wed Apr 
  
8 17:12:14 2009
+++ /trunk/user/src/com/google/gwt/core/client/JsArrayInteger.java      Fri Oct 
 
16 12:49:04 2009
@@ -1,12 +1,12 @@
  /*
   * Copyright 2008 Google Inc.
- *
+ *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may  
not
   * use this file except in compliance with the License. You may obtain a  
copy of
   * the License at
- *
+ *
   * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -17,10 +17,10 @@

  /**
   * A simple wrapper around a homogeneous native array of integer values.
- *
+ *
   * This class may not be directly instantiated, and can only be returned  
from a
   * native method. For example,
- *
+ *
   * <code>
   * native JsArrayInteger getNativeArray() /*-{
   *   return [1, 2, 3];
@@ -34,12 +34,12 @@

    /**
     * Gets the value at a given index.
-   *
+   *
     * If no value exists at the given index, a type-conversion error will  
occur
     * in hosted mode and unpredictable behavior may occur in web mode. If  
the
     * numeric value returned is non-integral, it will cause a warning in  
hosted
     * mode, and may affect the results of mathematical expressions.
-   *
+   *
     * @param index the index to be retrieved
     * @return the value at the given index
     */
@@ -47,9 +47,28 @@
      return this[index];
    }-*/;

+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final String join() {
+    // As per JS spec
+    return join(",");
+  }
+
+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final native String join(String separator) /*-{
+    return this.join(separator);
+  }-*/;
+
    /**
     * Gets the length of the array.
-   *
+   *
     * @return the array length
     */
    public final native int length() /*-{
@@ -65,10 +84,10 @@

    /**
     * Sets the value value at a given index.
-   *
+   *
     * If the index is out of bounds, the value will still be set. The  
array's
     * length will be updated to encompass the bounds implied by the added  
value.
-   *
+   *
     * @param index the index to be set
     * @param value the value to be stored
     */
@@ -76,11 +95,30 @@
      this[index] = value;
    }-*/;

+  /**
+   * Reset the length of the array.
+   *
+   * @param newLength the new length of the array
+   */
+  public final native void setLength(int newLength) /*-{
+    this.length = newLength;
+  }-*/;
+
    /**
     * Shifts the first value off the array.
+   *
     * @return the shifted value
     */
    public final native int shift() /*-{
      return this.shift();
    }-*/;
-}
+
+  /**
+   * Shifts a value onto the beginning of the array.
+   *
+   * @param value the value to the stored
+   */
+  public final native void unshift(int value) /*-{
+    this.unshift(value);
+  }-*/;
+}
=======================================
--- /trunk/user/src/com/google/gwt/core/client/JsArrayNumber.java       Mon May 
 
18 11:47:32 2009
+++ /trunk/user/src/com/google/gwt/core/client/JsArrayNumber.java       Fri Oct 
 
16 12:49:04 2009
@@ -1,12 +1,12 @@
  /*
   * Copyright 2008 Google Inc.
- *
+ *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may  
not
   * use this file except in compliance with the License. You may obtain a  
copy of
   * the License at
- *
+ *
   * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -17,13 +17,13 @@

  /**
   * A simple wrapper around a homogeneous native array of numeric values.
- *
+ *
   * All native JavaScript numeric values are implicitly double-precision,  
so only
   * double values may be set and retrieved.
- *
+ *
   * This class may not be directly instantiated, and can only be returned  
from a
   * native method. For example,
- *
+ *
   * <code>
   * native JsArrayNumber getNativeArray() /*-{
   *   return [1.1, 2.2, 3.3];
@@ -31,17 +31,17 @@
   * </code>
   */
  public class JsArrayNumber extends JavaScriptObject {
-
+
    protected JsArrayNumber() {
    }

    /**
     * Gets the value at a given index.
-   *
+   *
     * If an undefined or non-numeric value exists at the given index, a
     * type-conversion error will occur in hosted mode and unpredictable  
behavior
     * may occur in web mode.
-   *
+   *
     * @param index the index to be retrieved
     * @return the value at the given index
     */
@@ -49,9 +49,28 @@
      return this[index];
    }-*/;

+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final String join() {
+    // As per JS spec
+    return join(",");
+  }
+
+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final native String join(String separator) /*-{
+    return this.join(separator);
+  }-*/;
+
    /**
     * Gets the length of the array.
-   *
+   *
     * @return the array length
     */
    public final native int length() /*-{
@@ -67,10 +86,10 @@

    /**
     * Sets the value value at a given index.
-   *
+   *
     * If the index is out of bounds, the value will still be set. The  
array's
     * length will be updated to encompass the bounds implied by the added  
value.
-   *
+   *
     * @param index the index to be set
     * @param value the value to be stored
     */
@@ -78,11 +97,30 @@
      this[index] = value;
    }-*/;

+  /**
+   * Reset the length of the array.
+   *
+   * @param newLength the new length of the array
+   */
+  public final native void setLength(int newLength) /*-{
+    this.length = newLength;
+  }-*/;
+
    /**
     * Shifts the first value off the array.
+   *
     * @return the shifted value
     */
    public final native double shift() /*-{
      return this.shift();
    }-*/;
-}
+
+  /**
+   * Shifts a value onto the beginning of the array.
+   *
+   * @param value the value to the stored
+   */
+  public final native void unshift(double value) /*-{
+    this.unshift(value);
+  }-*/;
+}
=======================================
--- /trunk/user/src/com/google/gwt/core/client/JsArrayString.java       Wed Apr 
  
8 17:12:14 2009
+++ /trunk/user/src/com/google/gwt/core/client/JsArrayString.java       Fri Oct 
 
16 12:49:04 2009
@@ -1,12 +1,12 @@
  /*
   * Copyright 2008 Google Inc.
- *
+ *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may  
not
   * use this file except in compliance with the License. You may obtain a  
copy of
   * the License at
- *
+ *
   * http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -17,10 +17,10 @@

  /**
   * A simple wrapper around a homogeneous native array of string values.
- *
+ *
   * This class may not be directly instantiated, and can only be returned  
from a
   * native method. For example,
- *
+ *
   * <code>
   * native JsArrayString getNativeArray() /*-{
   *   return ['foo', 'bar', 'baz'];
@@ -34,7 +34,7 @@

    /**
     * Gets the value at a given index.
-   *
+   *
     * @param index the index to be retrieved
     * @return the value at the given index, or <code>null</code> if none  
exists
     */
@@ -42,9 +42,28 @@
      return this[index];
    }-*/;

+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final String join() {
+    // As per JS spec
+    return join(",");
+  }
+
+  /**
+   * Convert each element of the array to a String and join them with a  
comma
+   * separator. The value returned from this method may vary between  
browsers
+   * based on how JavaScript values are converted into strings.
+   */
+  public final native String join(String separator) /*-{
+    return this.join(separator);
+  }-*/;
+
    /**
     * Gets the length of the array.
-   *
+   *
     * @return the array length
     */
    public final native int length() /*-{
@@ -60,10 +79,10 @@

    /**
     * Sets the value value at a given index.
-   *
+   *
     * If the index is out of bounds, the value will still be set. The  
array's
     * length will be updated to encompass the bounds implied by the added  
value.
-   *
+   *
     * @param index the index to be set
     * @param value the value to be stored
     */
@@ -71,11 +90,30 @@
      this[index] = value;
    }-*/;

+  /**
+   * Reset the length of the array.
+   *
+   * @param newLength the new length of the array
+   */
+  public final native void setLength(int newLength) /*-{
+    this.length = newLength;
+  }-*/;
+
    /**
     * Shifts the first value off the array.
+   *
     * @return the shifted value
     */
    public final native String shift() /*-{
      return this.shift();
    }-*/;
-}
+
+  /**
+   * Shifts a value onto the beginning of the array.
+   *
+   * @param value the value to the stored
+   */
+  public final native void unshift(String value) /*-{
+    this.unshift(value);
+  }-*/;
+}
=======================================
--- /trunk/user/test/com/google/gwt/core/client/JsArrayTest.java        Wed Jun 
25  
08:05:37 2008
+++ /trunk/user/test/com/google/gwt/core/client/JsArrayTest.java        Fri Oct 
16  
12:49:04 2009
@@ -50,6 +50,9 @@
      JsPoint p1 = jsArray.get(1);
      JsPoint p2 = jsArray.get(2);

+    assertEquals("JsPoint,JsPoint,JsPoint", jsArray.join());
+    assertEquals("JsPoint:JsPoint:JsPoint", jsArray.join(":"));
+
      assertEquals(0, p0.x());
      assertEquals(1, p0.y());
      assertEquals(2, p1.x());
@@ -77,6 +80,9 @@
        } catch (Throwable e) {
        }
      }
+
+    jsArray.setLength(0);
+    assertEquals(0, jsArray.length());
    }

    public void testJsArrayBoolean() {
@@ -89,6 +95,9 @@
      assertEquals(false, jsArray.get(1));
      assertEquals(true, jsArray.get(2));

+    assertEquals("true,false,true", jsArray.join());
+    assertEquals("true:false:true", jsArray.join(":"));
+
      // Make sure getting the '3' element throws an exception in hosted mode
      // (this won't happen in web mode).
      if (!GWT.isScript()) {
@@ -114,7 +123,20 @@
          fail("Expected an exception getting an invalid value in hosted  
mode");
        } catch (Throwable e) {
        }
-    }
+    } else {
+      // Keep the length of the array sane for the remainer of the test
+      jsArray.set(4, false);
+    }
+
+    // Add an element to the beginning of the array
+    jsArray.unshift(true);
+    assertEquals(6, jsArray.length());
+    assertTrue(jsArray.get(0));
+    assertTrue(jsArray.shift());
+    assertEquals(5, jsArray.length());
+
+    jsArray.setLength(0);
+    assertEquals(0, jsArray.length());
    }

    public void testJsArrayInteger() {
@@ -127,6 +149,9 @@
      assertEquals(1, jsArray.get(1));
      assertEquals(2, jsArray.get(2));

+    assertEquals("0,1,2", jsArray.join());
+    assertEquals("0:1:2", jsArray.join(":"));
+
      // Make sure getting the '3' element throws an exception in hosted mode
      // (this won't happen in web mode).
      if (!GWT.isScript()) {
@@ -152,7 +177,20 @@
          fail("Expected an exception getting an invalid value in hosted  
mode");
        } catch (Throwable e) {
        }
-    }
+    } else {
+      // Keep the length of the array sane for the remainer of the test
+      jsArray.set(4, 33);
+    }
+
+    // Add an element to the beginning of the array
+    jsArray.unshift(42);
+    assertEquals(6, jsArray.length());
+    assertEquals(42, jsArray.get(0));
+    assertEquals(42, jsArray.shift());
+    assertEquals(5, jsArray.length());
+
+    jsArray.setLength(0);
+    assertEquals(0, jsArray.length());
    }

    public void testJsArrayNumber() {
@@ -165,6 +203,9 @@
      assertEquals(1.1, jsArray.get(1));
      assertEquals(2.2, jsArray.get(2));

+    assertEquals("0,1.1,2.2", jsArray.join());
+    assertEquals("0:1.1:2.2", jsArray.join(":"));
+
      // Make sure getting the '3' element throws an exception in hosted mode
      // (this won't happen in web mode).
      if (!GWT.isScript()) {
@@ -190,7 +231,20 @@
          fail("Expected an exception getting an invalid value in hosted  
mode");
        } catch (Throwable e) {
        }
-    }
+    } else {
+      // Keep the length of the array sane for the remainer of the test
+      jsArray.set(4, 4.4);
+    }
+
+    // Add an element to the beginning of the array
+    jsArray.unshift(42.0);
+    assertEquals(6, jsArray.length());
+    assertEquals(42.0, jsArray.get(0));
+    assertEquals(42.0, jsArray.shift());
+    assertEquals(5, jsArray.length());
+
+    jsArray.setLength(0);
+    assertEquals(0, jsArray.length());
    }

    public void testJsArrayString() {
@@ -207,6 +261,9 @@
      assertEquals("bar", s1);
      assertEquals("baz", s2);

+    assertEquals("foo,bar,baz", jsArray.join());
+    assertEquals("foo:bar:baz", jsArray.join(":"));
+
      // Make sure the '3' element is null.
      assertNull(jsArray.get(3));

@@ -225,14 +282,27 @@
          fail("Expected an exception getting an invalid value in hosted  
mode");
        } catch (Throwable e) {
        }
-    }
+    } else {
+      // Keep the length of the array sane for the remainer of the test
+      jsArray.set(4, "quux");
+    }
+
+    // Add an element to the beginning of the array
+    jsArray.unshift("42");
+    assertEquals(6, jsArray.length());
+    assertEquals("42", jsArray.get(0));
+    assertEquals("42", jsArray.shift());
+    assertEquals(5, jsArray.length());
+
+    jsArray.setLength(0);
+    assertEquals(0, jsArray.length());
    }

    private native JsArray<JsPoint> makeJsArray() /*-{
      return [
-      { x: 0, y: 1},
-      { x: 2, y: 3},
-      { x: 4, y: 5},
+      { x: 0, y: 1, toString: function() { return 'JsPoint';} },
+      { x: 2, y: 3, toString: function() { return 'JsPoint';} },
+      { x: 4, y: 5, toString: function() { return 'JsPoint';} },
      ];
    }-*/;


--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to