You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/05/31 16:56:50 UTC
svn commit: r1344757 [4/4] - in /commons/proper/functor/trunk: ./
src/changes/ src/main/java/org/apache/commons/functor/aggregator/
src/main/java/org/apache/commons/functor/aggregator/functions/
src/site/xdoc/ src/test/java/org/apache/commons/functor/a...
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntPercentileAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntPercentileAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntPercentileAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntPercentileAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.IntegerPercentileAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IntegerPercentileAggregatorFunction}.
+ */
+public class IntPercentileAggregatorFunctionTest extends BaseFunctorTest {
+ private static final double DELTA = 0.01; // make room for some poor
+ // floating point support
+
+ private static final double DEF_PERC = 90; // by default use the 90th
+ // percentile
+
+ @Override
+ protected Object makeFunctor() throws Exception {
+ return new IntegerPercentileAggregatorFunction(DEF_PERC);
+ }
+
+ @Test
+ public void testCreate() throws Exception {
+ // default
+ IntegerPercentileAggregatorFunction fct = (IntegerPercentileAggregatorFunction) makeFunctor();
+ assertTrue(fct.isUseCopy());
+ assertEquals(fct.getPercentile(), DEF_PERC, DELTA);
+
+ // different percentage
+ double perc = 50.0;
+ fct = new IntegerPercentileAggregatorFunction(perc);
+ assertTrue(fct.isUseCopy());
+ assertEquals(fct.getPercentile(), perc, DELTA);
+
+ // use copy
+ fct = new IntegerPercentileAggregatorFunction(perc, false);
+ assertFalse(fct.isUseCopy());
+ assertEquals(fct.getPercentile(), perc, DELTA);
+
+ // test illegal exc
+ boolean exc = false;
+ fct = null;
+ try {
+ fct = new IntegerPercentileAggregatorFunction(-100);
+ } catch (IllegalArgumentException e) {
+ exc = true;
+ }
+ assertTrue(exc);
+
+ fct = null;
+ exc = false;
+ try {
+ fct = new IntegerPercentileAggregatorFunction(101);
+ } catch (IllegalArgumentException e) {
+ exc = true;
+ }
+ assertTrue(exc);
+ }
+
+ @Test
+ public void testComputeRank() throws Exception {
+ List<Integer> data = new ArrayList<Integer>();
+ data.add(0);
+ data.add(1);
+ data.add(2);
+ data.add(3);
+ data.add(4);
+ // size of list is 5 now
+
+ // first item
+ IntegerPercentileAggregatorFunction fct = new IntegerPercentileAggregatorFunction(0.0);
+ int rank = fct.computeRank(data);
+ assertEquals(rank, 0);
+ assertEquals(data.get(rank).intValue(), 0);
+ // last item
+ fct = new IntegerPercentileAggregatorFunction(100.0);
+ rank = fct.computeRank(data);
+ assertEquals(rank, data.size() - 1);
+ assertEquals(data.get(rank).intValue(), 4);
+
+ // middle one
+ fct = new IntegerPercentileAggregatorFunction(50.0);
+ rank = fct.computeRank(data);
+ assertEquals(rank, 2);
+ assertEquals(data.get(rank).intValue(), 2);
+
+ // 40% = 2nd item
+ fct = new IntegerPercentileAggregatorFunction(40.0);
+ rank = fct.computeRank(data);
+ assertEquals(rank, 1);
+ assertEquals(data.get(rank).intValue(), 1);
+
+ // 80% = 4th item
+ fct = new IntegerPercentileAggregatorFunction(80.0);
+ rank = fct.computeRank(data);
+ assertEquals(rank, 3);
+ assertEquals(data.get(rank).intValue(), 3);
+
+ // in between (e.g. 70%) means 3rd item
+ fct = new IntegerPercentileAggregatorFunction(70.0);
+ rank = fct.computeRank(data);
+ assertEquals(rank, 2);
+ assertEquals(data.get(rank).intValue(), 2);
+
+ // but 75% means 4th item
+ fct = new IntegerPercentileAggregatorFunction(75.0);
+ rank = fct.computeRank(data);
+ assertEquals(rank, 3);
+ assertEquals(data.get(rank).intValue(), 3);
+ }
+
+ @Test
+ public void testEvaluateNullEmpty() throws Exception {
+ IntegerPercentileAggregatorFunction fct = (IntegerPercentileAggregatorFunction) makeFunctor();
+ List<Integer> data = null;
+ Integer d = fct.evaluate(data);
+ assertNull(d);
+ data = new ArrayList<Integer>();
+ d = fct.evaluate(data);
+ assertNull(d);
+ }
+
+ @Test
+ public void testEvaluateNoCopy() throws Exception {
+ // using a copy
+ IntegerPercentileAggregatorFunction fct = (IntegerPercentileAggregatorFunction) makeFunctor();
+ List<Integer> data = new ArrayList<Integer>();
+ data.add(4);
+ data.add(3);
+ data.add(2);
+ data.add(1);
+ data.add(0);
+ // size of list is 5 now
+
+ Integer d = fct.evaluate(data);
+ assertEquals(d.intValue(), 3);
+ assertEquals(data.get(0).intValue(), 4);
+ assertEquals(data.get(1).intValue(), 3);
+ assertEquals(data.get(2).intValue(), 2);
+ assertEquals(data.get(3).intValue(), 1);
+ assertEquals(data.get(4).intValue(), 0);
+
+ // using a copy (explicitly)
+ fct = new IntegerPercentileAggregatorFunction(DEF_PERC, true);
+ d = fct.evaluate(data);
+ assertEquals(d.intValue(), 3);
+ assertEquals(data.get(0).intValue(), 4);
+ assertEquals(data.get(1).intValue(), 3);
+ assertEquals(data.get(2).intValue(), 2);
+ assertEquals(data.get(3).intValue(), 1);
+ assertEquals(data.get(4).intValue(), 0);
+
+ // operate on the list directly
+ fct = new IntegerPercentileAggregatorFunction(DEF_PERC, false);
+ d = fct.evaluate(data);
+ assertEquals(d.intValue(), 3);
+ assertEquals(data.get(4).intValue(), 4);
+ assertEquals(data.get(3).intValue(), 3);
+ assertEquals(data.get(2).intValue(), 2);
+ assertEquals(data.get(1).intValue(), 1);
+ assertEquals(data.get(0).intValue(), 0);
+ }
+}
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntPercentileAggregatorFunctionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntPercentileAggregatorFunctionTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntPercentileAggregatorFunctionTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorBinaryFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorBinaryFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorBinaryFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorBinaryFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Random;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.IntegerSumAggregatorBinaryFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IntegerSumAggregatorBinaryFunction}.
+ */
+public class IntSumAggregatorBinaryFunctionTest extends BaseFunctorTest {
+ @Override
+ protected Object makeFunctor() throws Exception {
+ return new IntegerSumAggregatorBinaryFunction();
+ }
+
+ @Test
+ public void testNulls() throws Exception {
+ IntegerSumAggregatorBinaryFunction fct = (IntegerSumAggregatorBinaryFunction) makeFunctor();
+ Integer d = fct.evaluate(null, null);
+ assertNull(d);
+ d = fct.evaluate(null, 1);
+ assertEquals(1, d.intValue());
+ d = fct.evaluate(2, null);
+ assertEquals(2, d.intValue());
+ }
+
+ @Test
+ public void testSum() throws Exception {
+ IntegerSumAggregatorBinaryFunction fct = (IntegerSumAggregatorBinaryFunction) makeFunctor();
+ int total = 0;
+ int result = 0;
+ int calls = 31;
+ Random rnd = new Random();
+ for (int i = 0; i < calls; i++) {
+ int number = rnd.nextInt();
+ total += number;
+ result = fct.evaluate(result, number);
+ assertEquals(result, total);
+ }
+ }
+}
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.IntegerSumAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IntegerSumAggregatorFunction}.
+ */
+public class IntSumAggregatorFunctionTest extends BaseFunctorTest {
+ @Override
+ protected Object makeFunctor() throws Exception {
+ return new IntegerSumAggregatorFunction();
+ }
+
+ @Test
+ public void testEmptyList() throws Exception {
+ IntegerSumAggregatorFunction fct = (IntegerSumAggregatorFunction) makeFunctor();
+ List<Integer> lst = null;
+ Integer res = fct.evaluate(lst);
+ assertNull(res);
+ lst = new ArrayList<Integer>();
+ res = fct.evaluate(lst);
+ assertNull(res);
+ }
+
+ @Test
+ public void testSum() throws Exception {
+ IntegerSumAggregatorFunction fct = (IntegerSumAggregatorFunction) makeFunctor();
+ List<Integer> lst = new ArrayList<Integer>();
+ lst.add(0);
+ int res = fct.evaluate(lst);
+ assertEquals(res, 0);
+ lst.add(1);
+ res = fct.evaluate(lst);
+ assertEquals(res, 1);
+ lst.add(2);
+ res = fct.evaluate(lst);
+ assertEquals(res, 3);
+ // finally carry out a random addition
+ lst.clear();
+ int calls = 31;
+ int total = 0;
+ Random rnd = new Random();
+ for (int i = 0; i < calls; i++) {
+ int random = rnd.nextInt();
+ lst.add(random);
+ total += random;
+ res = fct.evaluate(lst);
+ assertEquals(res, total);
+ }
+ }
+}
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorFunctionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorFunctionTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntSumAggregatorFunctionTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnFunctionImplementationSample.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnFunctionImplementationSample.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnFunctionImplementationSample.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnFunctionImplementationSample.java Thu May 31 14:56:47 2012
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.example.aggregator.list;
+
+import java.util.List;
+
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.aggregator.ArrayListBackedAggregator;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Shows how to implement own aggregation function with a List-backed
+ * aggregator. In this example we want to monitor whether a certain value
+ * appears in the list or not.
+ */
+public class OwnFunctionImplementationSample {
+ @Test
+ public void findValue() throws Exception {
+ // we're looking for 10
+ ArrayListBackedAggregator<Integer> agg = new ArrayListBackedAggregator<Integer>(new OwnFunction(10));
+ int eval = agg.evaluate();
+ assertEquals(eval, -1);
+
+ agg.add( 1 );
+ eval = agg.evaluate();
+ assertEquals(eval, -1);
+
+ agg.add( 2 );
+ eval = agg.evaluate();
+ assertEquals(eval, -1);
+
+ agg.add( 10 );
+ eval = agg.evaluate();
+ assertEquals(eval, 2);
+ //function finds FIRST occurence!
+ agg.add( 10 );
+ eval = agg.evaluate();
+ assertEquals(eval, 2);
+ agg.add( 10 );
+ eval = agg.evaluate();
+ assertEquals(eval, 2);
+ agg.add( 10 );
+ eval = agg.evaluate();
+ assertEquals(eval, 2);
+ }
+
+ /**
+ * This function returns the index of the first occurrence in the list of
+ * the given value.
+ */
+ static class OwnFunction implements UnaryFunction<List<Integer>, Integer> {
+ /** Value to find in the list. */
+ private int valueToFind;
+
+ public OwnFunction(int valueToFind) {
+ this.valueToFind = valueToFind;
+ }
+
+ /**
+ * Search in the list and find the first index of the given value.
+ *
+ * @param lst
+ * List to search in
+ * @return index of {@link #valueToFind} if found or -1 if value not
+ * present in the list
+ */
+ public Integer evaluate(List<Integer> lst) {
+ if (lst == null || lst.size() == 0) {
+ return -1;
+ }
+ for (int i = 0; i < lst.size(); i++) {
+ if (lst.get(i).intValue() == valueToFind) {
+ return i;
+ }
+ }
+ return -1;
+ }
+ }
+}
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnFunctionImplementationSample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnFunctionImplementationSample.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnFunctionImplementationSample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnListImplementationSample.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnListImplementationSample.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnListImplementationSample.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnListImplementationSample.java Thu May 31 14:56:47 2012
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.example.aggregator.list;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.aggregator.AbstractListBackedAggregator;
+import org.apache.commons.functor.aggregator.functions.IntegerSumAggregatorFunction;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Class which shows how to provide a custom List implementation with an
+ * instance of {@link AbstractListBackedAggregator}. This particular instance
+ * uses a LinkedList.
+ */
+public class OwnListImplementationSample {
+ @Test
+ public void sampleCreateOwnList() {
+ CustomListAggregator<Integer> agg = new CustomListAggregator<Integer>(new IntegerSumAggregatorFunction());
+ int eval = agg.evaluate();
+ assertEquals(eval, 0);
+ agg.add(2);
+ agg.add(3);
+ eval = agg.evaluate();
+ assertEquals(eval, 5);
+ assertEquals(agg.getDataSize(), 2); // 2 items added
+ }
+
+ /**
+ * List-backed aggregator which uses a LinkedList.
+ *
+ * @param <T>
+ * type of parameter stored.
+ */
+ static class CustomListAggregator<T> extends AbstractListBackedAggregator<T> {
+ public CustomListAggregator(UnaryFunction<List<T>, T> aggregationFunction) {
+ super(aggregationFunction);
+ }
+
+ public CustomListAggregator(UnaryFunction<List<T>, T> aggregationFunction, long interval) {
+ super(aggregationFunction, interval);
+ }
+
+ public CustomListAggregator(UnaryFunction<List<T>, T> aggregationFunction, long interval,
+ boolean useSharedTimer) {
+ super(aggregationFunction, interval, useSharedTimer);
+ }
+
+ @Override
+ protected List<T> createList() {
+ return new LinkedList<T>();
+ }
+ }
+}
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnListImplementationSample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnListImplementationSample.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/OwnListImplementationSample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/package-info.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/package-info.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/package-info.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/package-info.java Thu May 31 14:56:47 2012
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * Package which groups a set of examples for list-backed {@link org.apache.commons.functor.aggregator.Aggregator}.
+ */
+package org.apache.commons.functor.example.aggregator.list;
+
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/package-info.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/list/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/AggregatedFunctionSample.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/AggregatedFunctionSample.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/AggregatedFunctionSample.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/AggregatedFunctionSample.java Thu May 31 14:56:47 2012
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.functor.example.aggregator.nostore;
+
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.aggregator.AbstractNoStoreAggregator;
+
+/**
+ * Shows how to implement own aggregated function to use with
+ * {@link AbstractNoStoreAggregator}.
+ */
+public class AggregatedFunctionSample {
+ /**
+ * Uses a custom function together with a nostore aggregator to provide a
+ * continuous logical OR in between all the values added to the aggregator.
+ */
+ public void useOwnFunction() throws Exception {
+ AbstractNoStoreAggregator<Boolean> or = new AbstractNoStoreAggregator<Boolean>( new OwnBinaryFunction() ) {
+ @Override
+ protected Boolean initialValue() {
+ return false;
+ }
+ };
+ or.add( false );
+ System.out.println( "OR : " + or.evaluate() );
+ or.add( true );
+ System.out.println( "OR : " + or.evaluate() );
+ or.add( false );
+ System.out.println( "OR : " + or.evaluate() );
+ }
+
+ /**
+ * This class implements a logical OR: it OR's the 2 parameters passed in
+ * and returns the result.
+ * (There are similar implementations already in functor, this is just to
+ * be used as an example for doing this with a nostore aggregator.
+ */
+ static class OwnBinaryFunction implements BinaryFunction<Boolean, Boolean, Boolean> {
+ public Boolean evaluate(Boolean left, Boolean right) {
+ return left.booleanValue() || right.booleanValue();
+ }
+ }
+}
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/AggregatedFunctionSample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/AggregatedFunctionSample.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/AggregatedFunctionSample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/NoStoreAggregatorSample.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/NoStoreAggregatorSample.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/NoStoreAggregatorSample.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/NoStoreAggregatorSample.java Thu May 31 14:56:47 2012
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.functor.example.aggregator.nostore;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.functor.aggregator.AbstractNoStoreAggregator;
+import org.apache.commons.functor.aggregator.AbstractTimedAggregator;
+import org.apache.commons.functor.aggregator.TimedAggregatorListener;
+import org.apache.commons.functor.aggregator.functions.IntegerCountAggregatorBinaryFunction;
+import org.apache.commons.functor.aggregator.functions.IntegerMaxAggregatorBinaryFunction;
+import org.apache.commons.functor.aggregator.functions.IntegerSumAggregatorBinaryFunction;
+import org.junit.Test;
+
+/**
+ * Examples of using a {@link AbstractNoStoreAggregator}.
+ */
+public class NoStoreAggregatorSample {
+ /**
+ * Shows how to use a simple <i>nostore</i> aggregator to just count number
+ * of times data is added.
+ */
+ @Test
+ public void countItems() {
+ // no timer used
+ AbstractNoStoreAggregator<Integer> counter = new AbstractNoStoreAggregator<Integer>(
+ new IntegerCountAggregatorBinaryFunction()) {
+ @Override
+ protected Integer initialValue() {
+ return 0;
+ }
+ };
+ counter.add(100);
+ int eval = counter.evaluate();
+ // result of evaluate() is 1 as we only added 1 item
+ assertEquals(eval, 1);
+ counter.add(1);
+ counter.add(2);
+ eval = counter.evaluate();
+ // result is now 3 as we added 2 more items
+ assertEquals(eval, 3);
+
+ // since there is no timer, we need to reset the data manually
+ counter.reset();
+ eval = counter.evaluate();
+ assertEquals(eval, 0);
+ }
+
+ /**
+ * Shows how to sum up all the items passed in and "flush" them regularly on
+ * a timer. When the flushing occurs, we print the evaluation to console.
+ */
+ @Test
+ public void sumItems() throws Exception {
+ // prepare the listener
+ TimedAggregatorListener<Integer> listener = new TimedAggregatorListener<Integer>() {
+ public void onTimer(AbstractTimedAggregator<Integer> aggregator, Integer evaluation) {
+ System.out.println("Current sum is :" + evaluation);
+ }
+ };
+ // flush data regularly every 1/2 second
+ AbstractNoStoreAggregator<Integer> sum = new AbstractNoStoreAggregator<Integer>(
+ new IntegerSumAggregatorBinaryFunction(), 500) {
+ @Override
+ protected Integer initialValue() {
+ return 0;
+ }
+ };
+ sum.addTimerListener(listener);
+
+ long startTime = System.currentTimeMillis();
+ long stopAfter = 5000; // 5 seconds
+ int maxInt = 100;
+ Random rnd = new Random();
+ while (System.currentTimeMillis() - startTime <= stopAfter) {
+ sum.add(rnd.nextInt(maxInt));
+ TimeUnit.MILLISECONDS.sleep(rnd.nextInt(maxInt));
+ }
+ sum.stop();
+ }
+
+ /**
+ * Calculate the maximum value of a series of numbers.
+ */
+ @Test
+ public void maxValue() throws Exception {
+ // no timer
+ AbstractNoStoreAggregator<Integer> sum = new AbstractNoStoreAggregator<Integer>(
+ new IntegerMaxAggregatorBinaryFunction()) {
+ @Override
+ protected Integer initialValue() {
+ return 0;
+ }
+ };
+
+ long startTime = System.currentTimeMillis();
+ long stopAfter = 5000; // 5 seconds
+ int maxInt = 100;
+ Random rnd = new Random();
+ while (System.currentTimeMillis() - startTime <= stopAfter) {
+ sum.add(rnd.nextInt());
+ System.out.println( "Max so far is " + sum.evaluate());
+ TimeUnit.MILLISECONDS.sleep(rnd.nextInt(maxInt));
+ }
+ sum.stop();
+ }
+}
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/NoStoreAggregatorSample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/NoStoreAggregatorSample.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/NoStoreAggregatorSample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/package-info.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/package-info.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/package-info.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/package-info.java Thu May 31 14:56:47 2012
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Package which groups a set of examples for {@link org.apache.commons.functor.aggregator.AbstractNoStoreAggregator}.
+ */
+package org.apache.commons.functor.example.aggregator.nostore;
+
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/package-info.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/nostore/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/package-info.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/package-info.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/package-info.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/package-info.java Thu May 31 14:56:47 2012
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * Package which groups a set of examples for {@link org.apache.commons.functor.aggregator.Aggregator}.
+ */
+package org.apache.commons.functor.example.aggregator;
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/package-info.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/example/aggregator/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain