/*
 *************************************************************************
 * 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.Date;
import java.util.LinkedList;

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.movingAva aggregation
 */
public class TotalMovingAve implements IAggregation
{

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

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

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

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

    private class MyAccumulator extends RunningAccumulator
    {

        private LinkedList list;

        private int window = 1;

        private double sum = 0D;
        
        private boolean isDateType = false;

        public void start()
        {
            sum = 0D;
            list = new LinkedList();
            window = 1;
            isDateType = false;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.birt.data.engine.aggregation.Accumulator#onRow(java.lang.Object[])
         */
        public void onRow(Object[] args) throws DataException
        {
            assert (args.length > 1);
            if (args[0] != null && args[1] != null)
            {
                try
				{
					if (list.size()==0)
					{
					    window = DataTypeUtil.toInteger(args[1]).intValue();
					    assert(window>0);
					    if(args[0] instanceof Date)
					    {
					        isDateType = true;
					    }
					}
					if(isDateType)
					{
					    long value = ((Date)args[0]).getTime();
					    list.addLast(new Long(value));
					    sum += (double)value;
					}
					else
					{
					    Double value = DataTypeUtil.toDouble(args[0]);
					    list.addLast(value);
					    sum += value.doubleValue();
					    
					}
					if (list.size() > window)
					{
					    sum -= ((Number) list.get(0)).doubleValue();
					    list.remove(0);
					}
				}
				catch ( BirtException e )
				{
					throw new DataException( ResourceConstants.DATATYPEUTIL_ERROR, e );
				}
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.birt.data.engine.aggregation.Accumulator#getValue()
         */
        public Object getValue()
        {
            if (list.size() == 0)
            {
                return null;
            }

            return new Double(sum / list.size());
        }

    }

}