
net.sf.jagg.SumAggregator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jagg-core Show documentation
Show all versions of jagg-core Show documentation
jAgg is a Java 5.0 API that supports “group by” operations on Lists of Java objects: aggregate operations such as count, sum, max, min, avg, and many more. It also allows custom aggregate operations.
The newest version!
package net.sf.jagg;
import net.sf.jagg.exception.ExpectedNumberException;
import net.sf.jagg.math.DoubleDouble;
import net.sf.jagg.model.WindowClause;
/**
* This class represents the "sum" aggregator over numeric values.
*
* @author Randy Gettman
* @since 0.1.0
*/
public class SumAggregator extends Aggregator implements AnalyticFunction
{
private DoubleDouble mySum = new DoubleDouble();
/**
* Constructs an SumAggregator
that operates on the specified
* property.
* @param property Add up all this property's values.
*/
public SumAggregator(String property)
{
setProperty(property);
}
/**
* Returns an uninitialized copy of this Aggregator
object,
* with the same property(ies) to analyze.
* @return An uninitialized copy of this Aggregator
object.
*/
public SumAggregator replicate()
{
return new SumAggregator(getProperty());
}
/**
* Initialize the sum to zero.
*/
public void init()
{
mySum.reset();
}
/**
* Add the property value to the sum.
*
* @param value The value to aggregate.
*/
public void iterate(Object value)
{
if (value != null)
{
String property = getProperty();
try
{
Number obj = (Number) getValueFromProperty(value, property);
// Don't count nulls.
if (obj != null)
{
mySum.addToSelf(obj.doubleValue());
}
}
catch (ClassCastException e)
{
throw new ExpectedNumberException("Property \"" + property +
"\" must represent a Number.", e);
}
}
}
/**
* Subtract the property value from the sum.
*
* @param value The value to delete.
* @since 0.9.0
*/
public void delete(Object value)
{
if (value != null)
{
String property = getProperty();
try
{
Number obj = (Number) getValueFromProperty(value, property);
// Don't un-count nulls.
if (obj != null)
{
mySum.subtractFromSelf(obj.doubleValue());
}
}
catch (ClassCastException e)
{
throw new ExpectedNumberException("Property \"" + property +
"\" must represent a Number.", e);
}
}
}
/**
* The sum function can take a window clause.
* @return true
.
* @since 0.9.0
*/
public boolean takesWindowClause()
{
return true;
}
/**
* The sum function doesn't supply its own window clause.
* @return null
* @since 0.9.0
*/
public WindowClause getWindowClause()
{
return null;
}
/**
* Merge the given Aggregator
into this one by adding the
* respective sums.
*
* @param agg The Aggregator
to merge into this one.
*/
public void merge(AggregateFunction agg)
{
if (agg != null && agg instanceof SumAggregator)
{
SumAggregator otherAgg = (SumAggregator) agg;
mySum.addToSelf(otherAgg.mySum);
}
}
/**
* Return the sum.
*
* @return The sum as a Double
, or 0
if
* no values have been accumulated.
*/
public Double terminate()
{
return terminateDoubleDouble().doubleValue();
}
/**
* Return the result as a DoubleDouble
. This is used mainly
* when other Aggregators
that use this result must maintain a
* high precision.
* @return The sum as a DoubleDouble
, or 0
if
* no values have been accumulated.
* @since 0.4.0
*/
public DoubleDouble terminateDoubleDouble()
{
return new DoubleDouble(mySum);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy