/*******************************************************************************
 * 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.executor.transform.group;

import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.odi.IQuery.GroupSpec;

/**
 * Interval range implementation of GroupBy, which does grouping in such a
 * column which data type is Integer, Float, Double.
 */
class GroupByNumberRange extends GroupBy
{

	/** start value */
	protected double startValue;

	/** interval value */
	protected double intervalValue;


	/**
	 * @param groupDefn
	 */
	GroupByNumberRange( GroupSpec groupDefn )
	{
		this.startValue = 0;
		//Object start = new Integer(0);

		startValue = 0;

		intervalValue = 1;

/*		// for descending data only
		if ( groupDefn.getSortDirection( ) == GroupSpec.SORT_DESC )
		{
			intervalValue *= -1;
		}*/
	}

	/*
	 * @see org.eclipse.birt.data.engine.api.GroupExtraDefn#isSameGroupValue(java.lang.Object,
	 *      java.lang.Object)
	 */
	boolean isSameGroup( Object currentGroupKey, Object previousGroupKey )
			throws DataException
	{
		assert ( currentGroupKey != null );
		assert ( previousGroupKey != null );

//		if ( intervalValue == 0 )
//			return currentGroupKey.equals( previousGroupKey );

		double prevValue = 0;
		double currValue = 0;
		if ( currentGroupKey instanceof Number )
		{
			prevValue = ( (Number) previousGroupKey ).doubleValue( );
			currValue = ( (Number) currentGroupKey ).doubleValue( );
		}
		else
		{
			throw new DataException( ResourceConstants.BAD_GROUP_KEY_TYPE,
					currentGroupKey.getClass( ).getName( ) );
		}

		return GroupUtil.isWithinInterval( startValue,
				intervalValue,
				currValue,
				prevValue );
	}

}
