package ws.prova.reference;

/*
 * Copyright (C) 1999-2005 <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 org.mandarax.kernel.Query;
import org.mandarax.kernel.Result;
import org.mandarax.reference.IEUtils;
import org.mandarax.util.CachedResultSet;
import ws.prova.kernel.ProvaResultSet;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

/**
 * Implementation of a result set for ResolutionInferenceEngine2. Could be an inner class, the only
 * reason to have it as a separate class is that tool support (e.g. debugging) for separate classes is better.
 *
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @version 2.0 <25 March 2007>
 */

public class ProvaResultSetImpl2 extends CachedResultSet implements ProvaResultSet {
	Exception ex = null;

	class ProvaResulSetImpl2Iterator implements Iterator {
		private int cp = -1;

		/* (non-Javadoc)
		 * @see java.util.Iterator#remove()
		 */
		public void remove() {
			throw new UnsupportedOperationException( );
		}

		/* (non-Javadoc)
		 * @see java.util.Iterator#hasNext()
		 */
		public boolean hasNext() {
			return (cp+1) < results.size(); 
		}

		/* (non-Javadoc)
		 * @see java.util.Iterator#next()
		 */
		public Object next() {
			return (Result) results.get( ++cp );
		}
	}
	
	public ProvaResultSetImpl2() {
		super();
		vars = new ArrayList();
		results = new Vector();
	}

	public ProvaResultSetImpl2(Query query) {
		super();
		vars = IEUtils.getVars(query);
		results = new Vector();
	}

	final public void add(Result res) {
		pos++;
		if (results.size() > pos) {
			results.set(pos, res);
			return;
		}
		results.add(pos, res);
	}

	final public int numberOfResults() {
		return results.size();
	}

	final public Exception getException() {
		return ex;
	}

	final public void setException(Exception ex) {
		this.ex = ex;
	}

	/* (non-Javadoc)
	 * @see ws.prova.kernel.ProvaResultSet#iterator()
	 */
	public Iterator iterator() {
		return new ProvaResulSetImpl2Iterator();
	}
}
