
net.sf.jagg.MaxAnalyticAggregator 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 java.util.TreeMap;
import net.sf.jagg.exception.ExpectedComparableException;
import net.sf.jagg.util.FrequencyMapUtil;
import net.sf.jagg.model.WindowClause;
/**
* This class represents the "max" analytic function over Comparable
* values.
*
* @author Randy Gettman
* @since 0.9.0
*/
public class MaxAnalyticAggregator extends MaxAggregator implements AnalyticFunction
{
/**
* A TreeMap is needed here, so that all of the following operations' costs
* are minimized: add O(log n), remove O(log n), and find max O(log n).
*/
private TreeMap myIteratedElements;
/**
* Constructs a MaxAnalyticAggregator
that operates on the
* specified property.
* @param property Determine the maximum of this property's values.
*/
public MaxAnalyticAggregator(String property)
{
super(property);
myIteratedElements = new TreeMap();
}
/**
* Returns an uninitialized copy of this Aggregator
object,
* with the same property(ies) to analyze.
* @return An uninitialized copy of this Aggregator
object.
*/
public MaxAnalyticAggregator replicate()
{
return new MaxAnalyticAggregator(getProperty());
}
/**
* In addition to what the superclass initialization does, clear this
* object's own iterated elements collection.
*/
public void init()
{
super.init();
myIteratedElements.clear();
}
/**
* In addition to what the superclass iteration does, add the property value
* to this object's own iterated elements collection.
*
* @param value The value to aggregate.
*/
@SuppressWarnings("unchecked")
public void iterate(Object value)
{
super.iterate(value);
if (value != null)
{
String property = getProperty();
try
{
Comparable obj = (Comparable) getValueFromProperty(value, property);
// Don't count nulls.
if (obj != null)
{
FrequencyMapUtil.add(myIteratedElements, obj);
}
}
catch (ClassCastException e)
{
throw new ExpectedComparableException("Property \"" + property +
"\" must be Comparable.", e);
}
}
}
/**
* Remove the property value from this object's own iterated elements
* collection.
*
* @param value The value to remove.
*/
@SuppressWarnings("unchecked")
public void delete(Object value)
{
if (value != null)
{
String property = getProperty();
try
{
Comparable obj = (Comparable) getValueFromProperty(value, property);
// Don't count nulls.
if (obj != null)
{
Integer frequency = myIteratedElements.get(obj);
if (frequency != null)
{
if (!FrequencyMapUtil.remove(myIteratedElements, obj))
{
// Get the new max, which only needs to be done if a distinct
// element is removed because the frequency decreased to zero.
Comparable currMax = getMax();
if (currMax != null && currMax.compareTo(obj) == 0)
{
if (myIteratedElements.isEmpty())
setMax(null);
else
setMax(myIteratedElements.lastKey());
}
}
}
}
}
catch (ClassCastException e)
{
throw new ExpectedComparableException("Property \"" + property +
"\" must be Comparable.", e);
}
}
}
/**
* The max function can take a window clause.
* @return true
.
* @since 0.9.0
*/
public boolean takesWindowClause()
{
return true;
}
/**
* The max function doesn't supply its own window clause.
* @return null
* @since 0.9.0
*/
public WindowClause getWindowClause()
{
return null;
}
/**
* In addition to what the superclass merging does, add the other
* aggregator's iterated elements to this object's own iterated elements
* collection.
*
* @param agg The Aggregator
to merge into this one.
*/
@SuppressWarnings("unchecked")
public void merge(AggregateFunction agg)
{
super.merge(agg);
if (agg != null && agg instanceof MaxAnalyticAggregator)
{
MaxAnalyticAggregator otherAgg = (MaxAnalyticAggregator) agg;
FrequencyMapUtil.combine(otherAgg.myIteratedElements, myIteratedElements);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy