package org.mandarax.util.resultsetfilters;

/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.*;
import org.mandarax.kernel.*;
import org.mandarax.util.*;

/**
 * A filter that filters results by accepting only results that satisfy a certain condition.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 3.0
 */

public class WhereFilter extends CachedResultSet {

	private ResultSet original = null;
	private ResultSetCondition condition = null;
	private boolean hasMore = true;
	
	

	/**
	 * Constructor.
	 * @param original a not yet initialized result set (the cursor has not yet been moved)
	 * @param condition a result set condition
	 */
	public WhereFilter(ResultSet org,	ResultSetCondition condition) throws InferenceException {
		super();
		this.original = org;
		this.condition = condition;
		this.results = new ArrayList();
	}
	/**
	 * Get a list of all variable terms in the query.
	 * @return a list of variable terms
	 */
	public List getQueryVariables() throws InferenceException {
		return original.getQueryVariables();
	}
	/**
	 * Cache all records.
	 */
	private void cacheAll() throws InferenceException {
		while(original.next()) {
			if (condition.isSatisfiedBy(original)) results.add(new ResultImpl(original));
		}
	}
	/**
	 * Cache the next results(s).
	 * Try to cache the given number of results.
	 * @param numberOfResults the number of results
	 */
	private void cache(int numberOfResults) throws InferenceException {		
		while(results.size()<numberOfResults && original.next()) {
			if (condition.isSatisfiedBy(original)) results.add(new ResultImpl(original));
		}
	}
	/**
	 * Indicates whether the cursor is at the last position.
	 * @return a boolean
	 */
	public boolean isLast() throws InferenceException {
		cacheAll();
		return super.isLast();
	}
	/**
	 * Moves the cursor to the first row in this ResultSet object.
	 * @return true if the cursor is on a valid row; false if there are no rows in the result set
	 */
	public boolean first() throws InferenceException {
		cache(1);
		return super.first();
	}
	/**
	 * Moves the cursor to the last row in this ResultSet object.
	 * @return true if the cursor is on a valid row; false if there are no rows in the results
	 */
	public boolean last() throws InferenceException {
		cacheAll();
		return super.last();
	}
	/**
	 * Retrieves the current result number. Numbering starts with 1.
	 * @return a number
	 */
	public int getResultNumber() throws InferenceException {
		cacheAll();
		return super.getResultNumber();
	}
	/**
	 * Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned
	 * before the first row; the first call to the method next makes the first row the current row;
	 * the second call makes the second row the current row, and so on.
	 * @return true if the new current row is valid; false if there are no more rows
	 */
	public boolean next() throws InferenceException {
		cache(pos+2);
		return super.next();
	}
	/**
	 * Moves the cursor to the previous row in this ResultSet object.
	 * @return true if the cursor is on a valid row; false if it is off the result set
	 */
	public boolean previous() throws InferenceException {
		cache(pos);
		return super.previous();
	};
	/**
	 * Moves the cursor a relative number of results, either positive or negative.
	 * @param offset the number of results
	 * @return true if the cursor is on the result set; false otherwise.
	 */
	public boolean relative(int offset) throws InferenceException {
		cache(pos+offset+2);
		return super.relative(offset);
	}
	/**
	 * Moves the cursor to the given result number.
	 * @param resultNo the number of results.
	 * @return true if the cursor is on the result set; false otherwise
	 */
	public boolean absolute(int resultNo) throws InferenceException {
		cache(resultNo+2);
		return super.absolute(resultNo); 
	}


	/**
	 * Moves the cursor to the end of this ResultSet object, just after the last row. 
	 * This method has no effect if the result set contains no rows.
	 */
	public void afterLast() throws InferenceException {
		cacheAll();
		pos = results.size();
	}

	/**
	 * Retrieves whether the cursor is after the last result in this ResultSet object.
	 * @return true if the cursor is after the last result; false if the cursor is at any other position or the result set contains no results
	 */
	public boolean isAfterLast() throws InferenceException {
		cacheAll();
		return super.isAfterLast();
	}
	


}
