/*******************************************************************************
 * 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.impl.document.viewing;

import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.executor.cache.ResultSetCache;
import org.eclipse.birt.data.engine.impl.document.util.IExprDataResultSet;
import org.eclipse.birt.data.engine.odi.IResultClass;
import org.eclipse.birt.data.engine.odi.IResultObject;

/**
 * Do sorting for the rows in one group. This is only used with
 * ExprTotalDataResultSet.
 */
class ExprGroupDataResultSet implements IExprDataResultSet
{
	private ResultSetCache rsCache;
	private IResultClass resultClass;
	private int count;
	
	private int rowIndex;
	
	/**
	 * @param rsCache
	 * @param count
	 */
	ExprGroupDataResultSet( ResultSetCache rsCache,
			IResultClass resultClass, int count )
	{
		this.rsCache = rsCache;
		this.resultClass = resultClass;
		this.count = count;
	}

	/*
	 * @see org.eclipse.birt.data.engine.odi.IExprDataResultSet#getResultClass()
	 */
	public IResultClass getResultClass( )
	{
		return this.resultClass;
	}

	/*
	 * @see org.eclipse.birt.data.engine.odi.IExprDataResultSet#fetch()
	 */
	public IResultObject next( ) throws DataException
	{
		return this.fetch( );
	}
	
	/*
	 * @see org.eclipse.birt.data.engine.odi.IExprDataResultSet#fetch()
	 */
	public IResultObject fetch( ) throws DataException
	{
		if ( rowIndex == count )
			return null;

		rowIndex++;
		return rsCache.fetch( );
	}

	/*
	 * @see org.eclipse.birt.data.engine.odi.IExprDataResultSet#getCount()
	 */
	public int getCount( ) throws DataException
	{
		return this.count;
	}
	
	/*
	 * @see org.eclipse.birt.data.engine.impl.document.viewing.IExprDataResultSet#close()
	 */
	public void close( )
	{
		rsCache.close( );
	}

}
