Author: mbenson
Date: Wed May 28 13:13:39 2008
New Revision: 661067
URL: http://svn.apache.org/viewvc?rev=661067&view=rev
Log:
merge rev 656305 from branch; remove algorithms from Generator interface;
remove Algorithms
Removed:
commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/Algorithms.java
Modified:
commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java
commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/TestAlgorithms.java
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/QuicksortExample.java
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/four/DataMunger.java
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/two/TestBinaryChop.java
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/lines/TestLines.java
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/map/FixedSizeMap.java
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/util/TestEachElement.java
Modified:
commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java
(original)
+++
commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java
Wed May 28 13:13:39 2008
@@ -16,10 +16,7 @@
import java.util.Collection;
-import org.apache.commons.functor.Algorithms;
-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.generator.util.CollectionTransformer;
@@ -86,86 +83,6 @@
/**
* [EMAIL PROTECTED]
- * See [EMAIL PROTECTED] Algorithms#apply}.
- */
- public final Generator apply(UnaryFunction func) {
- return Algorithms.apply(this, func);
- }
-
- /**
- * [EMAIL PROTECTED]
- * See [EMAIL PROTECTED] Algorithms#contains}.
- */
- public final boolean contains(UnaryPredicate pred) {
- return Algorithms.contains(this, pred);
- }
-
- /**
- * [EMAIL PROTECTED]
- * See [EMAIL PROTECTED] Algorithms#detect}.
- */
- public final Object detect(UnaryPredicate pred) {
- return Algorithms.detect(this, pred);
- }
-
- /**
- * [EMAIL PROTECTED]
- * See [EMAIL PROTECTED] Algorithms#detect}.
- */
- public final Object detect(UnaryPredicate pred, Object ifNone) {
- return Algorithms.detect(this, pred, ifNone);
- }
-
- /**
- * [EMAIL PROTECTED]
- * Synonym for run.
- */
- public final void foreach(UnaryProcedure proc) {
- Algorithms.foreach(this, proc);
- }
-
- /**
- * [EMAIL PROTECTED]
- * See [EMAIL PROTECTED] Algorithms#inject}.
- */
- public final Object inject(Object seed, BinaryFunction func) {
- return Algorithms.inject(this, seed, func);
- }
-
- /**
- * [EMAIL PROTECTED]
- * See [EMAIL PROTECTED] Algorithms#reject}.
- */
- public final Generator reject(UnaryPredicate pred) {
- return Algorithms.reject(this, pred);
- }
-
- /**
- * [EMAIL PROTECTED]
- * See [EMAIL PROTECTED] Algorithms#select}.
- */
- public final Generator select(UnaryPredicate pred) {
- return Algorithms.select(this, pred);
- }
-
- /**
- * [EMAIL PROTECTED]
- * See [EMAIL PROTECTED] Algorithms#select}.
- */
- public final Generator where(UnaryPredicate pred) {
- return Algorithms.select(this, pred);
- }
-
- /**
- * [EMAIL PROTECTED]
- * See [EMAIL PROTECTED] Algorithms#until}.
- */
- public final Generator until(UnaryPredicate pred) {
- return Algorithms.until(this, pred);
- }
-
- /**
- * [EMAIL PROTECTED]
* Transforms this generator using the passed in
* UnaryFunction. An example function might turn the contents of the
* generator into a [EMAIL PROTECTED] Collection} of elements.
Modified:
commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java
(original)
+++
commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java
Wed May 28 13:13:39 2008
@@ -15,9 +15,7 @@
import java.util.Collection;
-import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.UnaryFunction;
-import org.apache.commons.functor.UnaryPredicate;
import org.apache.commons.functor.UnaryProcedure;
/**
@@ -47,77 +45,6 @@
public abstract boolean isStopped();
/**
- * See [EMAIL PROTECTED] org.apache.commons.functor.Algorithms#apply}.
- * @param func UnaryFunction to apply
- * @return this
- */
- public abstract Generator apply(UnaryFunction func);
-
- /**
- * See [EMAIL PROTECTED] org.apache.commons.functor.Algorithms#contains}.
- * @param pred UnaryPredicate to apply
- * @return true if a match was found
- */
- public abstract boolean contains(UnaryPredicate pred);
-
- /**
- * See [EMAIL PROTECTED] org.apache.commons.functor.Algorithms#detect}.
- * @param pred UnaryPredicate to apply
- * @return first match or <code>null</code>
- */
- public abstract Object detect(UnaryPredicate pred);
-
- /**
- * See [EMAIL PROTECTED] org.apache.commons.functor.Algorithms#detect}.
- * @param pred UnaryPredicate to apply
- * @param ifNone default result
- * @return first match or <code>null</code>
- */
- public abstract Object detect(UnaryPredicate pred, Object ifNone);
-
- /**
- * Synonym for run.
- * @param proc UnaryProcedure to run against each element
- */
- public abstract void foreach(UnaryProcedure proc);
-
- /**
- * See [EMAIL PROTECTED] org.apache.commons.functor.Algorithms#inject}.
- * @param seed seed Object
- * @param func BinaryFunction to apply
- * @return final result
- */
- public abstract Object inject(Object seed, BinaryFunction func);
-
- /**
- * See [EMAIL PROTECTED] org.apache.commons.functor.Algorithms#reject}.
- * @param pred UnaryPredicate to apply
- * @return a Generator of non-matching elements
- */
- public abstract Generator reject(UnaryPredicate pred);
-
- /**
- * See [EMAIL PROTECTED] org.apache.commons.functor.Algorithms#select}.
- * @param pred UnaryPredicate to apply
- * @return Generator of matching elements
- */
- public abstract Generator select(UnaryPredicate pred);
-
- /**
- * See [EMAIL PROTECTED] org.apache.commons.functor.Algorithms#select}.
- * @param pred UnaryPredicate to apply
- * @return Generator of matching elements
- */
- public abstract Generator where(UnaryPredicate pred);
-
- /**
- * See [EMAIL PROTECTED] org.apache.commons.functor.Algorithms#until}.
- * @param pred UnaryPredicate to apply
- * @return a Generator of non-matching elements
- */
- public abstract Generator until(UnaryPredicate pred);
-
- /**
* Transforms this generator using the passed in
* transformer. An example transformer might turn the contents of the
* generator into a [EMAIL PROTECTED] Collection} of elements.
Modified:
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/TestAlgorithms.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/TestAlgorithms.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/TestAlgorithms.java
(original)
+++
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/TestAlgorithms.java
Wed May 28 13:13:39 2008
@@ -33,8 +33,23 @@
import org.apache.commons.functor.core.IsEqual;
import org.apache.commons.functor.core.Limit;
import org.apache.commons.functor.core.Offset;
+import org.apache.commons.functor.core.algorithm.DoUntil;
+import org.apache.commons.functor.core.algorithm.DoWhile;
+import org.apache.commons.functor.core.algorithm.FindWithinGenerator;
+import org.apache.commons.functor.core.algorithm.FoldLeft;
+import org.apache.commons.functor.core.algorithm.FoldRight;
+import org.apache.commons.functor.core.algorithm.GeneratorContains;
+import org.apache.commons.functor.core.algorithm.InPlaceTransform;
+import org.apache.commons.functor.core.algorithm.RecursiveEvaluation;
+import org.apache.commons.functor.core.algorithm.RemoveMatching;
+import org.apache.commons.functor.core.algorithm.RetainMatching;
+import org.apache.commons.functor.core.algorithm.UntilDo;
+import org.apache.commons.functor.core.algorithm.WhileDo;
+import org.apache.commons.functor.core.composite.UnaryNot;
+import org.apache.commons.functor.generator.FilteredGenerator;
import org.apache.commons.functor.generator.Generator;
import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
+import org.apache.commons.functor.generator.TransformedGenerator;
import org.apache.commons.functor.generator.util.IntegerRange;
/**
@@ -87,35 +102,10 @@
// Tests
// ------------------------------------------------------------------------
- public void testCollect() {
- Collection result = Algorithms.collect(list.iterator());
- assertNotNull(result);
- assertEquals(list.size(),result.size());
- assertEquals(list,result);
- }
-
- public void testCollect2() {
- Set set = new HashSet();
- assertSame(set,Algorithms.collect(list.iterator(),set));
- assertEquals(list.size(),set.size());
- for (Iterator iter = list.iterator(); iter.hasNext(); ) {
- assertTrue(set.contains(iter.next()));
- }
- }
-
- public void testCollect3() {
- Set set = new HashSet();
- assertSame(set,Algorithms.collect(listWithDuplicates.iterator(),set));
- assertTrue(listWithDuplicates.size() > set.size());
- for (Iterator iter = listWithDuplicates.iterator(); iter.hasNext(); ) {
- assertTrue(set.contains(iter.next()));
- }
- }
-
public void testDetect() {
- assertEquals(new
Integer(3),Algorithms.detect(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsThree));
+ assertEquals(new
Integer(3),FindWithinGenerator.instance().evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsThree));
try {
-
Algorithms.detect(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsTwentyThree);
+
FindWithinGenerator.instance().evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsTwentyThree);
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
@@ -123,52 +113,52 @@
}
public void testDetectIfNone() {
- assertEquals(new
Integer(3),Algorithms.detect(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsThree,"Xyzzy"));
-
assertEquals("Xyzzy",Algorithms.detect(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsTwentyThree,"Xyzzy"));
+ assertEquals(new Integer(3),new
FindWithinGenerator("Xyzzy").evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsThree));
+ assertEquals("Xyzzy",new
FindWithinGenerator("Xyzzy").evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsTwentyThree));
}
- public void testForEach() {
+ public void testRun() {
Summer summer = new Summer();
-
Algorithms.foreach(IteratorToGeneratorAdapter.adapt(list.iterator()),summer);
+ IteratorToGeneratorAdapter.adapt(list.iterator()).run(summer);
assertEquals(sum,summer.sum);
}
public void testSelect1() {
- Collection result =
Algorithms.select(IteratorToGeneratorAdapter.adapt(list.iterator()),isEven).toCollection();
+ Collection result = new
FilteredGenerator(IteratorToGeneratorAdapter.adapt(list.iterator()),isEven).toCollection();
assertNotNull(result);
assertEquals(evens,result);
}
public void testSelect2() {
ArrayList result = new ArrayList();
-
assertSame(result,Algorithms.select(IteratorToGeneratorAdapter.adapt(list.iterator()),isEven).to(result));
+ assertSame(result,new
FilteredGenerator(IteratorToGeneratorAdapter.adapt(list.iterator()),isEven).to(result));
assertEquals(evens,result);
}
public void testReject1() {
- Collection result =
Algorithms.reject(IteratorToGeneratorAdapter.adapt(list.iterator()),isOdd).toCollection();
+ Collection result = new
FilteredGenerator(IteratorToGeneratorAdapter.adapt(list.iterator()),new
UnaryNot(isOdd)).toCollection();
assertNotNull(result);
assertEquals(evens,result);
}
public void testReject2() {
ArrayList result = new ArrayList();
-
assertSame(result,Algorithms.reject(IteratorToGeneratorAdapter.adapt(list.iterator()),isOdd).to(result));
+ assertSame(result,new
FilteredGenerator(IteratorToGeneratorAdapter.adapt(list.iterator()),new
UnaryNot(isOdd)).to(result));
assertEquals(evens,result);
}
public void testRetain() {
- Algorithms.retain(list.iterator(),isEven);
+ RetainMatching.instance().run(list.iterator(),isEven);
assertEquals(evens,list);
}
public void testRemove() {
- Algorithms.remove(list.iterator(),isOdd);
+ RemoveMatching.instance().run(list.iterator(),isOdd);
assertEquals(evens,list);
}
public void testTransform() {
- Algorithms.transform(
+ InPlaceTransform.instance().run(
list.listIterator(),
new UnaryFunction() {
public Object evaluate(Object obj) {
@@ -179,22 +169,17 @@
assertEquals(doubled,list);
}
- public void testHasPublicConstructor() {
- // some frameworks work best with instantiable classes
- assertNotNull(new Algorithms());
- }
-
public void testApplyToGenerator() {
Generator gen = new IntegerRange(1,5);
Summer summer = new Summer();
- Algorithms.apply(gen,new Doubler()).run(summer);
+ new TransformedGenerator(gen, new Doubler()).run(summer);
assertEquals(2*(1+2+3+4),summer.sum);
}
public void testApply() {
- Collection result =
Algorithms.apply(IteratorToGeneratorAdapter.adapt(list.iterator()), new
Doubler())
+ Collection result = new
TransformedGenerator(IteratorToGeneratorAdapter.adapt(list.iterator()), new
Doubler())
.toCollection();
assertNotNull(result);
assertEquals(doubled,result);
@@ -202,7 +187,7 @@
public void testApply2() {
Set set = new HashSet();
- assertSame(set,
Algorithms.apply(IteratorToGeneratorAdapter.adapt(list.iterator()),
Identity.instance())
+ assertSame(set, new
TransformedGenerator(IteratorToGeneratorAdapter.adapt(list.iterator()),
Identity.instance())
.to(set));
assertEquals(list.size(),set.size());
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
@@ -212,7 +197,7 @@
public void testApply3() {
Set set = new HashSet();
- assertSame(set,
Algorithms.apply(IteratorToGeneratorAdapter.adapt(listWithDuplicates.iterator()),
+ assertSame(set, new
TransformedGenerator(IteratorToGeneratorAdapter.adapt(listWithDuplicates.iterator()),
Identity.instance()).to(set));
assertTrue(listWithDuplicates.size() > set.size());
for (Iterator iter = listWithDuplicates.iterator(); iter.hasNext(); ) {
@@ -221,31 +206,35 @@
}
public void testContains() {
-
assertTrue(Algorithms.contains(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsThree));
-
assertTrue(!Algorithms.contains(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsTwentyThree));
+
assertTrue(GeneratorContains.instance().test(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsThree));
+
assertFalse(GeneratorContains.instance().test(IteratorToGeneratorAdapter.adapt(list.iterator()),equalsTwentyThree));
}
- public void testInject() {
- Object result = Algorithms.inject(IteratorToGeneratorAdapter.adapt(
- list.iterator()),
- new Integer(0),
- new BinaryFunction() {
- public Object evaluate(Object a, Object b) {
- return new Integer(((Number) a).intValue() + ((Number)
b).intValue());
- }
- });
- assertEquals(new Integer(sum),result);
+ public void testFoldLeft() {
+ FoldLeft foldLeft = new FoldLeft(new BinaryFunction() {
+ public Object evaluate(Object a, Object b) {
+ return new Integer(((Number) a).intValue() + ((Number)
b).intValue());
+ }
+ });
+ assertEquals(new Integer(sum),
foldLeft.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator())));
+ assertEquals(new Integer(sum),
foldLeft.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()), new
Integer(0)));
}
- public void testLimit() {
- Collection col =
Algorithms.until(IteratorToGeneratorAdapter.adapt(list.iterator()), new
Offset(2)).toCollection();
- assertEquals("[0, 1]", col.toString());
+ public void testFoldRight() {
+ FoldRight foldRight = new FoldRight(new BinaryFunction() {
+ public Object evaluate(Object left, Object right) {
+ StringBuffer buf = left instanceof StringBuffer ?
(StringBuffer) left : new StringBuffer().append(left);
+ return buf.append(right);
+ }
+ });
+ assertEquals("0123456789",
foldRight.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator())).toString());
+ assertEquals("0123456789x",
foldRight.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()),
"x").toString());
}
public void testDoUntil() {
for (int i=0;i<3;i++){
Counter counter = new Counter();
- Algorithms.dountil(counter,new Offset(i));
+ new DoUntil(counter, new Offset(i)).run();
assertEquals(i+1,counter.count);
}
}
@@ -253,7 +242,7 @@
public void testDoWhile() {
for (int i=0;i<3;i++){
Counter counter = new Counter();
- Algorithms.dowhile(counter,new Limit(i));
+ new DoWhile(counter, new Limit(i)).run();
assertEquals(i+1,counter.count);
}
}
@@ -261,7 +250,7 @@
public void testUntilDo() {
for (int i=0;i<3;i++){
Counter counter = new Counter();
- Algorithms.untildo(new Offset(i),counter);
+ new UntilDo(new Offset(i), counter).run();
assertEquals(i,counter.count);
}
}
@@ -269,16 +258,17 @@
public void testWhileDo() {
for (int i=0;i<3;i++){
Counter counter = new Counter();
- Algorithms.whiledo(new Limit(i),counter);
+ new WhileDo(new Limit(i),counter).run();
assertEquals(i,counter.count);
}
}
+
public void testRecurse() {
- assertEquals(new Integer(5), Algorithms.recurse(new RecFunc(0,
false)));
+ assertEquals(new Integer(5), new RecursiveEvaluation(new RecFunc(0,
false)).evaluate());
// this version will return a function. since it is not the same type
// as RecFunc recursion will end.
- Function func = (Function) Algorithms.recurse(new RecFunc(0, true));
+ Function func = (Function) new RecursiveEvaluation(new RecFunc(0,
true)).evaluate();
assertEquals(new Integer(5), func.evaluate());
}
Modified:
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/QuicksortExample.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/QuicksortExample.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/QuicksortExample.java
(original)
+++
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/QuicksortExample.java
Wed May 28 13:13:39 2008
@@ -25,7 +25,6 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.apache.commons.functor.Algorithms;
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.UnaryFunction;
import org.apache.commons.functor.core.Constant;
@@ -33,6 +32,7 @@
import org.apache.commons.functor.core.comparator.IsGreaterThanOrEqual;
import org.apache.commons.functor.core.comparator.IsLessThan;
import org.apache.commons.functor.core.composite.ConditionalUnaryFunction;
+import org.apache.commons.functor.generator.FilteredGenerator;
import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
/*
@@ -462,7 +462,7 @@
*/
private BinaryFunction lesserTail = new ObjectListFunction() {
public Object evaluate(Object head, List tail) {
- return Algorithms.select(
+ return new FilteredGenerator(
IteratorToGeneratorAdapter.adapt(tail.iterator()),
IsLessThan.instance((Comparable) head)).toCollection();
}
@@ -475,7 +475,7 @@
*/
private BinaryFunction greaterTail = new ObjectListFunction() {
public Object evaluate(Object head, List tail) {
- return Algorithms.select(
+ return new FilteredGenerator(
IteratorToGeneratorAdapter.adapt(tail.iterator()),
IsGreaterThanOrEqual.instance((Comparable)
head)).toCollection();
}
Modified:
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/four/DataMunger.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/four/DataMunger.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/four/DataMunger.java
(original)
+++
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/four/DataMunger.java
Wed May 28 13:13:39 2008
@@ -20,12 +20,12 @@
import java.io.InputStreamReader;
import java.io.Reader;
-import org.apache.commons.functor.Algorithms;
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.UnaryFunction;
import org.apache.commons.functor.core.IsNull;
import org.apache.commons.functor.core.LeftIdentity;
import org.apache.commons.functor.core.RightIdentity;
+import org.apache.commons.functor.core.algorithm.FoldLeft;
import org.apache.commons.functor.core.comparator.IsLessThan;
import org.apache.commons.functor.core.composite.Composite;
import org.apache.commons.functor.core.composite.Conditional;
@@ -33,6 +33,7 @@
import org.apache.commons.functor.example.kata.one.BinaryFunctionUnaryFunction;
import org.apache.commons.functor.example.kata.one.Subtract;
import org.apache.commons.functor.example.lines.Lines;
+import org.apache.commons.functor.generator.FilteredGenerator;
/**
* The real workhorse of this Kata excercise.
@@ -55,16 +56,12 @@
*/
public static final Object process(final Reader file, final int selected,
final int col1, final int col2) {
return NthColumn.instance(selected).evaluate(
- Algorithms.inject(
- Lines.from(file).where(
-
Composite.predicate(IsInteger.instance(),NthColumn.instance(0))),
- null,
- lesserSpread(col1,col2)));
+ new FoldLeft(lesserSpread(col1, col2)).evaluate(new
FilteredGenerator(Lines.from(file),
+
Composite.predicate(IsInteger.instance(),NthColumn.instance(0)))));
}
-
/**
- * A BinaryFunction that will calcuate the absolute
+ * A BinaryFunction that will calculate the absolute
* difference between col1 and col2 in the given
* String arguments, and return the argument
* whose difference is smallest.
Modified:
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/two/TestBinaryChop.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/two/TestBinaryChop.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/two/TestBinaryChop.java
(original)
+++
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/two/TestBinaryChop.java
Wed May 28 13:13:39 2008
@@ -23,10 +23,11 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.apache.commons.functor.Algorithms;
import org.apache.commons.functor.Function;
import org.apache.commons.functor.Predicate;
import org.apache.commons.functor.Procedure;
+import org.apache.commons.functor.core.algorithm.RecursiveEvaluation;
+import org.apache.commons.functor.core.algorithm.UntilDo;
import org.apache.commons.functor.generator.util.IntegerRange;
/**
@@ -311,7 +312,7 @@
}
};
- Algorithms.untildo(loop,loop);
+ new UntilDo(loop, loop).run();
return ((Number) loop.evaluate()).intValue();
}
});
@@ -443,7 +444,7 @@
public void testTailRecursive() {
chopTest(new BaseBinaryChop() {
public int find(final Object seeking, final List list) {
- return ((Number) Algorithms.recurse(new Function() {
+ return ((Number) new RecursiveEvaluation(new Function() {
public Object evaluate() {
if (high - low > 1) {
int mid = (high + low) / 2;
@@ -463,7 +464,7 @@
}
int high = list.size();
int low = 0;
- })).intValue();
+ }).evaluate()).intValue();
}
});
}
@@ -517,7 +518,7 @@
public void testTailRecursive2() {
chopTest(new BaseBinaryChop() {
public int find(final Object seeking, final List list) {
- return ((Number) Algorithms.recurse(new Function() {
+ return ((Number) new RecursiveEvaluation(new Function() {
public Object evaluate() {
if (sublist.isEmpty()) {
return BaseBinaryChop.NEGATIVE_ONE;
@@ -538,7 +539,7 @@
}
int offset = 0;
List sublist = list;
- })).intValue();
+ }).evaluate()).intValue();
}
});
}
Modified:
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/lines/TestLines.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/lines/TestLines.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/lines/TestLines.java
(original)
+++
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/lines/TestLines.java
Wed May 28 13:13:39 2008
@@ -26,7 +26,12 @@
import org.apache.commons.functor.adapter.ProcedureUnaryProcedure;
import org.apache.commons.functor.core.Offset;
+import org.apache.commons.functor.core.algorithm.FoldLeft;
import org.apache.commons.functor.core.collection.Size;
+import org.apache.commons.functor.core.composite.UnaryAnd;
+import org.apache.commons.functor.core.composite.UnaryNot;
+import org.apache.commons.functor.generator.FilteredGenerator;
+import org.apache.commons.functor.generator.TransformedGenerator;
/**
@@ -56,19 +61,15 @@
}
public void testCountCharacters() throws Exception {
- Object result = Lines
- .from(reader)
- .apply(Size.instance())
- .inject(new Integer(0),Sum.instance());
+ Object result = new FoldLeft(Sum.instance()).evaluate(
+ new TransformedGenerator(Lines.from(reader), Size.instance()));
assertEquals("Expected 990 characters",new Integer(990),result);
}
public void testCountWords() throws Exception {
- Object result = Lines
- .from(reader)
- .apply(WordCount.instance())
- .inject(new Integer(0),Sum.instance());
+ Object result = new FoldLeft(Sum.instance()).evaluate(
+ new
TransformedGenerator(Lines.from(reader),WordCount.instance()));
assertEquals("Expected 157 words",new Integer(157),result);
}
@@ -77,47 +78,35 @@
Count count = new Count();
Lines
.from(reader)
- .foreach(ProcedureUnaryProcedure.adapt(count));
+ .run(ProcedureUnaryProcedure.adapt(count));
assertEquals("Expected 16 lines",16,count.getCount());
}
public void testCountWordsExcludingComments() throws Exception {
- Object result = Lines
- .from(reader)
- .reject(new StartsWith("#"))
- .apply(WordCount.instance())
- .inject(new Integer(0),Sum.instance());
+ Object result = new FoldLeft(Sum.instance()).evaluate(new
TransformedGenerator(new FilteredGenerator(Lines
+ .from(reader), new UnaryNot(new StartsWith("#"))),
+ WordCount.instance()));
assertEquals("Expected 90 words",new Integer(90),result);
}
public void testCountCommentLines() throws Exception {
Count count = new Count();
- Lines
- .from(reader)
- .select(new StartsWith("#"))
- .foreach(ProcedureUnaryProcedure.adapt(count));
+ new FilteredGenerator(Lines.from(reader), new StartsWith("#"))
+ .run(ProcedureUnaryProcedure.adapt(count));
assertEquals("Expected 6 lines",6,count.getCount());
}
public void testFindMatchingLines() throws Exception {
- Collection matches =
- Lines
- .from(reader)
- .select(new Contains("lo"))
- .toCollection();
+ Collection matches = new FilteredGenerator(Lines.from(reader), new
Contains("lo")).toCollection();
assertEquals("Expected 5 lines",5,matches.size());
}
public void testFindMatchingFromTail() throws Exception {
- Collection matches =
- Lines
- .from(reader)
- .select(new Offset(8))
- .select(new Contains("lo"))
- .toCollection();
+ Collection matches = new FilteredGenerator(Lines.from(reader), new
UnaryAnd(new Offset(8), new Contains("lo")))
+ .toCollection();
assertEquals("Expected 2 lines",2,matches.size());
}
Modified:
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/map/FixedSizeMap.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/map/FixedSizeMap.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/map/FixedSizeMap.java
(original)
+++
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/map/FixedSizeMap.java
Wed May 28 13:13:39 2008
@@ -19,10 +19,10 @@
import java.lang.reflect.Array;
import java.util.Map;
-import org.apache.commons.functor.Algorithms;
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.BinaryProcedure;
import org.apache.commons.functor.adapter.BinaryProcedureBinaryFunction;
+import org.apache.commons.functor.core.algorithm.GeneratorContains;
import org.apache.commons.functor.core.composite.UnaryNot;
import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
@@ -51,7 +51,7 @@
Map dest = (Map) a;
Map src = (Map) b;
- if
(Algorithms.contains(IteratorToGeneratorAdapter.adapt(src.keySet().iterator()),UnaryNot.not(new
ContainsKey(dest)))) {
+ if
(GeneratorContains.instance().test(IteratorToGeneratorAdapter.adapt(src.keySet().iterator()),UnaryNot.not(new
ContainsKey(dest)))) {
throw new IllegalArgumentException();
} else {
dest.putAll(src);
Modified:
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java
(original)
+++
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java
Wed May 28 13:13:39 2008
@@ -18,25 +18,18 @@
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
-import java.util.NoSuchElementException;
import junit.framework.Test;
import junit.framework.TestCase;
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;
import org.apache.commons.functor.core.IsEqual;
-import org.apache.commons.functor.core.Offset;
import org.apache.commons.functor.generator.util.CollectionTransformer;
-import org.apache.commons.functor.generator.util.EachElement;
-import org.apache.commons.functor.generator.util.IntegerRange;
/**
* Tests the Base Generator class.
@@ -170,87 +163,6 @@
// Tests
// ------------------------------------------------------------------------
- public void testApply() {
- Generator gen = new IntegerRange(1,5);
- UnaryFunction dbl = new UnaryFunction() {
- public Object evaluate(Object obj) {
- return new Integer(2*((Number) obj).intValue());
- }
- };
- Summer summer = new Summer();
-
- gen.apply(dbl).run(summer);
-
- assertEquals(2*(1+2+3+4),summer.sum);
- }
-
- public void testContains() {
- assertTrue(EachElement.from(list).contains(equalsThree));
- assertTrue(!EachElement.from(list).contains(equalsTwentyThree));
- }
-
- public void testDetect() {
- assertEquals(new
Integer(3),EachElement.from(list).detect(equalsThree));
- try {
- EachElement.from(list).detect(equalsTwentyThree);
- fail("Expected NoSuchElementException");
- } catch(NoSuchElementException e) {
- // expected
- }
- }
-
- public void testDetectIfNone() {
- assertEquals(new
Integer(3),EachElement.from(list).detect(equalsThree,"Xyzzy"));
-
assertEquals("Xyzzy",EachElement.from(list).detect(equalsTwentyThree,"Xyzzy"));
- }
-
- public void testForEach() {
- Summer summer = new Summer();
- EachElement.from(list).foreach(summer);
- assertEquals(sum,summer.sum);
- }
-
- public void testWhere() {
-
assertEquals(evens,EachElement.from(list).where(isEven).toCollection());
- }
-
- public void testSelect1() {
-
assertEquals(evens,EachElement.from(list).select(isEven).toCollection());
- }
-
- public void testSelect2() {
- ArrayList result = new ArrayList();
- assertSame(result,EachElement.from(list).select(isEven).to(result));
- assertEquals(evens,result);
- }
-
- public void testReject1() {
-
assertEquals(evens,EachElement.from(list).reject(isOdd).toCollection());
- assertEquals(Collections.EMPTY_LIST,
EachElement.from(list).reject(isOdd).reject(isEven).toCollection());
- }
-
- public void testReject2() {
- ArrayList result = new ArrayList();
- assertSame(result,EachElement.from(list).reject(isOdd).to(result));
- assertEquals(evens,result);
- }
-
- public void testInject() {
- Object result = EachElement.from(list).inject(
- new Integer(0),
- new BinaryFunction() {
- public Object evaluate(Object a, Object b) {
- return new Integer(((Number) a).intValue() + ((Number)
b).intValue());
- }
- });
- assertEquals(new Integer(sum),result);
- }
-
- public void testLimit() {
- Collection col = simpleGenerator.until(new Offset(2)).toCollection();
- assertEquals("[0, 1]", col.toString());
- }
-
public void testTo() {
Collection col = (Collection) simpleGenerator.to(new
CollectionTransformer());
assertEquals("[0, 1, 2, 3, 4]", col.toString());
Modified:
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/util/TestEachElement.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/util/TestEachElement.java?rev=661067&r1=661066&r2=661067&view=diff
==============================================================================
---
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/util/TestEachElement.java
(original)
+++
commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/generator/util/TestEachElement.java
Wed May 28 13:13:39 2008
@@ -27,7 +27,12 @@
import junit.framework.TestSuite;
import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.core.Limit;
import org.apache.commons.functor.core.Offset;
+import org.apache.commons.functor.generator.GenerateUntil;
+import org.apache.commons.functor.generator.GenerateWhile;
+import org.apache.commons.functor.generator.UntilGenerate;
+import org.apache.commons.functor.generator.WhileGenerate;
/**
* @author Jason Horman ([EMAIL PROTECTED])
@@ -129,9 +134,10 @@
}
public void testWithStop() {
- Collection col = EachElement.from(list).until(new
Offset(3)).toCollection();
- assertEquals("[0, 1, 2]", col.toString());
-
+ assertEquals("[0, 1, 2]", new UntilGenerate(new Offset(3),
EachElement.from(list)).toCollection().toString());
+ assertEquals("[0, 1, 2, 3]", new GenerateUntil(EachElement.from(list),
new Offset(3)).toCollection().toString());
+ assertEquals("[0, 1, 2]", new WhileGenerate(new Limit(3),
EachElement.from(list)).toCollection().toString());
+ assertEquals("[0, 1, 2, 3]", new GenerateWhile(EachElement.from(list),
new Limit(3)).toCollection().toString());
}
public void testWithIterator() {