Author: nbelaevski
Date: 2009-10-25 17:34:24 -0400 (Sun, 25 Oct 2009)
New Revision: 15757
Added:
root/ui-sandbox/trunk/components/tables/ui/src/test/java/
root/ui-sandbox/trunk/components/tables/ui/src/test/java/org/
root/ui-sandbox/trunk/components/tables/ui/src/test/java/org/richfaces/
root/ui-sandbox/trunk/components/tables/ui/src/test/java/org/richfaces/component/
root/ui-sandbox/trunk/components/tables/ui/src/test/java/org/richfaces/component/IteratorBaseTest.java
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/ColumnsIterator.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/DataIterator.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/FixedChildrenIterator.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/IteratorBase.java
Log:
IteratorBase refactoring
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/ColumnsIterator.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/ColumnsIterator.java 2009-10-25
20:17:50 UTC (rev 15756)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/ColumnsIterator.java 2009-10-25
21:34:24 UTC (rev 15757)
@@ -40,7 +40,7 @@
this.childrenIterator = dataTable.getChildren().iterator();
}
- protected javax.faces.component.UIColumn nextComponent(){
+ protected javax.faces.component.UIColumn nextItem(){
while (childrenIterator != null && childrenIterator.hasNext()) {
UIComponent child = childrenIterator.next();
if(child instanceof javax.faces.component.UIColumn){
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/DataIterator.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/DataIterator.java 2009-10-25
20:17:50 UTC (rev 15756)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/DataIterator.java 2009-10-25
21:34:24 UTC (rev 15757)
@@ -36,7 +36,7 @@
this.facetsIterator = dataTable.getFacets().values().iterator();
}
- protected UIComponent nextComponent() {
+ protected UIComponent nextItem() {
UIComponent nextColumn = null;
while (nextColumn == null && childrenIterator.hasNext()) {
UIComponent child = childrenIterator.next();
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/FixedChildrenIterator.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/FixedChildrenIterator.java 2009-10-25
20:17:50 UTC (rev 15756)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/FixedChildrenIterator.java 2009-10-25
21:34:24 UTC (rev 15757)
@@ -34,7 +34,7 @@
}
@Override
- protected UIComponent nextComponent() {
+ protected UIComponent nextItem() {
UIComponent next = null;
if(currentColumnIterator != null && currentColumnIterator.hasNext()) {
@@ -48,7 +48,7 @@
UIComponent child = childrenIterator.next();
if((child instanceof UIColumn) && child.isRendered()) {
currentColumnIterator = getChildFacetIterator(child);
- next = nextComponent();
+ next = nextItem();
} else if(checkAjaxComponent(child)) {
next = child;
}
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/IteratorBase.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/IteratorBase.java 2009-10-25
20:17:50 UTC (rev 15756)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/IteratorBase.java 2009-10-25
21:34:24 UTC (rev 15757)
@@ -5,29 +5,41 @@
/**
* @author Anton Belevich
- *
+ * @author Nick Belaevski
*/
public abstract class IteratorBase<E> implements Iterator<E> {
- private boolean initialized = false;
+ private boolean isCompleted = false;
- private E next;
+ private E next = null;
+ private void setupNext() {
+ if (!isCompleted) {
+ if (next == null) {
+ next = nextItem();
- public boolean hasNext() {
- if(!initialized){
- next = nextComponent();
- initialized = true;
+ if (next == null) {
+ isCompleted = true;
+ }
+ }
}
- return null != next;
}
+
+ public boolean hasNext() {
+ setupNext();
+
+ return next != null;
+ }
public E next() {
- if (!hasNext()) {
+ setupNext();
+
+ if (next == null) {
throw new NoSuchElementException();
- }
+ }
+
E result = next;
- next = nextComponent();
+ next = null;
return result;
}
@@ -35,5 +47,5 @@
throw new UnsupportedOperationException("Iterator is read-only");
}
- abstract protected E nextComponent();
+ abstract protected E nextItem();
}
Added:
root/ui-sandbox/trunk/components/tables/ui/src/test/java/org/richfaces/component/IteratorBaseTest.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/test/java/org/richfaces/component/IteratorBaseTest.java
(rev 0)
+++
root/ui-sandbox/trunk/components/tables/ui/src/test/java/org/richfaces/component/IteratorBaseTest.java 2009-10-25
21:34:24 UTC (rev 15757)
@@ -0,0 +1,150 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.component;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.junit.Test;
+
+
+/**
+ * @author Nick Belaevski
+ */
+public class IteratorBaseTest {
+
+ private static final List<String> strings =
Collections.unmodifiableList(Arrays.asList("a", "b", "c"));
+
+ private static class IteratorImpl<E> extends IteratorBase<E> {
+
+ private Iterator<E> iterator;
+
+ public IteratorImpl(List<E> list) {
+ super();
+
+ this.iterator = list.iterator();
+ }
+
+ @Override
+ protected E nextItem() {
+ if (iterator.hasNext()) {
+ return iterator.next();
+ }
+
+ return null;
+ }
+
+ }
+
+ private Iterator<String> createTestIterator(List<String> list) {
+ return new IteratorImpl<String>(list);
+ }
+
+ private Iterator<String> createStringsIterator() {
+ return createTestIterator(strings);
+ }
+
+ private Iterator<String> createEmptyIterator() {
+ return createTestIterator(Collections.<String>emptyList());
+ }
+
+ @Test
+ public void testIteration() throws Exception {
+ Iterator<String> testIterator = createStringsIterator();
+ assertTrue(testIterator.hasNext());
+ assertEquals("a", testIterator.next());
+ assertTrue(testIterator.hasNext());
+ assertEquals("b", testIterator.next());
+ assertTrue(testIterator.hasNext());
+ assertEquals("c", testIterator.next());
+ assertFalse(testIterator.hasNext());
+ try {
+ testIterator.next();
+
+ fail();
+ } catch (NoSuchElementException e) {
+ //should be thrown - ignore
+ }
+ }
+
+ @Test
+ public void testIterationWithoutHasNext() throws Exception {
+ Iterator<String> testIterator = createStringsIterator();
+ assertEquals("a", testIterator.next());
+ assertEquals("b", testIterator.next());
+ assertEquals("c", testIterator.next());
+ try {
+ testIterator.next();
+
+ fail();
+ } catch (NoSuchElementException e) {
+ //should be thrown - ignore
+ }
+ assertFalse(testIterator.hasNext());
+ }
+
+ @Test
+ public void testEmptyList() throws Exception {
+ Iterator<String> testIterator = createEmptyIterator();
+ assertFalse(testIterator.hasNext());
+ try {
+ testIterator.next();
+
+ fail();
+ } catch (NoSuchElementException e) {
+ //should be thrown - ignore
+ }
+ }
+
+ @Test
+ public void testEmptyListWithoutHasNext() throws Exception {
+ Iterator<String> testIterator = createEmptyIterator();
+ try {
+ testIterator.next();
+
+ fail();
+ } catch (NoSuchElementException e) {
+ //should be thrown - ignore
+ }
+ }
+
+ @Test
+ public void testRemove() throws Exception {
+ Iterator<String> testIterator = createStringsIterator();
+ testIterator.next();
+ try {
+ testIterator.remove();
+
+ fail();
+ } catch (Exception e) {
+ //unsupported - ignore
+ }
+ }
+}