package org.mandarax.util;

/*
 * 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.List;
import java.util.Map;

import org.mandarax.kernel.Derivation;
import org.mandarax.kernel.InferenceException;
import org.mandarax.kernel.Result;
import org.mandarax.kernel.ResultSet;
import org.mandarax.kernel.VariableTerm;

/**
 * Abstract superclass for result set implementations with results being organized in a
 * list that is fetched at once. Results are represented by a list of instances of
 * org.mandarax.kernel.Result. Subclasses are responsible for appropriate initialization
 * of this list.
 * Moved from org.mandarax.util to org.mandarax.util.resultsetfilters
 * @see org.mandarax.kernel.ResultSet
 * @see org.mandarax.kernel.Result
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.8.1 (in util), 3.0 (in util.resultsetfilters)
 */
public class CachedResultSet implements ResultSet {

	protected List results = null;
	protected List vars = null;
	protected int pos = -1;

	/**
	 * Constructor.
	 */
	public CachedResultSet() {
	  super();
	}
	/**
	 * Constructor.
	 */
	public CachedResultSet(List results,List variables) {
	  super();
	  this.results = results;
	  this.vars = variables;
	}
	/**
	 * Indicates whether the cursor is at the first position.
	 * @return a boolean
	 */
	public boolean isFirst() throws InferenceException {
		return pos==0;
	}
	/**
	 * Indicates whether the cursor is at the last position.
	 * @return a boolean
	 */
	public boolean isLast() throws InferenceException {
		return pos==results.size()-1;
	}
	/**
	 * 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 {
		if (results.size() > 0) {
			pos = 0;
			return true;
		}
		return false;
	}
	/**
	 * 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 {
		if (results.size() > 0) {
			pos = results.size() - 1;
			return true;
		}
		return false;
	}
	/**
	 * Retrieves the current result number. Numbering starts with 1.
	 * @return a number
	 */
	public int getResultNumber() throws InferenceException {
		return pos == -1 ? -1 : pos + 1;
	}
	/**
	 * 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 {
		pos = pos + 1;
		return isValid();
	}
	/**
	 * 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 {
		pos = pos - 1;
		return isValid();
	};

	/**
	 * Releases this ResultSet resources.
	 */
	public void close() throws InferenceException {
	}
	/**
	 * Get a list of all variable terms in the query.
	 * @return a list of variable terms
	 */
	public List getQueryVariables() throws InferenceException {
		return vars;
	}
    /**
	 * Get the result at the current cursor position.
	 * @return a result
	 */
	private Result getResult() throws InferenceException {
		if (!isValid()) throw new InferenceException("Illegal cursor position " + pos + " in result set.");
		return ((Result) results.get(pos));
	}
	/**
	 * Get the derivation of the current result.
	 * Throw an exception if the cursor is not positioned at a result.
	 * @return the proof (derivation)
	 */
	public Derivation getProof() throws InferenceException {
		return getResult().getProof();
	}
	/**
	 * Get the object that replaces the variable with the given name and type.
	 * If there is no such object, return null.
	 * Throw an exception if the cursor is not positioned at a result.
	 * @return the object that replaced the variable
	 * @param type the type of the variable
	 * @param name the name of the variable
	 * @exception an inference exception
	 */
	public Object getResult(Class type, String name) throws InferenceException {
		return getResult().getResult(type, name);
	}
	/**
	 * Get the object that replaces the variable.
	 * If there is no such object, return null.
	 * Throw an exception if the cursor is not positioned at a result.
	 * @return the object that replaced the variable
	 * @param term the variable term that is (perhaps) replaced
	 * @exception an inference exception
	 */
	public Object getResult(VariableTerm term) throws InferenceException {
		return getResult().getResult(term);
	}
	/**
	 * Get the current position of the cursor.
	 * @return an integer
	 */
	public int getCursorPosition() throws InferenceException {
		return pos;
	}
	/**
	 * 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 {
		pos = pos+offset;
		return isValid();
	}
	/**
	 * 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 {
		pos = resultNo-1; // like in JDBC - the number of the first result is 1, not 0 !
		return isValid(); 
	}
	/**
	 * Moves the cursor to the front of this ResultSet object, just before the first row. 
	 * This method has no effect if the result set contains no rows.
	 */
	public void beforeFirst() throws InferenceException {
		pos = -1;
	}

	/**
	 * 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 {
		pos = results.size();
	}
	/**
	 * Retrieves whether the cursor is before the first row in this ResultSet object.
	 * @return true if the cursor is before the first row; false if the cursor is at any other position or the result set contains no rows
	 */
	public boolean isBeforeFirst() throws InferenceException {
		return pos==-1;
	}

	/**
	 * 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 {
		return pos==results.size();
	}
	
	/**
	 * Get the replacements as map (term -> term associations).
	 * @return a map
	 */
	public Map getResults() throws InferenceException {
		return getResult().getResults();
	}
	/**
	 * Indicates whether the cursor is on a valid position.
	 * @return a boolean
	 */
	private boolean isValid() {
		return 0<=pos && pos<results.size(); 
	}
	
	


}
