Hello,
I think I'm still a Velocity committer, but I've been inactive for so long I'd rather contribute like a non-committer for a while.
For one of our projects I created an Aggregate Tool. This tool takes a list of numbers and calculates the sum/average for the list of numbers.
The tool can also take a list of beans and a field name. It will then extract the numeric values for the field and add them together.
~ Leon
/* * Copyright 2004 The Apache Software Foundation. * * Licensed 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.velocity.tools.generic;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.beanutils.BeanUtils;
/**
* A tool that gives template writes the ability to get totals and averages
* of lists of objects.
*
* <p><b>Example usage:</b>
* <pre>
*
* java...
* List l = new ArrayList();
* for (int i=0; i<3; i++) {
* MyBean bean = new MyBean();
* bean.setField (i);
* bean.setOtherField ("abc");
* l.add (bean);
* }
* context.put("list",l);
*
* template...
* $aggregate.getTotal($list,"field")
* $aggregate.getAverage($list,"field);
* ## Tip: Use
*
* output...
* 3
* 1.5
*
* </pre><p>
*
* @since Velocity Tools 1.
* @version $$
*
*/
public class AggregateTool {
/**
* Get the sum of the values from a list
*
* @param collection A collection containing Java beans
* @param field A Java Bean field for the objects in <i>collection</i> that
* will retrun a number.
*
* @return The sum of the values in <i>collection</i>.
*
* @throws Exception
*/
public double getTotal (Collection collection, String field) throws Exception {
double result = 0;
for (Iterator iter = collection.iterator(); iter.hasNext();) {
Object object = (Object)iter.next();
String value = BeanUtils.getProperty(object, field);
result += Double.parseDouble(value);
}
return result;
}
/**
* Get the average of the values from a list
*
* @param collection A collection containing Java beans
* @param field A Java Bean field for the objects in <i>collection</i> that
* will retrun a number.
*
* @return The average of the values in <i>collection</i>.
*
* @throws Exception
*/
public double getAverage (Collection collection, String field) throws Exception {
double result = getTotal(collection, field);
return result / collection.size();
}
/**
* Get the sum of the values from a list
*
* @param collection A collection containing Java beans
* @param field A Java Bean field for the objects in <i>array</i> that
* will retrun a number.
*
* @return The sum of the values in <i>array</i>.
*
* @throws Exception
*/
public double getTotal (Object[] array, String field) throws Exception {
return getTotal (Arrays.asList(array),field);
}
/**
* Get the sum of the values from a list
*
* @param array A collection containing Java beans
* @param field A Java Bean field for the objects in <i>array</i> that
* will retrun a number.
*
* @return The sum of the values in <i>array</i>.
*
* @throws Exception
*/
public double getAverage (Object[] array, String field) throws Exception {
return getTotal (Arrays.asList(array),field);
}
/**
* Get the sum of the values
*
* @param values The list of double values to add up.
*
* @return The sum of the arrays
*/
public double getTotal (double[] values) {
double result = 0;
for (int i = 0; i < values.length; i++) {
result += values[i];
}
return result;
}
/**
* Get the average of the values in an array of double values
*
* @param values The list of double values
*
* @return The average of the array of values
*/
public double getAverage (double[] values) {
double total = getTotal(values);
return total / values.length;
}
/**
* Get the sum of the values
*
* @param values The list of long values to add up.
*
* @return The sum of the arrays
*/
public long getTotal (long[] values) {
long result = 0;
for (int i = 0; i < values.length; i++) {
result += values[i];
}
return result;
}
/**
* Get the average of the values in an array of long values
*
* @param values The list of long values
*
* @return The average of the array of values
*/
public double getAverage (long[] values) {
double total = getTotal(values);
return total / values.length;
}
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
