You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2013/01/27 18:32:24 UTC
svn commit: r1439123 [2/2] - in
/commons/proper/functor/branches/FUNCTOR-14-mm: ./ core/
core/src/main/java/org/apache/commons/functor/core/algorithm/
core/src/main/java/org/apache/commons/functor/generator/
core/src/main/java/org/apache/commons/functo...
Modified: commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/range/TestIntegerRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/range/TestIntegerRange.java?rev=1439123&r1=1376354&r2=1439123&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/range/TestIntegerRange.java (original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/range/TestIntegerRange.java Sun Jan 27 17:32:23 2013
@@ -17,13 +17,22 @@
package org.apache.commons.functor.generator.range;
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.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import org.apache.commons.functor.BaseFunctorTest;
-import org.apache.commons.functor.generator.range.IntegerRange;
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.generator.Generator;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
/**
@@ -31,19 +40,42 @@ import org.junit.Test;
*/
public class TestIntegerRange extends BaseFunctorTest {
+ // Attributes
+ // ------------------------------------------------------------------------
+ private IntegerRange ascIntRange = null;
+ private IntegerRange descIntRange = null;
+ private Collection<Integer> expectedAsc = null;
+ private Collection<Integer> expectedDesc = null;
+
+ // Test set up
+ // ------------------------------------------------------------------------
+ @Before
+ public void setUp() {
+ ascIntRange = Ranges.integerRange(0, 10);
+ descIntRange = Ranges.integerRange(10, 0);
+ expectedAsc = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
+ expectedDesc = Arrays.asList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
+ }
+
+ @After
+ public void tearDown() {
+ ascIntRange = null;
+ descIntRange = null;
+ }
+
@Override
protected Object makeFunctor() throws Exception {
- return new IntegerRange(10, 20);
+ return Ranges.integerRange(10, 20);
}
- // Tests
+ // Generator tests
// ------------------------------------------------------------------------
@Test
public void testGenerateListExample() {
// generates a collection of Integers from 0 (inclusive) to 10 (exclusive)
{
- List<? super Integer> list = (List<? super Integer>)(new IntegerRange(0,10).to(new ArrayList<Integer>()));
+ List<? super Integer> list = (List<? super Integer>)(Ranges.integerRange(0,10).to(new ArrayList<Integer>()));
for (int i=0;i<10;i++) {
assertEquals(new Integer(i),list.get(i));
}
@@ -51,7 +83,7 @@ public class TestIntegerRange extends Ba
// generates a collection of Integers from 10 (inclusive) to 0 (exclusive)
{
- List<? super Integer> list = (List<? super Integer>)(new IntegerRange(10,0).to(new ArrayList<Integer>()));
+ List<? super Integer> list = (List<? super Integer>)(Ranges.integerRange(10,0).to(new ArrayList<Integer>()));
for (int i=10;i>0;i--) {
assertEquals(new Integer(i),list.get(10-i));
}
@@ -61,34 +93,34 @@ public class TestIntegerRange extends Ba
@Test
public void testStepChecking() {
{
- new IntegerRange(2, 2, 0); // step of 0 is ok when range is empty
+ Ranges.integerRange(2, 2, 0); // step of 0 is ok when range is empty
}
{
- new IntegerRange(2, 2, 1); // positive step is ok when range is empty
+ Ranges.integerRange(2, 2, 1); // positive step is ok when range is empty
}
{
- new IntegerRange(2, 2, -1); // negative step is ok when range is empty
+ Ranges.integerRange(2, 2, -1); // negative step is ok when range is empty
}
{
- new IntegerRange(0, 1, 10); // big steps are ok
+ Ranges.integerRange(0, 1, 10); // big steps are ok
}
{
- new IntegerRange(1, 0, -10); // big steps are ok
+ Ranges.integerRange(1, 0, -10); // big steps are ok
}
try {
- new IntegerRange(0, 1, 0);
+ Ranges.integerRange(0, 1, 0);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
try {
- new IntegerRange(0, 1, -1);
+ Ranges.integerRange(0, 1, -1);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
try {
- new IntegerRange(0, -1, 1);
+ Ranges.integerRange(0, -1, 1);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
@@ -97,59 +129,628 @@ public class TestIntegerRange extends Ba
@Test
public void testObjectConstructor() {
- IntegerRange range = new IntegerRange(new Integer(0), new Integer(5));
+ IntegerRange range = Ranges.integerRange(new Integer(0), new Integer(5));
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
- range = new IntegerRange(new Integer(0), new Integer(5), new Integer(1));
+ range = Ranges.integerRange(new Integer(0), new Integer(5), new Integer(1));
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
}
@Test
public void testReverseStep() {
- IntegerRange range = new IntegerRange(10, 0, -2);
+ IntegerRange range = Ranges.integerRange(10, 0, -2);
assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
}
@Test
public void testStep() {
- IntegerRange range = new IntegerRange(0, 10, 2);
+ IntegerRange range = Ranges.integerRange(0, 10, 2);
assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
}
@Test
public void testForwardRange() {
- IntegerRange range = new IntegerRange(0, 5);
+ IntegerRange range = Ranges.integerRange(0, 5);
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
}
@Test
public void testReverseRange() {
- IntegerRange range = new IntegerRange(5, 0);
+ IntegerRange range = Ranges.integerRange(5, 0);
assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
}
@Test
public void testEdgeCase() {
- IntegerRange range = new IntegerRange(Integer.MAX_VALUE - 3, Integer.MAX_VALUE);
+ IntegerRange range = Ranges.integerRange(Integer.MAX_VALUE - 3, Integer.MAX_VALUE);
assertEquals("[2147483644, 2147483645, 2147483646]", range.toCollection().toString());
assertEquals("[2147483644, 2147483645, 2147483646]", range.toCollection().toString());
}
@Test
- public void testEquals() {
- IntegerRange range = new IntegerRange(1, 5);
- assertObjectsAreEqual(range, range);
- assertObjectsAreEqual(range, new IntegerRange(1, 5));
- assertObjectsAreEqual(range, new IntegerRange(1, 5, 1));
- assertObjectsAreEqual(range, new IntegerRange(new Long(1), new Long(5)));
- assertObjectsAreEqual(range, new IntegerRange(new Long(1), new Long(5), new Long(1)));
- assertObjectsAreNotEqual(range, new IntegerRange(2, 5));
- assertObjectsAreNotEqual(range, new IntegerRange(1, 6));
- assertObjectsAreNotEqual(range, new IntegerRange(1, 5, 2));
+ public void testBoundaries() {
+ IntegerRange range = Ranges.integerRange(0, 10);
+ assertEquals(new Endpoint<Comparable<?>>(0, BoundType.CLOSED),
+ range.getLeftEndpoint());
+ assertEquals(new Endpoint<Comparable<?>>(10, BoundType.OPEN),
+ range.getRightEndpoint());
+ }
+
+ @Test
+ public void testClosedClosedAscending() {
+ // [-5, 5], 3 = -5, -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.CLOSED, 5,
+ BoundType.CLOSED, 3);
+ // [-5, 5], 3 = -5, -2, 1, 4
+ List<Integer> expected = Arrays.asList(-5, -2, 1, 4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenClosedAscending() {
+ // (-5, 5], 3 = -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.OPEN, 5,
+ BoundType.CLOSED, 3);
+ // (-5, 5], 3 = -2, 1, 4
+ List<Integer> expected = Arrays.asList(-2, 1, 4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedOpenAscending() {
+ // [-5, 5), 3 = -5, -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.CLOSED, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5], 3 = -5, -2, 1, 4
+ List<Integer> expected = Arrays.asList(-5, -2, 1, 4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenOpenAscending() {
+ // (-5, 5), 3 = -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.OPEN, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5), 3 = -2, 1, 4
+ List<Integer> expected = Arrays.asList(-2, 1, 4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testSingleStepAscending() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ IntegerRange range = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ // (-2, 2], 1 = -1, 0, 1, 2
+ List<Integer> expected = Arrays.asList(-1, 0, 1, 2);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedClosedDescending() {
+ // [5, -5], -3 = 5, 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.CLOSED, -5,
+ BoundType.CLOSED, -3);
+ // [5, -5], -3 = 5, 2, -1, -4
+ List<Integer> expected = Arrays.asList(5, 2, -1, -4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenClosedDescending() {
+ // (5, -5], -3 = 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.OPEN, -5,
+ BoundType.CLOSED, -3);
+ // (5, -5], -3 = 2, -1, -4
+ List<Integer> expected = Arrays.asList(2, -1, -4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedOpenDescending() {
+ // [5, -5), -3 = 5, 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.CLOSED, -5,
+ BoundType.OPEN, -3);
+ // [5, -5), -3 = 5, 2, -1, -4
+ List<Integer> expected = Arrays.asList(5, 2, -1, -4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenOpenDescending() {
+ // (5, -5), -3 = 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.OPEN, -5,
+ BoundType.OPEN, -3);
+ // (5, -5), -3 = 2, -1, -4
+ List<Integer> expected = Arrays.asList(2, -1, -4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testSingleStepDescending() {
+ // [2, -2), -1 = 2, 1, 0, -1
+ IntegerRange range = Ranges.integerRange(2, BoundType.CLOSED, -2,
+ BoundType.OPEN, -1);
+ // [2, -2), -1 = 2, 1, 0, -1
+ List<Integer> expected = Arrays.asList(2, 1, 0, -1);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testAscending() {
+ final List<Integer> list = new ArrayList<Integer>();
+ ascIntRange.run(new UnaryProcedure<Integer>() {
+
+ public void run(Integer obj) {
+ list.add(obj);
+ }
+ });
+ assertTrue(expectedAsc.containsAll(list));
+ }
+
+ @Test
+ public void testDescending() {
+ final List<Integer> list = new ArrayList<Integer>();
+ descIntRange.run(new UnaryProcedure<Integer>() {
+
+ public void run(Integer obj) {
+ list.add(obj);
+ }
+ });
+ assertTrue(expectedDesc.containsAll(list));
+ }
+
+ @Test
+ public void testToCollection() {
+ Collection<Integer> ascCol = ascIntRange.toCollection();
+ assertEquals("Different collections", expectedAsc, ascCol);
+ Collection<Integer> descCol = descIntRange.toCollection();
+ assertEquals("Different collections", expectedDesc, descCol);
+ }
+
+ @Test
+ public void testTransformedGenerator() {
+ int expected = 45;
+ int total = ascIntRange
+ .to(new UnaryFunction<Generator<? extends Integer>, Integer>() {
+
+ public Integer evaluate(Generator<? extends Integer> obj) {
+ int total = 0;
+ for (Object element : obj.toCollection()) {
+ total += (Integer) element;
+ }
+ return total;
+ }
+ });
+ assertEquals(expected, total);
+ expected = 55;
+ total = descIntRange
+ .to(new UnaryFunction<Generator<? extends Integer>, Integer>() {
+
+ public Integer evaluate(Generator<? extends Integer> obj) {
+ int total = 0;
+ for (Object element : obj.toCollection()) {
+ total += (Integer) element;
+ }
+ return total;
+ }
+ });
+ assertEquals(expected, total);
+ }
+
+ // Range tests
+ // ---------------------------------------------------------------
+
+ // A base range with all integers between -6 and 6
+ private final List<Integer> fullRange = Collections.unmodifiableList(Arrays
+ .asList(-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6));
+
+ @Test
+ public void testEmptyRanges() {
+ IntegerRange empty1 = Ranges.integerRange(-2, BoundType.OPEN, -1,
+ BoundType.OPEN, 2);
+ assertTrue("The range was expected to be empty.", empty1.isEmpty());
+ IntegerRange empty2 = Ranges.integerRange(2, BoundType.OPEN, 0,
+ BoundType.OPEN, -2);
+ assertTrue("The range was expected to be empty.", empty2.isEmpty());
+ IntegerRange empty3 = Ranges.integerRange(0, BoundType.OPEN, 1,
+ BoundType.CLOSED, 2);
+ assertTrue("The range was expected to be empty.", empty3.isEmpty());
+ IntegerRange empty4 = Ranges.integerRange(-3, BoundType.OPEN, -3,
+ BoundType.OPEN, 1);
+ assertTrue("The range was expected to be empty.", empty4.isEmpty());
+ IntegerRange empty5 = Ranges.integerRange(-3, BoundType.CLOSED, -3,
+ BoundType.OPEN, 1);
+ assertTrue("The range was expected to be empty.", empty5.isEmpty());
+ IntegerRange empty6 = Ranges.integerRange(1, BoundType.OPEN, 0,
+ BoundType.CLOSED, -2);
+ assertTrue("The range was expected to be empty.", empty6.isEmpty());
+ IntegerRange notEmpty1 = Ranges.integerRange(-3, BoundType.CLOSED, -3,
+ BoundType.CLOSED, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty1.isEmpty());
+ IntegerRange notEmpty2 = Ranges.integerRange(-3, BoundType.OPEN, -2,
+ BoundType.CLOSED, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty2.isEmpty());
+ IntegerRange notEmpty3 = Ranges.integerRange(2, BoundType.OPEN, 1,
+ BoundType.CLOSED, -1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty3.isEmpty());
+ IntegerRange notEmpty4 = Ranges.integerRange(2, BoundType.CLOSED, 1,
+ BoundType.OPEN, -1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty4.isEmpty());
+ IntegerRange notEmpty5 = Ranges.integerRange(1, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty5.isEmpty());
+ }
+
+ @Test
+ public void testClosedClosedAscendingContains() {
+ // [-5, 5], 3 = -5, -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.CLOSED, 5,
+ BoundType.CLOSED, 3);
+ // [-5, 5], 3 = -5, -2, 1, 4
+ List<Integer> arr = Arrays.asList(-5, -2, 1, 4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenClosedAscendingContains() {
+ // (-5, 5], 3 = -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.OPEN, 5,
+ BoundType.CLOSED, 3);
+ // (-5, 5], 3 = -2, 1, 4
+ List<Integer> arr = Arrays.asList(-2, 1, 4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedOpenAscendingContains() {
+ // [-5, 5), 3 = -5, -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.CLOSED, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5], 3 = -5, -2, 1, 4
+ List<Integer> arr = Arrays.asList(-5, -2, 1, 4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenOpenAscendingContains() {
+ // (-5, 5), 3 = -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.OPEN, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5), 3 = -2, 1, 4
+ List<Integer> arr = Arrays.asList(-2, 1, 4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsSingleStepAscending() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ IntegerRange ascendingRange = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ // (-2, 2], 1 = -1, 0, 1, 2
+ List<Integer> arr = Arrays.asList(-1, 0, 1, 2);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + ascendingRange + "]",
+ ascendingRange.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + ascendingRange +
+ "]",
+ ascendingRange.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedClosedDescendingContains() {
+ // [5, -5], -3 = 5, 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.CLOSED, -5,
+ BoundType.CLOSED, -3);
+ // [5, -5], -3 = 5, 2, -1, -4
+ List<Integer> arr = Arrays.asList(5, 2, -1, -4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenClosedDescendingContains() {
+ // (5, -5], -3 = 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.OPEN, -5,
+ BoundType.CLOSED, -3);
+ // (5, -5], -3 = 2, -1, -4
+ List<Integer> arr = Arrays.asList(2, -1, -4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedOpenDescendingContains() {
+ // [5, -5), -3 = 5, 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.CLOSED, -5,
+ BoundType.OPEN, -3);
+ // [5, -5), -3 = 5, 2, -1, -4
+ List<Integer> arr = Arrays.asList(5, 2, -1, -4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenOpenDescendingContains() {
+ // (5, -5), -3 = 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.OPEN, -5,
+ BoundType.OPEN, -3);
+ // (5, -5), -3 = 2, -1, -4
+ List<Integer> arr = Arrays.asList(2, -1, -4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsSingleStepDescending() {
+ // [2, -2), -1 = 2, 1, 0, -1
+ IntegerRange descendingRange = Ranges.integerRange(2, BoundType.CLOSED,
+ -2, BoundType.OPEN, -1);
+ // [2, -2), -1 = 2, 1, 0, -1
+ List<Integer> arr = Arrays.asList(2, 1, 0, -1);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + descendingRange +
+ "]",
+ descendingRange.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + descendingRange +
+ "]",
+ descendingRange.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsNullOrEmpty() {
+ IntegerRange range = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ assertFalse(range.contains(null));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testContainsAll() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ IntegerRange range = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ List<Integer> list = Arrays.asList(-1, 0, 1, 2);
+ assertTrue("Range [" + range +
+ "] was expected to contain all elements from list [" + list +
+ "]", range.containsAll(list));
+ List<Integer> listWithExtraElements = Arrays.asList(2, -1, 0, 1, 2, 3);
+ assertFalse("Range [" + range + "] has more elements than expected",
+ range.containsAll(listWithExtraElements));
+ assertFalse(range.containsAll(null));
+ assertFalse(range.containsAll(Collections.EMPTY_LIST));
+ }
+
+ @Test
+ public void testEquals()
+ throws Exception {
+ // equals basic properties
+ IntegerRange range = Ranges.integerRange(-2, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertEquals("equals must be reflexive", range, range);
+ assertEquals("hashCode must be reflexive", range.hashCode(),
+ range.hashCode());
+ assertTrue(!range.equals(null)); // should be able to compare to null
+
+ Object range2 = Ranges.integerRange(-2, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ if (range.equals(range2)) {
+ assertEquals("equals implies hash equals", range.hashCode(),
+ range2.hashCode());
+ assertEquals("equals must be symmetric", range2, range);
+ } else {
+ assertTrue("equals must be symmetric", !range2.equals(range));
+ }
+
+ // Changing attributes
+ Object range3 = Ranges.integerRange(-1, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range3));
+
+ Object range4 = Ranges.integerRange(-2, BoundType.OPEN, 2, BoundType.OPEN,
+ 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range4));
+
+ Object range5 = Ranges.integerRange(-2, BoundType.CLOSED, 1,
+ BoundType.OPEN, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range5));
+
+ Object range6 = Ranges.integerRange(-2, BoundType.CLOSED, 2,
+ BoundType.CLOSED, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range6));
+
+ Object range7 = Ranges.integerRange(-2, BoundType.CLOSED, 2,
+ BoundType.OPEN, 2);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range7));
+
+ // Using different constructors
+ IntegerRange range8 = Ranges.integerRange(Long.valueOf(-2),
+ Long.valueOf(2), Long.valueOf(1));
+ assertEquals("Invalid equals using different constructor", range,
+ range8);
+
+ IntegerRange range9 = Ranges.integerRange(Long.valueOf(-2),
+ Long.valueOf(2));
+ assertEquals("Invalid equals using different constructor", range,
+ range9);
+
+ Endpoint<Integer> leftEndpoint = new Endpoint<Integer>(-2,
+ BoundType.CLOSED);
+ Endpoint<Integer> rightEndpoint = new Endpoint<Integer>(2,
+ BoundType.OPEN);
+ IntegerRange range10 = Ranges.integerRange(leftEndpoint, rightEndpoint, 1);
+ assertEquals("Invalid equals using different constructor", range,
+ range10);
+ }
+
+ @Test
+ public void testToString() {
+ IntegerRange range = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ assertEquals("Wrong string value", "IntegerRange<(-2, 2], 1>",
+ range.toString());
+ }
+
+ @Test
+ public void testConstructorUsingSameEndpoint() {
+ Endpoint<Integer> uniqueEndpoint = new Endpoint<Integer>(
+ 10,
+ BoundType.CLOSED);
+ try {
+ Ranges.integerRange(uniqueEndpoint, uniqueEndpoint, 1);
+ } catch (IllegalArgumentException e) {
+ fail("Not expected to get here");
+ }
+ }
+
+ @Test
+ public void testInvalidRange() {
+ try {
+ Ranges.integerRange(10, BoundType.OPEN, -5, BoundType.CLOSED, 10);
+ fail("Not expected to get here");
+ } catch (IllegalArgumentException e) {
+ // Do nothing
+ }
+ Endpoint<Integer> leftEndpoint = new Endpoint<Integer>(10,
+ BoundType.CLOSED);
+ Endpoint<Integer> rightEndpoint = new Endpoint<Integer>(-5,
+ BoundType.OPEN);
+ try {
+ Ranges.integerRange(leftEndpoint, rightEndpoint, 1);
+ fail("Not expected to get here");
+ } catch (IllegalArgumentException e) {
+ // Do nothing
+ }
+ }
+
+ @Test
+ public void testDefaultStep() {
+ assertEquals("Invalid default step", Integer.valueOf(-1),
+ IntegerRange.DEFAULT_STEP.evaluate(10, 1));
+ assertEquals("Invalid default step", Integer.valueOf(1),
+ IntegerRange.DEFAULT_STEP.evaluate(1, 10));
}
}
\ No newline at end of file
Modified: commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/range/TestLongRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/range/TestLongRange.java?rev=1439123&r1=1376354&r2=1439123&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/range/TestLongRange.java (original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/range/TestLongRange.java Sun Jan 27 17:32:23 2013
@@ -17,13 +17,22 @@
package org.apache.commons.functor.generator.range;
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.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import org.apache.commons.functor.BaseFunctorTest;
-import org.apache.commons.functor.generator.range.LongRange;
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.generator.Generator;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
/**
@@ -31,19 +40,47 @@ import org.junit.Test;
*/
public class TestLongRange extends BaseFunctorTest {
+ // A base range with all longs between -6 and 6
+ private final List<Long> fullRange = Collections.unmodifiableList(Arrays
+ .asList(-6L, -5L, -4L, -3L, -2L, -1L, 0L, 1L, 2L, 3L, 4L, 5L, 6L));
+
+ // Attributes
+ // ------------------------------------------------------------------------
+ private LongRange ascLongRange = null;
+ private LongRange descLongRange = null;
+ private Collection<Long> expectedAsc = null;
+ private Collection<Long> expectedDesc = null;
+
+ // Test set up
+ // ------------------------------------------------------------------------
+ @Before
+ public void setUp() {
+ ascLongRange = Ranges.longRange(0L, 10L);
+ descLongRange = Ranges.longRange(10L, 0L);
+ expectedAsc = Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
+ expectedDesc = Arrays.asList(10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L);
+ }
+
+ @After
+ public void tearDown() {
+ ascLongRange = null;
+ descLongRange = null;
+ }
+
@Override
- protected Object makeFunctor() throws Exception {
- return new LongRange(10, 20);
+ protected Object makeFunctor()
+ throws Exception {
+ return Ranges.longRange(10, 20);
}
- // Tests
+ // Generator tests
// ------------------------------------------------------------------------
@Test
public void testGenerateListExample() {
// generates a collection of Integers from 0 (inclusive) to 10 (exclusive)
{
- List<? super Long> list = (List<? super Long>)(new LongRange(0,10).to(new ArrayList<Long>()));
+ List<? super Long> list = (List<? super Long>)(Ranges.longRange(0,10).to(new ArrayList<Long>()));
for (int i=0;i<10;i++) {
assertEquals(new Long(i),list.get(i));
}
@@ -51,7 +88,7 @@ public class TestLongRange extends BaseF
// generates a collection of Integers from 10 (inclusive) to 0 (exclusive)
{
- List<? super Long> list = (List<? super Long>)(new LongRange(10,0).to(new ArrayList<Long>()));
+ List<? super Long> list = (List<? super Long>)(Ranges.longRange(10,0).to(new ArrayList<Long>()));
for (int i=10;i>0;i--) {
assertEquals(new Long(i),list.get(10-i));
}
@@ -61,34 +98,34 @@ public class TestLongRange extends BaseF
@Test
public void testStepChecking() {
{
- new LongRange(2, 2, 0); // step of 0 is ok when range is empty
+ Ranges.longRange(2, 2, 0); // step of 0 is ok when range is empty
}
{
- new LongRange(2, 2, 1); // positive step is ok when range is empty
+ Ranges.longRange(2, 2, 1); // positive step is ok when range is empty
}
{
- new LongRange(2, 2, -1); // negative step is ok when range is empty
+ Ranges.longRange(2, 2, -1); // negative step is ok when range is empty
}
{
- new LongRange(0, 1, 10); // big steps are ok
+ Ranges.longRange(0, 1, 10); // big steps are ok
}
{
- new LongRange(1, 0, -10); // big steps are ok
+ Ranges.longRange(1, 0, -10); // big steps are ok
}
try {
- new LongRange(0, 1, 0);
+ Ranges.longRange(0, 1, 0);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
try {
- new LongRange(0, 1, -1);
+ Ranges.longRange(0, 1, -1);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
try {
- new LongRange(0, -1, 1);
+ Ranges.longRange(0, -1, 1);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
@@ -97,59 +134,618 @@ public class TestLongRange extends BaseF
@Test
public void testObjectConstructor() {
- LongRange range = new LongRange(new Long(0), new Long(5));
+ LongRange range = Ranges.longRange(new Long(0), new Long(5));
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
- range = new LongRange(new Integer(0), new Long(5), new Long(1));
+ range = Ranges.longRange(new Integer(0), new Long(5), new Long(1));
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
}
@Test
public void testReverseStep() {
- LongRange range = new LongRange(10, 0, -2);
+ LongRange range = Ranges.longRange(10, 0, -2);
assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
}
@Test
public void testStep() {
- LongRange range = new LongRange(0, 10, 2);
+ LongRange range = Ranges.longRange(0, 10, 2);
assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
}
@Test
public void testForwardRange() {
- LongRange range = new LongRange(0, 5);
+ LongRange range = Ranges.longRange(0, 5);
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
}
@Test
public void testReverseRange() {
- LongRange range = new LongRange(5, 0);
+ LongRange range = Ranges.longRange(5, 0);
assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
}
@Test
public void testEdgeCase() {
- LongRange range = new LongRange(Long.MAX_VALUE - 3L, Long.MAX_VALUE);
+ LongRange range = Ranges.longRange(Long.MAX_VALUE - 3L, Long.MAX_VALUE);
assertEquals("[9223372036854775804, 9223372036854775805, 9223372036854775806]", range.toCollection().toString());
assertEquals("[9223372036854775804, 9223372036854775805, 9223372036854775806]", range.toCollection().toString());
}
+
+ @Test
+ public void testClosedClosedAscending() {
+ // [-5L, 5L], 3L = -5L, -2L, 1L, 4L
+ LongRange range = Ranges.longRange(-5L, BoundType.CLOSED, 5L,
+ BoundType.CLOSED, 3L);
+ // [-5L, 5L], 3L = -5L, -2L, 1L, 4L
+ List<Long> expected = Arrays.asList(-5L, -2L, 1L, 4L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenClosedAscending() {
+ // (-5L, 5L], 3L = -2L, 1L, 4L
+ LongRange range = Ranges.longRange(-5L, BoundType.OPEN, 5L,
+ BoundType.CLOSED, 3L);
+ // (-5L, 5L], 3L = -2L, 1L, 4L
+ List<Long> expected = Arrays.asList(-2L, 1L, 4L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedOpenAscending() {
+ // [-5L, 5L), 3L = -5L, -2L, 1L, 4L
+ LongRange range = Ranges.longRange(-5L, BoundType.CLOSED, 5L,
+ BoundType.OPEN, 3L);
+ // (-5L, 5L], 3L = -5L, -2L, 1L, 4L
+ List<Long> expected = Arrays.asList(-5L, -2L, 1L, 4L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenOpenAscending() {
+ // (-5L, 5L), 3L = -2L, 1L, 4L
+ LongRange range = Ranges.longRange(-5L, BoundType.OPEN, 5L,
+ BoundType.OPEN, 3L);
+ // (-5L, 5L), 3L = -2L, 1L, 4L
+ List<Long> expected = Arrays.asList(-2L, 1L, 4L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testSingleStepAscending() {
+ // (-2L, 2L], 1L = -1L, 0L, 1L, 2L
+ LongRange range = Ranges.longRange(-2L, BoundType.OPEN, 2L,
+ BoundType.CLOSED, 1L);
+ // (-2L, 2L], 1L = -1L, 0L, 1L, 2L
+ List<Long> expected = Arrays.asList(-1L, 0L, 1L, 2L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedClosedDescending() {
+ // [5L, -5L], -3L = 5L, 2L, -1L, -4L
+ LongRange range = Ranges.longRange(5L, BoundType.CLOSED, -5L,
+ BoundType.CLOSED, -3L);
+ // [5L, -5L], -3L = 5L, 2L, -1L, -4L
+ List<Long> expected = Arrays.asList(5L, 2L, -1L, -4L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenClosedDescending() {
+ // (5L, -5L], -3L = 2L, -1L, -4L
+ LongRange range = Ranges.longRange(5L, BoundType.OPEN, -5L,
+ BoundType.CLOSED, -3L);
+ // (5L, -5L], -3L = 2L, -1L, -4L
+ List<Long> expected = Arrays.asList(2L, -1L, -4L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedOpenDescending() {
+ // [5L, -5L), -3L = 5L, 2L, -1L, -4L
+ LongRange range = Ranges.longRange(5L, BoundType.CLOSED, -5L,
+ BoundType.OPEN, -3L);
+ // [5L, -5L), -3L = 5L, 2L, -1L, -4L
+ List<Long> expected = Arrays.asList(5L, 2L, -1L, -4L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenOpenDescending() {
+ // (5L, -5L), -3L = 2L, -1L, -4L
+ LongRange range = Ranges.longRange(5L, BoundType.OPEN, -5L,
+ BoundType.OPEN, -3L);
+ // (5L, -5L), -3L = 2L, -1L, -4L
+ List<Long> expected = Arrays.asList(2L, -1L, -4L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testSingleStepDescending() {
+ // [2L, -2L), -1L = 2L, 1L, 0L, -1L
+ LongRange range = Ranges.longRange(2L, BoundType.CLOSED, -2L,
+ BoundType.OPEN, -1L);
+ // [2L, -2L), -1L = 2L, 1L, 0L, -1L
+ List<Long> expected = Arrays.asList(2L, 1L, 0L, -1L);
+ Collection<Long> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testAscending() {
+ final List<Long> list = new ArrayList<Long>();
+ ascLongRange.run(new UnaryProcedure<Long>() {
+
+ public void run(Long obj) {
+ list.add(obj);
+ }
+ });
+ assertTrue(expectedAsc.containsAll(list));
+ }
+
+ @Test
+ public void testDescending() {
+ final List<Long> list = new ArrayList<Long>();
+ descLongRange.run(new UnaryProcedure<Long>() {
+
+ public void run(Long obj) {
+ list.add(obj);
+ }
+ });
+ assertTrue(expectedDesc.containsAll(list));
+ }
+
+ @Test
+ public void testToCollection() {
+ Collection<Long> ascCol = ascLongRange.toCollection();
+ assertEquals("Different collections", expectedAsc, ascCol);
+ Collection<Long> descCol = descLongRange.toCollection();
+ assertEquals("Different collections", expectedDesc, descCol);
+ }
+
+ @Test
+ public void testTransformedGenerator() {
+ long expected = 45L;
+ long total = ascLongRange
+ .to(new UnaryFunction<Generator<? extends Long>, Long>() {
+
+ public Long evaluate(Generator<? extends Long> obj) {
+ long total = 0L;
+ for (Object element : obj.toCollection()) {
+ total += (Long) element;
+ }
+ return total;
+ }
+ });
+ assertEquals(expected, total);
+ expected = 55L;
+ total = descLongRange
+ .to(new UnaryFunction<Generator<? extends Long>, Long>() {
+
+ public Long evaluate(Generator<? extends Long> obj) {
+ long total = 0L;
+ for (Object element : obj.toCollection()) {
+ total += (Long) element;
+ }
+ return total;
+ }
+ });
+ assertEquals(expected, total);
+ }
+
+ // Range tests
+ // ------------------------------------------------------------------------
+
+ @Test
+ public void testEmptyRanges() {
+ LongRange empty1 = Ranges.longRange(-2, BoundType.OPEN, -1,
+ BoundType.OPEN, 2);
+ assertTrue("The range was expected to be empty.", empty1.isEmpty());
+ LongRange empty2 = Ranges.longRange(2, BoundType.OPEN, 0, BoundType.OPEN,
+ -2);
+ assertTrue("The range was expected to be empty.", empty2.isEmpty());
+ LongRange empty3 = Ranges.longRange(0, BoundType.OPEN, 1,
+ BoundType.CLOSED, 2);
+ assertTrue("The range was expected to be empty.", empty3.isEmpty());
+ LongRange empty4 = Ranges.longRange(-3, BoundType.OPEN, -3,
+ BoundType.OPEN, 1);
+ assertTrue("The range was expected to be empty.", empty4.isEmpty());
+ LongRange empty5 = Ranges.longRange(-3, BoundType.CLOSED, -3,
+ BoundType.OPEN, 1);
+ assertTrue("The range was expected to be empty.", empty5.isEmpty());
+ LongRange empty6 = Ranges.longRange(1, BoundType.OPEN, 0,
+ BoundType.CLOSED, -2);
+ assertTrue("The range was expected to be empty.", empty6.isEmpty());
+ LongRange notEmpty1 = Ranges.longRange(-3, BoundType.CLOSED, -3,
+ BoundType.CLOSED, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty1.isEmpty());
+ LongRange notEmpty2 = Ranges.longRange(-3, BoundType.OPEN, -2,
+ BoundType.CLOSED, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty2.isEmpty());
+ LongRange notEmpty3 = Ranges.longRange(2, BoundType.OPEN, 1,
+ BoundType.CLOSED, -1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty3.isEmpty());
+ LongRange notEmpty4 = Ranges.longRange(2, BoundType.CLOSED, 1,
+ BoundType.OPEN, -1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty4.isEmpty());
+ LongRange notEmpty5 = Ranges.longRange(1, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty5.isEmpty());
+ }
+
+ @Test
+ public void testBoundaries() {
+ LongRange range = Ranges.longRange(0L, 10L);
+ assertEquals(new Endpoint<Comparable<?>>(0L, BoundType.CLOSED),
+ range.getLeftEndpoint());
+ assertEquals(new Endpoint<Comparable<?>>(10L, BoundType.OPEN),
+ range.getRightEndpoint());
+ }
+
+ @Test
+ public void testClosedClosedAscendingContains() {
+ // [-5, 5], 3 = -5, -2, 1, 4
+ LongRange range = Ranges.longRange(-5, BoundType.CLOSED, 5,
+ BoundType.CLOSED, 3);
+ // [-5, 5], 3 = -5, -2, 1, 4
+ List<Long> arr = Arrays.asList(-5L, -2L, 1L, 4L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenClosedAscendingContains() {
+ // (-5, 5], 3 = -2, 1, 4
+ LongRange range = Ranges.longRange(-5, BoundType.OPEN, 5,
+ BoundType.CLOSED, 3);
+ // (-5, 5], 3 = -2, 1, 4
+ List<Long> arr = Arrays.asList(-2L, 1L, 4L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedOpenAscendingContains() {
+ // [-5, 5), 3 = -5, -2, 1, 4
+ LongRange range = Ranges.longRange(-5, BoundType.CLOSED, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5], 3 = -5, -2, 1, 4
+ List<Long> arr = Arrays.asList(-5L, -2L, 1L, 4L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenOpenAscendingContains() {
+ // (-5, 5), 3 = -2, 1, 4
+ LongRange range = Ranges.longRange(-5, BoundType.OPEN, 5, BoundType.OPEN,
+ 3);
+ // (-5, 5), 3 = -2, 1, 4
+ List<Long> arr = Arrays.asList(-2L, 1L, 4L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsSingleStepAscending() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ LongRange ascendingRange = Ranges.longRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ // (-2, 2], 1 = -1, 0, 1, 2
+ List<Long> arr = Arrays.asList(-1L, 0L, 1L, 2L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + ascendingRange + "]",
+ ascendingRange.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + ascendingRange +
+ "]",
+ ascendingRange.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedClosedDescendingContains() {
+ // [5, -5], -3 = 5, 2, -1, -4
+ LongRange range = Ranges.longRange(5, BoundType.CLOSED, -5,
+ BoundType.CLOSED, -3);
+ // [5, -5], -3 = 5, 2, -1, -4
+ List<Long> arr = Arrays.asList(5L, 2L, -1L, -4L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenClosedDescendingContains() {
+ // (5, -5], -3 = 2, -1, -4
+ LongRange range = Ranges.longRange(5, BoundType.OPEN, -5,
+ BoundType.CLOSED, -3);
+ // (5, -5], -3 = 2, -1, -4
+ List<Long> arr = Arrays.asList(2L, -1L, -4L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedOpenDescendingContains() {
+ // [5, -5), -3 = 5, 2, -1, -4
+ LongRange range = Ranges.longRange(5, BoundType.CLOSED, -5,
+ BoundType.OPEN, -3);
+ // [5, -5), -3 = 5, 2, -1, -4
+ List<Long> arr = Arrays.asList(5L, 2L, -1L, -4L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenOpenDescendingContains() {
+ // (5, -5), -3 = 2, -1, -4
+ LongRange range = Ranges.longRange(5, BoundType.OPEN, -5, BoundType.OPEN,
+ -3);
+ // (5, -5), -3 = 2, -1, -4
+ List<Long> arr = Arrays.asList(2L, -1L, -4L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsSingleStepDescending() {
+ // [2, -2), -1 = 2, 1, 0, -1
+ LongRange descendingRange = Ranges.longRange(2, BoundType.CLOSED, -2,
+ BoundType.OPEN, -1);
+ // [2, -2), -1 = 2, 1, 0, -1
+ List<Long> arr = Arrays.asList(2L, 1L, 0L, -1L);
+ for (Long element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + descendingRange +
+ "]",
+ descendingRange.contains(element));
+ }
+ List<Long> elementsNotPresent = new ArrayList<Long>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Long element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + descendingRange +
+ "]",
+ descendingRange.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsNullOrEmpty() {
+ LongRange range = Ranges.longRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ assertFalse(range.contains(null));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testContainsAll() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ LongRange range = Ranges.longRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ List<Long> list = Arrays.asList(-1L, 0L, 1L, 2L);
+ assertTrue("Range [" + range +
+ "] was expected to contain all elements from list [" + list +
+ "]", range.containsAll(list));
+ List<Long> listWithExtraElements = Arrays.asList(2L, -1L, 0L, 1L, 2L,
+ 3L);
+ assertFalse("Range [" + range + "] has more elements than expected",
+ range.containsAll(listWithExtraElements));
+ assertFalse(range.containsAll(null));
+ assertFalse(range.containsAll(Collections.EMPTY_LIST));
+ }
+
+ @Test
+ public void testEquals()
+ throws Exception {
+ // equals basic properties
+ LongRange range = Ranges.longRange(-2, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertEquals("equals must be reflexive", range, range);
+ assertEquals("hashCode must be reflexive", range.hashCode(),
+ range.hashCode());
+ assertTrue(!range.equals(null)); // should be able to compare to null
+
+ Object range2 = Ranges.longRange(-2, BoundType.CLOSED, 2, BoundType.OPEN,
+ 1);
+ if (range.equals(range2)) {
+ assertEquals("equals implies hash equals", range.hashCode(),
+ range2.hashCode());
+ assertEquals("equals must be symmetric", range2, range);
+ } else {
+ assertTrue("equals must be symmetric", !range2.equals(range));
+ }
+
+ // Changing attributes
+ Object range3 = Ranges.longRange(-1, BoundType.CLOSED, 2, BoundType.OPEN,
+ 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range3));
+
+ Object range4 = Ranges.longRange(-2, BoundType.OPEN, 2, BoundType.OPEN, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range4));
+
+ Object range5 = Ranges.longRange(-2, BoundType.CLOSED, 1, BoundType.OPEN,
+ 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range5));
+
+ Object range6 = Ranges.longRange(-2, BoundType.CLOSED, 2,
+ BoundType.CLOSED, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range6));
+
+ Object range7 = Ranges.longRange(-2, BoundType.CLOSED, 2, BoundType.OPEN,
+ 2);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range7));
+
+ // Using different constructors
+ LongRange range8 = Ranges.longRange(Integer.valueOf(-2), Integer.valueOf(2),
+ Integer.valueOf(1));
+ assertEquals("Invalid equals using different constructor", range,
+ range8);
+
+ LongRange range9 = Ranges.longRange(Integer.valueOf(-2),Integer.valueOf(2));
+ assertEquals("Invalid equals using different constructor", range,
+ range9);
+
+ Endpoint<Long> leftEndpoint = new Endpoint<Long>(-2L, BoundType.CLOSED);
+ Endpoint<Long> rightEndpoint = new Endpoint<Long>(2L, BoundType.OPEN);
+ LongRange range10 = Ranges.longRange(leftEndpoint, rightEndpoint, 1L);
+ assertEquals("Invalid equals using different constructor", range,
+ range10);
+ }
+
+ @Test
+ public void testToString() {
+ LongRange range = Ranges.longRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ assertEquals("Wrong string value", "LongRange<(-2, 2], 1>",
+ range.toString());
+ }
+
+ @Test
+ public void testConstructorUsingSameEndpoint() {
+ Endpoint<Long> uniqueEndpoint = new Endpoint<Long>(10L,
+ BoundType.CLOSED);
+ try {
+ Ranges.longRange(uniqueEndpoint, uniqueEndpoint, 1L);
+ } catch (IllegalArgumentException e) {
+ fail("Not expected to get here");
+ }
+ }
+
+ @Test
+ public void testInvalidRange() {
+ try {
+ Ranges.longRange(10, BoundType.OPEN, -5, BoundType.CLOSED, 10);
+ fail("Not expected to get here");
+ } catch (IllegalArgumentException e) {
+ // Do nothing
+ }
+ Endpoint<Long> leftEndpoint = new Endpoint<Long>(10L, BoundType.CLOSED);
+ Endpoint<Long> rightEndpoint = new Endpoint<Long>(-5L, BoundType.OPEN);
+ try {
+ Ranges.longRange(leftEndpoint, rightEndpoint, 1L);
+ fail("Not expected to get here");
+ } catch (IllegalArgumentException e) {
+ // Do nothing
+ }
+ }
@Test
- public void testEquals() {
- LongRange range = new LongRange(1, 5);
- assertObjectsAreEqual(range, range);
- assertObjectsAreEqual(range, new LongRange(1, 5));
- assertObjectsAreEqual(range, new LongRange(1, 5, 1));
- assertObjectsAreEqual(range, new LongRange(new Integer(1), new Long(5)));
- assertObjectsAreEqual(range, new LongRange(new Long(1), new Short((short)5), new Long(1)));
- assertObjectsAreNotEqual(range, new LongRange(2, 5));
- assertObjectsAreNotEqual(range, new LongRange(1, 6));
- assertObjectsAreNotEqual(range, new LongRange(1, 5, 2));
+ public void testDefaultStep() {
+ assertEquals("Invalid default step", Long.valueOf(-1L),
+ LongRange.DEFAULT_STEP.evaluate(10L, 1L));
+ assertEquals("Invalid default step", Long.valueOf(1L),
+ LongRange.DEFAULT_STEP.evaluate(1L, 10L));
}
}
\ No newline at end of file
Modified: commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/util/TestEachElement.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/util/TestEachElement.java?rev=1439123&r1=1439120&r2=1439123&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/util/TestEachElement.java (original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/core/src/test/java/org/apache/commons/functor/generator/util/TestEachElement.java Sun Jan 27 17:32:23 2013
@@ -30,10 +30,10 @@ import java.util.Map;
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;
+import org.apache.commons.functor.generator.loop.GenerateUntil;
+import org.apache.commons.functor.generator.loop.GenerateWhile;
+import org.apache.commons.functor.generator.loop.UntilGenerate;
+import org.apache.commons.functor.generator.loop.WhileGenerate;
import org.junit.Before;
import org.junit.Test;
Propchange: commons/proper/functor/branches/FUNCTOR-14-mm/src/changes/
------------------------------------------------------------------------------
--- svn:mergeinfo (added)
+++ svn:mergeinfo Sun Jan 27 17:32:23 2013
@@ -0,0 +1 @@
+/commons/proper/functor/branches/generators-FUNCTOR-14/src/changes:1385335
Modified: commons/proper/functor/branches/FUNCTOR-14-mm/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/src/changes/changes.xml?rev=1439123&r1=1439120&r2=1439123&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/src/changes/changes.xml (original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/src/changes/changes.xml Sun Jan 27 17:32:23 2013
@@ -47,6 +47,9 @@
<action issue="FUNCTOR-15" dev="simonetripodi" due-to="Bruno P. Kinoshita">
Distribute tests from TestAlgorithms into several test classes under algorithm test package
</action>
+ <action issue="FUNCTOR-14" dev="kinow">
+ Enhancements on the Generator API
+ </action>
<action issue="FUNCTOR-9" dev="simonetripodi" due-to="Bruno P. Kinoshita">
Make Limit and Offset Serializable
</action>
Propchange: commons/proper/functor/branches/FUNCTOR-14-mm/src/site/
------------------------------------------------------------------------------
--- svn:mergeinfo (added)
+++ svn:mergeinfo Sun Jan 27 17:32:23 2013
@@ -0,0 +1 @@
+/commons/proper/functor/branches/generators-FUNCTOR-14/src/site:1385335
Modified: commons/proper/functor/branches/FUNCTOR-14-mm/src/site/xdoc/examples.xml
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/FUNCTOR-14-mm/src/site/xdoc/examples.xml?rev=1439123&r1=1439120&r2=1439123&view=diff
==============================================================================
--- commons/proper/functor/branches/FUNCTOR-14-mm/src/site/xdoc/examples.xml (original)
+++ commons/proper/functor/branches/FUNCTOR-14-mm/src/site/xdoc/examples.xml Sun Jan 27 17:32:23 2013
@@ -29,7 +29,8 @@
This page contains basic examples using <a href="apidocs/org/apache/commons/functor/Predicate.html">Predicates</a>,
<a href="apidocs/org/apache/commons/functor/Function.html">Functions</a>,
<a href="apidocs/org/apache/commons/functor/Procedure.html">Procedures</a>,
- <a href="apidocs/org/apache/commons/functor/generator/Generator.html">Generators</a> and
+ <a href="apidocs/org/apache/commons/functor/generator/Generator.html">Generators</a>,
+ <a href="apidocs/org/apache/commons/functor/generator/range/Range.html">Ranges</a> and
<a href="apidocs/org/apache/commons/functor/aggregator/Aggregator.html">Aggregators</a>.
There are also examples using composition and more practical examples
at the bottom of this page.
@@ -105,6 +106,129 @@ for( Integer number : numbers ) {
</p>
</subsection>
+ <subsection name="Generators">
+ <p>
+ Apache Functor includes other objects that you can can use to code in
+ a less imperative way, like <em>Generators</em>. In the following
+ example, we create an <em>Integer Generator</em> that generates
+ integers from 1 to 4 (the right argument is non-inclusive). The
+ generator is wrapped within a <em>Filtered Generator</em> that applies
+ the isEven predicate to each integer generated by the former generator.
+ Finally, we execute a <em>Composite Unary Procedure</em> that uses
+ a function to double the value of the integer before printing it.
+ </p>
+<source>
+Generator<Integer> integerGenerator = new IntegerRange(1, 5); // inclusive, exclusive
+
+UnaryPredicate<Integer> isEven = new UnaryPredicate<Integer>() {
+ public boolean test(Integer obj) {
+ return obj % 2 == 0;
+ }
+};
+
+FilteredGenerator<Integer> filteredGenerator =
+ new FilteredGenerator<Integer>(integerGenerator, isEven);
+
+UnaryFunction<Integer, Integer> doubler = new UnaryFunction<Integer, Integer>() {
+ public Integer evaluate(Integer obj) {
+ return obj * 2;
+ }
+};
+
+UnaryProcedure<Integer> print = new UnaryProcedure<Integer>() {
+ public void run(Integer obj) {
+ System.out.print(obj + " ");
+ }
+};
+
+CompositeUnaryProcedure<Integer> compositeProcedure =
+ new CompositeUnaryProcedure<Integer>(print);
+
+filteredGenerator.run(compositeProcedure.of(doubler));
+</source>
+ <p>
+ The <a href="http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/lines/">lines</a>
+ package demonstrates a functional approach to IO using Generators and the Algorithms class.
+ </p>
+ </subsection>
+
+ <subsection name="Ranges">
+ <p>
+ Using <em>Ranges</em> you are able to create a series of elements of
+ a certain type. The distance between each element is called
+ <em>step</em>. And the <em>left</em> and <em>right</em> limits are
+ <em>endpoints</em>.
+ </p>
+ <p>
+ By default, in numeric ranges the left value is inclusive and the
+ right is exclusive. The range below creates a series of Integers
+ between <em>0</em> and <em>10</em>, with a default step of <em>1</em>.
+ </p>
+<source>
+// [0, 10), 1 = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+IntegerRange range = new IntegerRange(0, 10);
+</source>
+ <p>
+ The step value can be changed, altering the distance between each
+ element in the range. As shown below.
+ </p>
+<source>
+// [0, 10), 2 = 0, 2, 4, 6, 8
+IntegerRange range = new IntegerRange(0, 10, 2);
+</source>
+ <p>
+ It is possible, too, to define the <em>bound type</em> of each endpoint.
+ It is similar to mathematical intervals, where a
+ <em>closed</em> endpoint means that the value is included in the
+ series of elements in the range. On the other hand, an <em>open</em>
+ endpoint means that the value is not included.
+ </p>
+<source>
+// (0, 10], 2 = 2, 4, 6, 8, 10
+IntegerRange range = new IntegerRange(0, BoundType.OPEN, 10, BoundType.CLOSED, 2);
+</source>
+ <p>
+ A Range is also by nature a Generator, so you can use it for executing
+ procedures for each of its elements.
+ </p>
+<source>
+// [0, 10), 1 = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+IntegerRange range = new IntegerRange(0, 10);
+UnaryProcedure<Integer> printProcedure = new UnaryProcedure<Integer>() {
+
+ public void run(Integer obj) {
+ System.out.print(obj + " ");
+ }
+};
+range.run(printProcedure);
+</source>
+ <p>
+ The code above produces the following output: 0 1 2 3 4 5 6 7 8 9
+ </p>
+ </subsection>
+
+ <subsection name="Aggregators">
+ <p>
+ There are some code snippets / examples for the <code>org.apache.commons.functor.aggregator</code> package
+ available on <a href="aggregator.html">this page</a>. Also, to exemplify the usage of the <code>Aggregator</code>
+ classes, there are code examples in the test section.
+ </p>
+ <p>
+ First such set of example involves the usage of the <i>nostore</i> <code>Aggregator</code>. Code can be found in
+ <a href="http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/">org.apache.commons.functor.example.aggregator.nostore</a>.
+ This shows how can you use an aggregator which doesn't store the data series and processes them on the fly.
+ Also, there are examples provided which show how can you implement your own aggregation function
+ to be used with this <code>Aggregator</code> type.
+ </p>
+ <p>
+ For using an <code>Aggregator</code> which stores the data series in a list, examples are in
+ <a href="http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/">org.apache.commons.functor.example.aggregator.list</a>.
+ This shows how can you use the <code>ArrayList</code>-backed aggregator or provide your own <code>List</code>-based implementation.
+ Also, there are examples provided which show how can you implement your own aggregation function
+ to be used with this <code>Aggregator</code> type.
+ </p>
+ </subsection>
+
<subsection name="Reuse Through Composition">
<p>
The <em>Functor</em> package, and more generally, a functional approach
@@ -179,74 +303,6 @@ for( Integer number : numbers ) {
</p>
</subsection>
- <subsection name="Generators">
- <p>
- Apache Functor includes other objects that you can can use to code in
- a less imperative way, like <em>Generators</em>. In the following
- example, we create an <em>Integer Generator</em> that generates
- integers from 1 to 4 (the right argument is non-inclusive). The
- generator is wrapped within a <em>Filtered Generator</em> that applies
- the isEven predicate to each integer generated by the former generator.
- Finally, we execute a <em>Composite Unary Procedure</em> that uses
- a function to double the value of the integer before printing it.
- </p>
-<source>
-Generator<Integer> integerGenerator = new IntegerRange(1, 5); // inclusive, exclusive
-
-UnaryPredicate<Integer> isEven = new UnaryPredicate<Integer>() {
- public boolean test(Integer obj) {
- return obj % 2 == 0;
- }
-};
-
-FilteredGenerator<Integer> filteredGenerator =
- new FilteredGenerator<Integer>(integerGenerator, isEven);
-
-UnaryFunction<Integer, Integer> doubler = new UnaryFunction<Integer, Integer>() {
- public Integer evaluate(Integer obj) {
- return obj * 2;
- }
-};
-
-UnaryProcedure<Integer> print = new UnaryProcedure<Integer>() {
- public void run(Integer obj) {
- System.out.print(obj + " ");
- }
-};
-
-CompositeUnaryProcedure<Integer> compositeProcedure =
- new CompositeUnaryProcedure<Integer>(print);
-
-filteredGenerator.run(compositeProcedure.of(doubler));
-</source>
- <p>
- The <a href="http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/lines/">lines</a>
- package demonstrates a functional approach to IO using Generators and the Algorithms class.
- </p>
- </subsection>
-
- <subsection name="Aggregators">
- <p>
- There are some code snippets / examples for the <code>org.apache.commons.functor.aggregator</code> package
- available on <a href="aggregator.html">this page</a>. Also, to exemplify the usage of the <code>Aggregator</code>
- classes, there are code examples in the test section.
- </p>
- <p>
- First such set of example involves the usage of the <i>nostore</i> <code>Aggregator</code>. Code can be found in
- <a href="http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/">org.apache.commons.functor.example.aggregator.nostore</a>.
- This shows how can you use an aggregator which doesn't store the data series and processes them on the fly.
- Also, there are examples provided which show how can you implement your own aggregation function
- to be used with this <code>Aggregator</code> type.
- </p>
- <p>
- For using an <code>Aggregator</code> which stores the data series in a list, examples are in
- <a href="http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/">org.apache.commons.functor.example.aggregator.list</a>.
- This shows how can you use the <code>ArrayList</code>-backed aggregator or provide your own <code>List</code>-based implementation.
- Also, there are examples provided which show how can you implement your own aggregation function
- to be used with this <code>Aggregator</code> type.
- </p>
- </subsection>
-
<subsection name="Code Katas">
<p>
"Pragmatic" Dave Thomas has been