rwaldhoff 02/02/25 15:53:20
Modified: collections/src/java/org/apache/commons/collections
ProxyIterator.java
collections/src/test/org/apache/commons/collections
TestAll.java
Added: collections/src/java/org/apache/commons/collections
FilterListIterator.java ProxyListIterator.java
collections/src/test/org/apache/commons/collections
TestFilterListIterator.java
Log:
adding ProxyListIterator, FilterListIterator, and tests
removing unneeded import from ProxyIterartor, javadoc tweaks
Revision Changes Path
1.3 +114 -112
jakarta-commons/collections/src/java/org/apache/commons/collections/ProxyIterator.java
Index: ProxyIterator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ProxyIterator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ProxyIterator.java 10 Feb 2002 08:07:42 -0000 1.2
+++ ProxyIterator.java 25 Feb 2002 23:53:20 -0000 1.3
@@ -1,112 +1,114 @@
-/*
- * $Header:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ProxyIterator.java,v
1.2 2002/02/10 08:07:42 jstrachan Exp $
- * $Revision: 1.2 $
- * $Date: 2002/02/10 08:07:42 $
- *
- * ====================================================================
- *
- * The Apache Software License, Version 1.1
- *
- * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
- * reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The end-user documentation included with the redistribution, if
- * any, must include the following acknowlegement:
- * "This product includes software developed by the
- * Apache Software Foundation (http://www.apache.org/)."
- * Alternately, this acknowlegement may appear in the software itself,
- * if and wherever such third-party acknowlegements normally appear.
- *
- * 4. The names "The Jakarta Project", "Commons", and "Apache Software
- * Foundation" must not be used to endorse or promote products derived
- * from this software without prior written permission. For written
- * permission, please contact [EMAIL PROTECTED]
- *
- * 5. Products derived from this software may not be called "Apache"
- * nor may "Apache" appear in their names without prior written
- * permission of the Apache Group.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-package org.apache.commons.collections;
-
-import java.util.Enumeration;
-import java.util.Iterator;
-
-/** A Proxy {@link Iterator Iterator} which delegates its methods to a proxy
instance.
- *
- * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- */
-
-public class ProxyIterator implements Iterator {
-
- /** Holds value of property iterator. */
- private Iterator iterator;
-
-
- public ProxyIterator() {
- }
-
- public ProxyIterator( Iterator iterator ) {
- this.iterator = iterator;
- }
-
- // Iterator interface
- //-------------------------------------------------------------------------
- public boolean hasNext() {
- return getIterator().hasNext();
- }
-
- public Object next() {
- return getIterator().next();
- }
-
- public void remove() {
- getIterator().remove();
- }
-
- // Properties
- //-------------------------------------------------------------------------
- /** Getter for property iterator.
- * @return Value of property iterator.
- */
- public Iterator getIterator() {
- return iterator;
- }
- /** Setter for property iterator.
- * @param iterator New value of property iterator.
- */
- public void setIterator(Iterator iterator) {
- this.iterator = iterator;
- }
-}
+/*
+ * $Header:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ProxyIterator.java,v
1.3 2002/02/25 23:53:20 rwaldhoff Exp $
+ * $Revision: 1.3 $
+ * $Date: 2002/02/25 23:53:20 $
+ *
+ * ====================================================================
+ *
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Commons", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact [EMAIL PROTECTED]
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.commons.collections;
+
+import java.util.Iterator;
+
+/** A Proxy {@link Iterator Iterator} which delegates its methods to a proxy
instance.
+ *
+ * @see ProxyListIterator
+ * @version $Revision: 1.3 $ $Date: 2002/02/25 23:53:20 $
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
+ */
+
+public class ProxyIterator implements Iterator {
+
+ /** Holds value of property iterator. */
+ private Iterator iterator;
+
+
+ public ProxyIterator() {
+ }
+
+ public ProxyIterator( Iterator iterator ) {
+ this.iterator = iterator;
+ }
+
+ // Iterator interface
+ //-------------------------------------------------------------------------
+ public boolean hasNext() {
+ return getIterator().hasNext();
+ }
+
+ public Object next() {
+ return getIterator().next();
+ }
+
+ public void remove() {
+ getIterator().remove();
+ }
+
+ // Properties
+ //-------------------------------------------------------------------------
+ /** Getter for property iterator.
+ * @return Value of property iterator.
+ */
+ public Iterator getIterator() {
+ return iterator;
+ }
+ /** Setter for property iterator.
+ * @param iterator New value of property iterator.
+ */
+ public void setIterator(Iterator iterator) {
+ this.iterator = iterator;
+ }
+}
1.1
jakarta-commons/collections/src/java/org/apache/commons/collections/FilterListIterator.java
Index: FilterListIterator.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/FilterListIterator.java,v
1.1 2002/02/25 23:53:20 rwaldhoff Exp $
* $Revision: 1.1 $
* $Date: 2002/02/25 23:53:20 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* A proxy {@link ListIterator ListIterator} which takes a {@link Predicate
Predicate} instance to filter
* out objects from an underlying {@link Iterator Iterator} instance.
* Only objects for which the
* specified <code>Predicate</code> evaluates to <code>true</code> are
* returned.
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
* @author Jan Sorensen
*/
public class FilterListIterator extends ProxyListIterator {
//-------------------------------------------------------------------------
public FilterListIterator() {
}
public FilterListIterator(ListIterator iterator ) {
super(iterator);
}
public FilterListIterator(ListIterator iterator, Predicate predicate) {
super(iterator);
this.predicate = predicate;
}
// ListIterator interface
//-------------------------------------------------------------------------
public void add(Object o) {
throw new UnsupportedOperationException("FilterListIterator.add(Object) is
not supported.");
}
public boolean hasNext() {
if(nextObjectSet) {
return true;
} else {
return setNextObject();
}
}
public boolean hasPrevious() {
if(previousObjectSet) {
return true;
} else {
return setPreviousObject();
}
}
public Object next() {
if(!nextObjectSet) {
if(!setNextObject()) {
throw new NoSuchElementException();
}
}
nextObjectSet = false;
nextIndex++;
return nextObject;
}
public int nextIndex() {
return nextIndex;
}
public Object previous() {
if(!previousObjectSet) {
if(!setPreviousObject()) {
throw new NoSuchElementException();
}
}
previousObjectSet = false;
nextIndex--;
return previousObject;
}
public int previousIndex() {
return (nextIndex-1);
}
public void remove() {
throw new UnsupportedOperationException("FilterListIterator.remove() is not
supported.");
}
public void set(Object o) {
throw new UnsupportedOperationException("FilterListIterator.set(Object) is
not supported.");
}
// Properties
//-------------------------------------------------------------------------
/**
* Getter for the predicate property.
* @return value of the predicate property.
*/
public Predicate getPredicate() {
return predicate;
}
/**
* Setter for the predicate property.
* @param predicate new value for the predicate property.
*/
public void setPredicate(Predicate predicate) {
this.predicate = predicate;
}
/**
* Set {@link #nextObject} to the next object. If there
* are no more objects then return <code>false</code>.
* Otherwise, return <code>true</code>.
*/
private boolean setNextObject() {
ListIterator iterator = getListIterator();
Predicate predicate = getPredicate();
while(iterator.hasNext()) {
Object object = iterator.next();
if(predicate.evaluate(object)) {
nextObject = object;
nextObjectSet = true;
return true;
}
}
return false;
}
/**
* Set {@link #nextObject} to the next object. If there
* are no more objects then return <code>false</code>.
* Otherwise, return <code>true</code>.
*/
private boolean setPreviousObject() {
ListIterator iterator = getListIterator();
Predicate predicate = getPredicate();
while(iterator.hasPrevious()) {
Object object = iterator.previous();
if(predicate.evaluate(object)) {
previousObject = object;
previousObjectSet = true;
return true;
}
}
return false;
}
// Attributes
//-------------------------------------------------------------------------
/** Holds value of property "predicate". */
private Predicate predicate;
/**
* The value of the next (matching) object, when
* {@link #nextObjectSet} is true.
*/
private Object nextObject;
/**
* Whether or not the {@link #nextObject} has been set
* (possibly to <code>null</code>).
*/
private boolean nextObjectSet = false;
/**
* The value of the previous (matching) object, when
* {@link #previousObjectSet} is true.
*/
private Object previousObject;
/**
* Whether or not the {@link #previousObject} has been set
* (possibly to <code>null</code>).
*/
private boolean previousObjectSet = false;
/**
* The index of the element that would be returned by {@link #next}.
*/
private int nextIndex = 0;
}
1.1
jakarta-commons/collections/src/java/org/apache/commons/collections/ProxyListIterator.java
Index: ProxyListIterator.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ProxyListIterator.java,v
1.1 2002/02/25 23:53:20 rwaldhoff Exp $
* $Revision: 1.1 $
* $Date: 2002/02/25 23:53:20 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections;
import java.util.ListIterator;
/**
* A proxy {@link ListIterator ListIterator} which delegates its
* methods to a proxy instance.
*
* @see ProxyIterator
* @version $Revision: 1.1 $ $Date: 2002/02/25 23:53:20 $
* @author Rodney Waldhoff
*/
public class ProxyListIterator implements ListIterator {
// Constructor
//-------------------------------------------------------------------------
public ProxyListIterator() {
}
public ProxyListIterator(ListIterator iterator) {
this.iterator = iterator;
}
// ListIterator interface
//-------------------------------------------------------------------------
public void add(Object o) {
getListIterator().add(o);
}
public boolean hasNext() {
return getListIterator().hasNext();
}
public boolean hasPrevious() {
return getListIterator().hasPrevious();
}
public Object next() {
return getListIterator().next();
}
public int nextIndex() {
return getListIterator().nextIndex();
}
public Object previous() {
return getListIterator().previous();
}
public int previousIndex() {
return getListIterator().previousIndex();
}
public void remove() {
getListIterator().remove();
}
public void set(Object o) {
getListIterator().set(o);
}
// Properties
//-------------------------------------------------------------------------
/**
* Getter for property iterator.
* @return Value of property iterator.
*/
public ListIterator getListIterator() {
return iterator;
}
/**
* Setter for property iterator.
* @param iterator New value of property iterator.
*/
public void setListIterator(ListIterator iterator) {
this.iterator = iterator;
}
// Attributes
//-------------------------------------------------------------------------
/** Holds value of property "iterator". */
private ListIterator iterator;
}
1.18 +5 -4
jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java
Index: TestAll.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- TestAll.java 25 Feb 2002 23:15:29 -0000 1.17
+++ TestAll.java 25 Feb 2002 23:53:20 -0000 1.18
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v
1.17 2002/02/25 23:15:29 morgand Exp $
- * $Revision: 1.17 $
- * $Date: 2002/02/25 23:15:29 $
+ * $Header:
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v
1.18 2002/02/25 23:53:20 rwaldhoff Exp $
+ * $Revision: 1.18 $
+ * $Date: 2002/02/25 23:53:20 $
*
* ====================================================================
*
@@ -66,7 +66,7 @@
/**
* Entry point for all Collections tests.
* @author Rodney Waldhoff
- * @version $Id: TestAll.java,v 1.17 2002/02/25 23:15:29 morgand Exp $
+ * @version $Id: TestAll.java,v 1.18 2002/02/25 23:53:20 rwaldhoff Exp $
*/
public class TestAll extends TestCase {
public TestAll(String testName) {
@@ -91,6 +91,7 @@
suite.addTest(TestFastTreeMap.suite());
suite.addTest(TestFastTreeMap1.suite());
suite.addTest(TestFilterIterator.suite());
+ suite.addTest(TestFilterListIterator.suite());
suite.addTest(TestHashBag.suite());
suite.addTest(TestHashMap.suite());
suite.addTest(TestLRUMap.suite());
1.1
jakarta-commons/collections/src/test/org/apache/commons/collections/TestFilterListIterator.java
Index: TestFilterListIterator.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFilterListIterator.java,v
1.1 2002/02/25 23:53:20 rwaldhoff Exp $
* $Revision: 1.1 $
* $Date: 2002/02/25 23:53:20 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
import java.util.NoSuchElementException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
/**
* @version $Revision: 1.1 $ $Date: 2002/02/25 23:53:20 $
* @author Rodney Waldhoff
*/
public class TestFilterListIterator extends TestCase {
public TestFilterListIterator(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestFilterListIterator.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestFilterListIterator.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
private ArrayList list = null;
private ArrayList odds = null;
private ArrayList evens = null;
private ArrayList threes = null;
private ArrayList fours = null;
private ArrayList sixes = null;
private Predicate truePred = null;
private Predicate falsePred = null;
private Predicate evenPred = null;
private Predicate oddPred = null;
private Predicate threePred = null;
private Predicate fourPred = null;
private Random random = new Random();
public void setUp() {
list = new ArrayList();
odds = new ArrayList();
evens = new ArrayList();
threes = new ArrayList();
fours = new ArrayList();
sixes = new ArrayList();
for(int i=0;i<20;i++) {
list.add(new Integer(i));
if(i%2 == 0) { evens.add(new Integer(i)); }
if(i%2 == 1) { odds.add(new Integer(i)); }
if(i%4 == 0) { fours.add(new Integer(i)); }
if(i%6 == 0) { sixes.add(new Integer(i)); }
}
truePred = new Predicate() {
public boolean evaluate(Object x) {
return true;
}
};
falsePred = new Predicate() {
public boolean evaluate(Object x) {
return true;
}
};
evenPred = new Predicate() {
public boolean evaluate(Object x) {
return (((Integer)x).intValue()%2 == 0);
}
};
oddPred = new Predicate() {
public boolean evaluate(Object x) {
return (((Integer)x).intValue()%2 == 1);
}
};
threePred = new Predicate() {
public boolean evaluate(Object x) {
return (((Integer)x).intValue()%3 == 0);
}
};
fourPred = new Predicate() {
public boolean evaluate(Object x) {
return (((Integer)x).intValue()%4 == 0);
}
};
}
public void tearDown() {
list = null;
odds = null;
evens = null;
threes = null;
fours = null;
sixes = null;
truePred = null;
falsePred = null;
evenPred = null;
oddPred = null;
threePred = null;
fourPred = null;
}
public void testWalkLists() {
// this just confirms that our walkLists method works OK
walkLists(list,list.listIterator());
}
public void testManual() {
// do this one "by hand" as a sanity check
FilterListIterator filtered = new
FilterListIterator(list.listIterator(),threePred);
assertEquals(new Integer(0),filtered.next());
assertEquals(new Integer(3),filtered.next());
assertEquals(new Integer(6),filtered.next());
assertEquals(new Integer(9),filtered.next());
assertEquals(new Integer(12),filtered.next());
assertEquals(new Integer(15),filtered.next());
assertEquals(new Integer(18),filtered.next());
assertEquals(new Integer(18),filtered.previous());
assertEquals(new Integer(15),filtered.previous());
assertEquals(new Integer(12),filtered.previous());
assertEquals(new Integer(9),filtered.previous());
assertEquals(new Integer(6),filtered.previous());
assertEquals(new Integer(3),filtered.previous());
assertEquals(new Integer(0),filtered.previous());
assertTrue(!filtered.hasPrevious());
assertEquals(new Integer(0),filtered.next());
assertEquals(new Integer(3),filtered.next());
assertEquals(new Integer(6),filtered.next());
assertEquals(new Integer(9),filtered.next());
assertEquals(new Integer(12),filtered.next());
assertEquals(new Integer(15),filtered.next());
assertEquals(new Integer(18),filtered.next());
assertTrue(!filtered.hasNext());
assertEquals(new Integer(18),filtered.previous());
assertEquals(new Integer(15),filtered.previous());
assertEquals(new Integer(12),filtered.previous());
assertEquals(new Integer(9),filtered.previous());
assertEquals(new Integer(6),filtered.previous());
assertEquals(new Integer(3),filtered.previous());
assertEquals(new Integer(0),filtered.previous());
assertEquals(new Integer(0),filtered.next());
assertEquals(new Integer(0),filtered.previous());
assertEquals(new Integer(0),filtered.next());
assertEquals(new Integer(3),filtered.next());
assertEquals(new Integer(6),filtered.next());
assertEquals(new Integer(6),filtered.previous());
assertEquals(new Integer(3),filtered.previous());
assertEquals(new Integer(3),filtered.next());
assertEquals(new Integer(6),filtered.next());
assertEquals(new Integer(9),filtered.next());
assertEquals(new Integer(12),filtered.next());
assertEquals(new Integer(15),filtered.next());
assertEquals(new Integer(15),filtered.previous());
assertEquals(new Integer(12),filtered.previous());
assertEquals(new Integer(9),filtered.previous());
}
public void testTruePredicate() {
FilterListIterator filtered = new
FilterListIterator(list.listIterator(),truePred);
walkLists(list,filtered);
}
public void testFalsePredicate() {
FilterListIterator filtered = new
FilterListIterator(list.listIterator(),falsePred);
walkLists(new ArrayList(),filtered);
}
public void testEvens() {
FilterListIterator filtered = new
FilterListIterator(list.listIterator(),evenPred);
walkLists(evens,filtered);
}
public void testOdds() {
FilterListIterator filtered = new
FilterListIterator(list.listIterator(),oddPred);
walkLists(odds,filtered);
}
public void testThrees() {
FilterListIterator filtered = new
FilterListIterator(list.listIterator(),threePred);
walkLists(threes,filtered);
}
public void testFours() {
FilterListIterator filtered = new
FilterListIterator(list.listIterator(),fourPred);
walkLists(fours,filtered);
}
public void testNestedSixes() {
FilterListIterator filtered = new FilterListIterator(
new
FilterListIterator(list.listIterator(),threePred),
evenPred
);
walkLists(sixes,filtered);
}
public void testNestedSixes2() {
FilterListIterator filtered = new FilterListIterator(
new
FilterListIterator(list.listIterator(),evenPred),
threePred
);
walkLists(sixes,filtered);
}
// Utilities
private void walkLists(List list, ListIterator testing) {
ListIterator expected = list.listIterator();
// walk all the way forward
while(expected.hasNext()) {
assertEquals(expected.nextIndex(),testing.nextIndex());
assertEquals(expected.previousIndex(),testing.previousIndex());
assertTrue("a",testing.hasNext());
assertEquals("b",expected.next(),testing.next());
}
// walk all the way back
while(expected.hasPrevious()) {
assertEquals(expected.nextIndex(),testing.nextIndex());
assertEquals(expected.previousIndex(),testing.previousIndex());
assertTrue("c",testing.hasPrevious());
assertEquals("d",expected.previous(),testing.previous());
}
// forward,back,foward
while(expected.hasNext()) {
assertEquals(expected.nextIndex(),testing.nextIndex());
assertEquals(expected.previousIndex(),testing.previousIndex());
assertTrue(testing.hasNext());
assertEquals(expected.next(),testing.next());
assertTrue(testing.hasPrevious());
assertEquals(expected.previous(),testing.previous());
assertTrue(testing.hasNext());
assertEquals(expected.next(),testing.next());
}
// walk all the way back
while(expected.hasPrevious()) {
assertEquals(expected.nextIndex(),testing.nextIndex());
assertEquals(expected.previousIndex(),testing.previousIndex());
assertTrue(testing.hasPrevious());
assertEquals(expected.previous(),testing.previous());
}
for(int i=0;i<list.size();i++) {
// walk forward i
for(int j=0;j<i;j++) {
assertEquals(expected.nextIndex(),testing.nextIndex());
assertEquals(expected.previousIndex(),testing.previousIndex());
assertTrue(expected.hasNext()); // if this one fails we've got a
logic error in the test
assertTrue(testing.hasNext());
assertEquals(expected.next(),testing.next());
}
// walk back i/2
for(int j=0;j<i/2;j++) {
assertEquals(expected.nextIndex(),testing.nextIndex());
assertEquals(expected.previousIndex(),testing.previousIndex());
assertTrue(expected.hasPrevious()); // if this one fails we've got a
logic error in the test
assertTrue(testing.hasPrevious());
assertEquals(expected.previous(),testing.previous());
}
// walk foward i/2
for(int j=0;j<i/2;j++) {
assertEquals(expected.nextIndex(),testing.nextIndex());
assertEquals(expected.previousIndex(),testing.previousIndex());
assertTrue(expected.hasNext()); // if this one fails we've got a
logic error in the test
assertTrue(testing.hasNext());
assertEquals(expected.next(),testing.next());
}
// walk back i
for(int j=0;j<i;j++) {
assertEquals(expected.nextIndex(),testing.nextIndex());
assertEquals(expected.previousIndex(),testing.previousIndex());
assertTrue(expected.hasPrevious()); // if this one fails we've got a
logic error in the test
assertTrue(testing.hasPrevious());
assertEquals(expected.previous(),testing.previous());
}
}
// random walk
StringBuffer walkdescr = new StringBuffer(500);
for(int i=0;i<500;i++) {
if(random.nextBoolean()) {
// step foward
walkdescr.append("+");
if(expected.hasNext()) {
assertEquals(walkdescr.toString(),expected.next(),testing.next());
}
} else {
// step backward
walkdescr.append("-");
if(expected.hasPrevious()) {
assertEquals(walkdescr.toString(),expected.previous(),testing.previous());
}
}
assertEquals(walkdescr.toString(),expected.nextIndex(),testing.nextIndex());
assertEquals(walkdescr.toString(),expected.previousIndex(),testing.previousIndex());
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>