Daniel Kurka has uploaded a new change for review.

  https://gwt-review.googlesource.com/2440


Change subject: Widget Iterator now throws NoSuchElementExceptions correctly.
......................................................................

Widget Iterator now throws NoSuchElementExceptions correctly.

Change-Id: I07fd1507226a185b94ae1e85b6eb0235ec370a9f
---
M user/src/com/google/gwt/user/client/ui/WidgetCollection.java
M user/test/com/google/gwt/user/client/ui/WidgetCollectionTest.java
2 files changed, 63 insertions(+), 30 deletions(-)



diff --git a/user/src/com/google/gwt/user/client/ui/WidgetCollection.java b/user/src/com/google/gwt/user/client/ui/WidgetCollection.java
index d1459ac..812fe19 100644
--- a/user/src/com/google/gwt/user/client/ui/WidgetCollection.java
+++ b/user/src/com/google/gwt/user/client/ui/WidgetCollection.java
@@ -1,16 +1,14 @@
 /*
  * Copyright 2007 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
+ * 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
- * License for the specific language governing permissions and limitations under + * 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 License for the specific language governing permissions and limitations under
  * the License.
  */
 package com.google.gwt.user.client.ui;
@@ -19,16 +17,15 @@
 import java.util.NoSuchElementException;

 /**
- * A simple collection of widgets to be used by
- * {@link com.google.gwt.user.client.ui.Panel panels} and
- * {@link com.google.gwt.user.client.ui.Composite composites}.
+ * A simple collection of widgets to be used by {@link com.google.gwt.user.client.ui.Panel panels}
+ * and {@link com.google.gwt.user.client.ui.Composite composites}.
  *
  * <p>
  * The main purpose of this specialized collection is to implement
- * {@link java.util.Iterator#remove()} in a way that delegates removal to its
- * panel. This makes it much easier for the panel to implement an
- * {@link com.google.gwt.user.client.ui.HasWidgets#iterator() iterator} that
- * supports removal of widgets.
+ * {@link java.util.Iterator#remove()} in a way that delegates removal to its panel. This makes it
+ * much easier for the panel to implement an
+ * {@link com.google.gwt.user.client.ui.HasWidgets#iterator() iterator} that supports removal of
+ * widgets.
  * </p>
  */
 public class WidgetCollection implements Iterable<Widget> {
@@ -42,9 +39,10 @@
     }

     public Widget next() {
-      if (index >= size) {
+      if (index + 1 >= size) {
         throw new NoSuchElementException();
       }
+
       return array[++index];
     }

@@ -65,8 +63,8 @@
   /**
    * Constructs a new widget collection.
    *
- * @param parent the container whose {@link HasWidgets#remove(Widget)} will be - * delegated to by the iterator's {@link Iterator#remove()} method. + * @param parent the container whose {@link HasWidgets#remove(Widget)} will be delegated to by the
+   *          iterator's {@link Iterator#remove()} method.
    */
   public WidgetCollection(HasWidgets parent) {
     this.parent = parent;
@@ -111,8 +109,7 @@
    * Gets the index of the specified index.
    *
    * @param w the widget to be found
-   * @return the index of the specified widget, or <code>-1</code> if it is
-   *         not found
+ * @return the index of the specified widget, or <code>-1</code> if it is not found
    */
   public int indexOf(Widget w) {
     for (int i = 0; i < size; ++i) {
@@ -129,8 +126,7 @@
    *
    * @param w the widget to be inserted
    * @param beforeIndex the index before which the widget will be inserted
- * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of
-   *           range
+ * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of range
    */
   public void insert(Widget w, int beforeIndex) {
     if ((beforeIndex < 0) || (beforeIndex > size)) {
@@ -157,8 +153,8 @@
   }

   /**
- * Gets an iterator on this widget collection. This iterator is guaranteed to
-   * implement remove() in terms of its containing {@link HasWidgets}.
+ * Gets an iterator on this widget collection. This iterator is guaranteed to implement remove()
+   * in terms of its containing {@link HasWidgets}.
    *
    * @return an iterator
    */
diff --git a/user/test/com/google/gwt/user/client/ui/WidgetCollectionTest.java b/user/test/com/google/gwt/user/client/ui/WidgetCollectionTest.java
index 121b54c..aec2106 100644
--- a/user/test/com/google/gwt/user/client/ui/WidgetCollectionTest.java
+++ b/user/test/com/google/gwt/user/client/ui/WidgetCollectionTest.java
@@ -1,16 +1,14 @@
 /*
  * Copyright 2007 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
+ * 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
- * License for the specific language governing permissions and limitations under + * 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 License for the specific language governing permissions and limitations under
  * the License.
  */
 package com.google.gwt.user.client.ui;
@@ -18,6 +16,7 @@
 import com.google.gwt.junit.client.GWTTestCase;

 import java.util.Iterator;
+import java.util.NoSuchElementException;

 /**
  * TODO: document me.
@@ -97,4 +96,42 @@
     assertEquals(it.next(), l2);
     assertFalse(it.hasNext());
   }
+
+  public void testExceptionInInterator() {
+
+    Container c = new Container();
+    WidgetCollection wc = c.collection;
+
+    wc.add(new Button("a"));
+    wc.add(new Button("b"));
+
+    Iterator<Widget> iter = wc.iterator();
+    iter.next();
+    iter.next();
+    try {
+      iter.next();
+      fail("expected NoSuchElementException");
+    } catch (NoSuchElementException expected) {
+    }
+
+    c = new Container();
+    wc = c.collection;
+
+    wc.add(new Button("a"));
+    wc.add(new Button("b"));
+    wc.add(new Button("c"));
+    wc.add(new Button("d"));
+
+    iter = wc.iterator();
+    iter.next();
+    iter.next();
+    iter.next();
+    iter.next();
+    iter.remove();
+    try {
+      iter.next();
+      fail("expected NoSuchElementException");
+    } catch (NoSuchElementException expected) {
+    }
+  }
 }

--
To view, visit https://gwt-review.googlesource.com/2440
To unsubscribe, visit https://gwt-review.googlesource.com/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I07fd1507226a185b94ae1e85b6eb0235ec370a9f
Gerrit-PatchSet: 1
Gerrit-Project: gwt
Gerrit-Branch: master
Gerrit-Owner: Daniel Kurka <[email protected]>

--
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors
--- You received this message because you are subscribed to the Google Groups "Google Web Toolkit Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to