rwaldhoff 2003/02/19 04:34:20
Modified: functor/src/java/org/apache/commons/functor/core/collection
CollectionAlgorithms.java
functor/src/test/org/apache/commons/functor/core/collection
TestCollectionAlgorithms.java
Log:
add transform, remove, retain, and tests
Revision Changes Path
1.3 +73 -27
jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/CollectionAlgorithms.java
Index: CollectionAlgorithms.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/CollectionAlgorithms.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- CollectionAlgorithms.java 19 Feb 2003 12:09:04 -0000 1.2
+++ CollectionAlgorithms.java 19 Feb 2003 12:34:20 -0000 1.3
@@ -59,6 +59,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
+import java.util.ListIterator;
import java.util.NoSuchElementException;
import org.apache.commons.functor.BinaryFunction;
@@ -95,7 +96,7 @@
* and {@link Collection#add add} the result to a
* new {@link Collection}.
*
- * @see
#collect(java.util.Iterator,org.apache.commons.functor.UnaryFunction,java.util.Collection)
+ * @see #collect(Iterator,UnaryFunction,Collection)
*/
public static Collection collect(Iterator iter, UnaryFunction func) {
return collect(iter,func,new ArrayList());
@@ -109,7 +110,7 @@
* given {@link Collection}.
*
* @return the given {@link Collection}
- * @see #collect(java.util.Iterator,org.apache.commons.functor.UnaryFunction)
+ * @see #collect(Iterator,UnaryFunction)
*/
public static Collection collect(Iterator iter, UnaryFunction func, Collection
col) {
while(iter.hasNext()) {
@@ -124,7 +125,7 @@
* {@link Iterator Iterator} that matches the given
* {@link UnaryPredicate UnaryPredicate}.
*
- * @see #detect(java.util.Iterator,org.apache.commons.functor.UnaryPredicate)
+ * @see #detect(Iterator,UnaryPredicate)
*/
public static boolean contains(Iterator iter, UnaryPredicate pred) {
while(iter.hasNext()) {
@@ -142,7 +143,7 @@
* {@link NoSuchElementException NoSuchElementException} if no
* matching element can be found.
*
- * @see
#detect(java.util.Iterator,org.apache.commons.functor.UnaryPredicate,java.lang.Object)
+ * @see #detect(Iterator,UnaryPredicate,Object)
*/
public static Object detect(Iterator iter, UnaryPredicate pred) {
while(iter.hasNext()) {
@@ -161,7 +162,7 @@
* the given (possibly <code>null</code> <code>Object</code>
* if no matching element can be found.
*
- * @see #detect(java.util.Iterator,org.apache.commons.functor.UnaryPredicate)
+ * @see #detect(Iterator,UnaryPredicate)
*/
public static Object detect(Iterator iter, UnaryPredicate pred, Object ifNone) {
while(iter.hasNext()) {
@@ -208,18 +209,18 @@
/**
* {@link Collection#add Add} all elements within the
- * given {@link Iterator Iterator} that match the
+ * given {@link Iterator Iterator} that fail to match the
* given {@link UnaryPredicate UnaryPredicate} to the
* given {@link Collection Collection}.
*
* @return the given {@link Collection Collection}
- * @see #select(java.util.Iterator,org.apache.commons.functor.UnaryPredicate)
- * @see
#reject(java.util.Iterator,org.apache.commons.functor.UnaryPredicate,java.util.Collection)
+ * @see #reject(Iterator,UnaryPredicate)
+ * @see #select(Iterator,UnaryPredicate,Collection)
*/
- public static Collection select(Iterator iter, UnaryPredicate pred, Collection
col) {
+ public static Collection reject(Iterator iter, UnaryPredicate pred, Collection
col) {
while(iter.hasNext()) {
Object obj = iter.next();
- if(pred.test(obj)) {
+ if(!pred.test(obj)) {
col.add(obj);
}
}
@@ -228,32 +229,64 @@
/**
* {@link Collection#add Add} all elements within the
- * given {@link Iterator Iterator} that match the
+ * given {@link Iterator Iterator} that fail to match the
* given {@link UnaryPredicate UnaryPredicate} to a
* new {@link Collection Collection}.
*
* @return the new {@link Collection Collection}
- * @see
#select(java.util.Iterator,org.apache.commons.functor.UnaryPredicate,java.util.Collection)
- * @see #reject(java.util.Iterator,org.apache.commons.functor.UnaryPredicate)
+ * @see #reject(Iterator,UnaryPredicate,Collection)
+ * @see #select(Iterator,UnaryPredicate)
*/
- public static Collection select(Iterator iter, UnaryPredicate pred) {
- return select(iter,pred,new ArrayList());
+ public static Collection reject(Iterator iter, UnaryPredicate pred) {
+ return reject(iter,pred,new ArrayList());
+ }
+
+ /**
+ * {@link Iterator#remove Renmove} from the
+ * given {@link Iterator Iterator} all elements
+ * that match the
+ * given {@link UnaryPredicate UnaryPredicate}.
+ *
+ * @see #retain(Iterator,UnaryPredicate)
+ */
+ public static void remove(Iterator iter, UnaryPredicate pred) {
+ while(iter.hasNext()) {
+ if(pred.test(iter.next())) {
+ iter.remove();
+ }
+ }
+ }
+
+ /**
+ * {@link Iterator#remove Renmove} from the
+ * given {@link Iterator Iterator} all elements
+ * that fail to match the
+ * given {@link UnaryPredicate UnaryPredicate}.
+ *
+ * @see #remove(Iterator,UnaryPredicate)
+ */
+ public static void retain(Iterator iter, UnaryPredicate pred) {
+ while(iter.hasNext()) {
+ if(!(pred.test(iter.next()))) {
+ iter.remove();
+ }
+ }
}
/**
* {@link Collection#add Add} all elements within the
- * given {@link Iterator Iterator} that fail to match the
+ * given {@link Iterator Iterator} that match the
* given {@link UnaryPredicate UnaryPredicate} to the
* given {@link Collection Collection}.
*
* @return the given {@link Collection Collection}
- * @see #reject(java.util.Iterator,org.apache.commons.functor.UnaryPredicate)
- * @see
#select(java.util.Iterator,org.apache.commons.functor.UnaryPredicate,java.util.Collection)
+ * @see #select(Iterator,UnaryPredicate)
+ * @see #reject(Iterator,UnaryPredicate,Collection)
*/
- public static Collection reject(Iterator iter, UnaryPredicate pred, Collection
col) {
+ public static Collection select(Iterator iter, UnaryPredicate pred, Collection
col) {
while(iter.hasNext()) {
Object obj = iter.next();
- if(!pred.test(obj)) {
+ if(pred.test(obj)) {
col.add(obj);
}
}
@@ -262,15 +295,28 @@
/**
* {@link Collection#add Add} all elements within the
- * given {@link Iterator Iterator} that fail to match the
+ * given {@link Iterator Iterator} that match the
* given {@link UnaryPredicate UnaryPredicate} to a
* new {@link Collection Collection}.
*
* @return the new {@link Collection Collection}
- * @see
#reject(java.util.Iterator,org.apache.commons.functor.UnaryPredicate,java.util.Collection)
- * @see #select(java.util.Iterator,org.apache.commons.functor.UnaryPredicate)
+ * @see #select(Iterator,UnaryPredicate,Collection)
+ * @see #reject(Iterator,UnaryPredicate)
*/
- public static Collection reject(Iterator iter, UnaryPredicate pred) {
- return reject(iter,pred,new ArrayList());
+ public static Collection select(Iterator iter, UnaryPredicate pred) {
+ return select(iter,pred,new ArrayList());
+ }
+
+ /**
+ * {@link ListIterator#set Set} each element of the
+ * given {@link ListIterator ListIterator} to
+ * the result of applying the
+ * given {@link UnaryFunction UnaryFunction} to
+ * its original value.
+ */
+ public static void transform(ListIterator iter, UnaryFunction func) {
+ while(iter.hasNext()) {
+ iter.set(func.evaluate(iter.next()));
+ }
}
}
1.3 +28 -2
jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestCollectionAlgorithms.java
Index: TestCollectionAlgorithms.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestCollectionAlgorithms.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestCollectionAlgorithms.java 19 Feb 2003 12:09:04 -0000 1.2
+++ TestCollectionAlgorithms.java 19 Feb 2003 12:34:20 -0000 1.3
@@ -69,6 +69,7 @@
import junit.framework.TestSuite;
import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.UnaryFunction;
import org.apache.commons.functor.UnaryPredicate;
import org.apache.commons.functor.UnaryProcedure;
import org.apache.commons.functor.adapter.LeftBoundPredicate;
@@ -99,10 +100,12 @@
super.setUp();
list = new ArrayList();
evens = new ArrayList();
+ doubled = new ArrayList();
listWithDuplicates = new ArrayList();
sum = 0;
for(int i=0;i<10;i++) {
list.add(new Integer(i));
+ doubled.add(new Integer(i*2));
listWithDuplicates.add(new Integer(i));
listWithDuplicates.add(new Integer(i));
sum += i;
@@ -215,9 +218,32 @@
assertEquals(new Integer(sum),result);
}
+ public void testRetain() {
+ CollectionAlgorithms.retain(list.iterator(),isEven);
+ assertEquals(evens,list);
+ }
+
+ public void testRemove() {
+ CollectionAlgorithms.remove(list.iterator(),isOdd);
+ assertEquals(evens,list);
+ }
+
+ public void testTransform() {
+ CollectionAlgorithms.transform(
+ list.listIterator(),
+ new UnaryFunction() {
+ public Object evaluate(Object obj) {
+ return new Integer(((Number)obj).intValue()*2);
+ }
+ }
+ );
+ assertEquals(doubled,list);
+ }
+
// Attributes
// ------------------------------------------------------------------------
private List list = null;
+ private List doubled = null;
private List evens = null;
private List listWithDuplicates = null;
private int sum = 0;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]