/*
 *************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */
package org.eclipse.birt.data.engine.aggregation;

import java.util.HashMap;

import org.eclipse.birt.core.data.DataTypeUtil;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.data.engine.api.aggregation.Accumulator;
import org.eclipse.birt.data.engine.api.aggregation.IAggregation;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;

/**
 * 
 * Implements the built-in Total.mode aggregation
 */
public class TotalMode implements IAggregation
{

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.birt.data.engine.aggregation.Aggregation#getName()
     */
    public String getName()
    {
        return BuiltInAggregationFactory.TOTAL_MODE_FUNC;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.birt.data.engine.aggregation.Aggregation#getType()
     */
    public int getType()
    {
        return SUMMARY_AGGR;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.birt.data.engine.aggregation.Aggregation#getParameterDefn()
     */
    public boolean[] getParameterDefn()
    {
        return new boolean[] { true };
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.birt.data.engine.aggregation.Aggregation#newAccumulator()
     */
    public Accumulator newAccumulator()
    {
        return new MyAccumulator();
    }

    private class MyAccumulator extends SummaryAccumulator
    {
    	// Maps a value (Double) to its count (Integer)
        private HashMap valueMap;
        private int topCount;
        private int secondCount;
        private Double topValue;


        public void start()
        {
            super.start();
            valueMap = new HashMap();
            topCount = 0;
            secondCount = 0;
            topValue = null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.birt.data.engine.aggregation.Accumulator#onRow(java.lang.Object[])
         */
        public void onRow(Object[] args) throws DataException
        {
            assert (args.length > 0);
            if (args[0] != null)
            {
				try
				{
					Double value = DataTypeUtil.toDouble( args[0] );
					Integer count = (Integer) valueMap.get( value );
					if ( count == null )
					{
						// first occurrence
						count = new Integer(1);
					}
					else
					{
						count = new Integer( count.intValue() + 1);
					}
					valueMap.put( value, count );
					
					// Keep track of two front runners
					if ( count.intValue() > topCount )
					{
						topValue = value;
						secondCount = topCount;
						topCount = count.intValue();
					}
					else if ( count.intValue() > secondCount )
					{
						secondCount = count.intValue();
					}
				}
				catch ( BirtException e )
				{
					throw new DataException( ResourceConstants.DATATYPEUTIL_ERROR, e );
				}
            }
        }

        public void finish() throws DataException
        {
            super.finish();
            valueMap = null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.birt.data.engine.aggregation.SummaryAccumulator#getSummaryValue()
         */
        public Object getSummaryValue()
        {
        	assert topCount >= secondCount;
        	if ( topCount == secondCount )
        	{
        		// multiple modes; ROM scripting spec says we should return null
        		return null;
        	}
        	return topValue;
        }

    }

}