/*******************************************************************************
 * Copyright (c) 2004, 2005 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 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;

/**
 * Implements build-in running count function.
 */
public class TotalRunningCount implements IAggregation
{

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

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

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.birt.data.engine.api.aggregation.IAggregation#getParameterDefn()
	 */
	public boolean[] getParameterDefn( )
	{
		final boolean[] paramDefn = {};
		return paramDefn;
	}

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

	private class MyAccumulator extends RunningAccumulator
	{

		private int count;

		/*
		 * (non-Javadoc)
		 * @see org.eclipse.birt.data.engine.api.aggregation.Accumulator#start()
		 */
		public void start( ) throws DataException
		{
			count = 0;
		}

		/*
		 * (non-Javadoc)
		 * @see org.eclipse.birt.data.engine.api.aggregation.Accumulator#onRow(java.lang.Object[])
		 */
		public void onRow( Object[] args ) throws DataException
		{
			count++;
		}

		/*
		 * (non-Javadoc)
		 * @see org.eclipse.birt.data.engine.api.aggregation.Accumulator#getValue()
		 */
		public Object getValue( ) throws DataException
		{
			return new Integer( count );
		}
	}
}
