All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jagg.ModeAggregator Maven / Gradle / Ivy

Go to download

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 "mode" aggregator over Comparable
 * values.
 *
 * @author Randy Gettman
 * @since 0.6.0
 */
public class ModeAggregator extends Aggregator implements AnalyticFunction
{
   private TreeMap, Integer> myIteratedElements;

   /**
    * Constructs a ModeAggregator that operates on the specified
    * property.
    * @param property Determine the statistical mode of this property's values.
    */
   public ModeAggregator(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 ModeAggregator replicate()
   {
      return new ModeAggregator(getProperty());
   }

   /**
    * Initialize an internal list to empty.
    */
   public void init()
   {
      myIteratedElements = new TreeMap, Integer>();
   }

   /**
    * Make sure the second property's value is not null, then add the entire
    * Object to an internal list.
    *
    * @param value The value to aggregate.
    */
   public void iterate(Object value)
   {
      if (value != null)
      {
         String property = getProperty();

         try
         {
            // The property must be Comparable.
            Comparable comp = (Comparable) getValueFromProperty(value, property);

            // Don't count nulls.
            if (comp != null)
            {
               FrequencyMapUtil.add(myIteratedElements, comp);
            }
         }
         catch (ClassCastException e)
         {
            throw new ExpectedComparableException("Property \"" + property +
               "\" must be Comparable.", e);
         }
      }
   }

   /**
    * Make sure the second property's value is not null, then remove the entire
    * Object from an internal list.
    *
    * @param value The value to remove.
    * @since 0.9.0
    */
   public void delete(Object value)
   {
      if (value != null)
      {
         String property = getProperty();

         try
         {
            // The property must be Comparable.
            Comparable comp = (Comparable) getValueFromProperty(value, property);

            // Don't count nulls.
            if (comp != null)
            {
               FrequencyMapUtil.remove(myIteratedElements, comp);
            }
         }
         catch (ClassCastException e)
         {
            throw new ExpectedComparableException("Property \"" + property +
               "\" must be Comparable.", e);
         }
      }
   }

   /**
    * The mode function can take a window clause.
    * @return true.
    * @since 0.9.0
    */
   public boolean takesWindowClause()
   {
      return true;
   }

   /**
    * The mode 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
    * contents of the given Aggregator's internal list into this
    * Aggregator's internal list.
    *
    * @param agg The Aggregator to merge into this one.
    */
   public void merge(AggregateFunction agg)
   {
      if (agg != null && agg instanceof ModeAggregator)
      {
         ModeAggregator otherAgg = (ModeAggregator) agg;
         FrequencyMapUtil.combine(otherAgg.myIteratedElements, myIteratedElements);
      }
   }

   /**
    * Return the value among the values in the specified property that occurs
    * most often (the statistical mode), or any of the modes if there is more
    * than one, with the following algorithm:
    * 
    *
  1. The internal tree map keeps values sorted.
  2. *
  3. Walk through the tree map of values, keeping track of the current * value and the current value's frequency.
  4. *
  5. If the current value changes, and the frequency is greater than the * maximum frequency found so far, then note the previous item as the new * mode with the new frequency.
  6. *
  7. Return the mode.
  8. *
* * @return The statistical mode. * @see java.util.Collections#sort * @see net.sf.jagg.util.ComparableComparator */ public Comparable terminate() { int numItems = myIteratedElements.size(); if (numItems == 0) return null; Comparable mode = null; int maxFrequency = 0; for (Comparable c : myIteratedElements.keySet()) { int frequency = myIteratedElements.get(c); if (frequency > maxFrequency) { maxFrequency = frequency; mode = c; } } return mode; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy