You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by da...@apache.org on 2015/09/21 13:58:28 UTC

[7/8] wicket git commit: Removed copy of children for get(int)

Removed copy of children for get(int)

Copying the children for getting a component by index seemed wasteful,
implemented get using iterator.


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/5271f27c
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/5271f27c
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/5271f27c

Branch: refs/heads/master
Commit: 5271f27c8f6a2f451f3587f8313af0c6da84f9c6
Parents: d7cf974
Author: Martijn Dashorst <ma...@gmail.com>
Authored: Mon Sep 21 13:40:42 2015 +0200
Committer: Martijn Dashorst <ma...@gmail.com>
Committed: Mon Sep 21 13:40:42 2015 +0200

----------------------------------------------------------------------
 .../java/org/apache/wicket/MarkupContainer.java | 14 ++++-
 .../org/apache/wicket/MarkupContainerTest.java  | 64 ++++++++++++++++++++
 2 files changed, 77 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/5271f27c/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java b/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
index ca79052..5f0d035 100644
--- a/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
+++ b/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
@@ -1007,12 +1007,24 @@ public abstract class MarkupContainer extends Component implements Iterable<Comp
 	 * the number of children.
 	 * 
 	 * @param index
+	 *            the index of the child in this container
 	 * @throws ArrayIndexOutOfBoundsException
+	 *             when {@code index} exceeds {@code size()}
 	 * @return child component at the specified index
 	 */
 	public final Component get(int index)
 	{
-		return copyChildren().get(index);
+		Component childAtIndex = null;
+		Iterator<Component> childIterator = iterator();
+		while (index-- >= 0 && childIterator.hasNext())
+		{
+			childAtIndex = childIterator.next();
+		}
+		if(childAtIndex == null) 
+		{
+			throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
+		}
+		return childAtIndex;
 	}
 
 	/**

http://git-wip-us.apache.org/repos/asf/wicket/blob/5271f27c/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java b/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java
index 4c24c02..b21757d 100644
--- a/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java
@@ -16,6 +16,10 @@
  */
 package org.apache.wicket;
 
+import static org.hamcrest.CoreMatchers.is;
+
+import java.util.NoSuchElementException;
+
 import org.apache.wicket.markup.IMarkupResourceStreamProvider;
 import org.apache.wicket.markup.html.WebComponent;
 import org.apache.wicket.markup.html.WebMarkupContainer;
@@ -23,6 +27,7 @@ import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.util.resource.IResourceStream;
 import org.apache.wicket.util.resource.StringResourceStream;
 import org.apache.wicket.util.tester.WicketTestCase;
+import org.junit.Assert;
 import org.junit.Test;
 
 
@@ -109,6 +114,65 @@ public class MarkupContainerTest extends WicketTestCase
 	}
 
 	/**
+	 * Tests the get(int) method of MarkupContainer.
+	 */
+	@Test
+	public void getIndexed() 
+	{
+		MarkupContainer c = new WebMarkupContainer("parent");
+		Component c1 = new WebComponent("c1");
+		Component c2 = new WebComponent("c2");
+
+		c.add(c1);
+		c.add(c2);
+
+		assertThat(c.get(0), is(c1));
+		assertThat(c.get(1), is(c2));
+	}
+
+	/**
+	 * Tests the get(int) method of MarkupContainer when the index exceeds the number of children.
+	 */
+	@Test(expected = ArrayIndexOutOfBoundsException.class)
+	public void getIndexedArrayIndexOutOfBoundsException() 
+	{
+		MarkupContainer c = new WebMarkupContainer("parent");
+		c.get(0);
+	}
+
+	/**
+	 * Tests the swap method.
+	 */
+	@Test
+	public void swap() 
+	{
+		MarkupContainer c = new WebMarkupContainer("parent");
+		Component c1 = new WebComponent("c1");
+		Component c2 = new WebComponent("c2");
+		Component c3 = new WebComponent("c3");
+
+		c.add(c1);
+		c.add(c2);
+		c.add(c3);
+
+		assertThat(c.get(0), is(c1));
+		assertThat(c.get(1), is(c2));
+		assertThat(c.get(2), is(c3));
+
+		c.swap(0, 1);
+		
+		assertThat(c.get(0), is(c2));
+		assertThat(c.get(1), is(c1));
+		assertThat(c.get(2), is(c3));
+
+		c.swap(0, 2);
+
+		assertThat(c.get(0), is(c3));
+		assertThat(c.get(1), is(c1));
+		assertThat(c.get(2), is(c2));
+	}
+
+	/**
 	 * https://issues.apache.org/jira/browse/WICKET-4006
 	 */
 	@Test(expected = IllegalArgumentException.class)