package de.uka.ipd.consensus.impl;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;

import org.tm4j.tologx.TologResultsSet;

import de.uka.ipd.consensus.foundation.query.QueryEngineResultSet;

/**
 * 
 * TODO
 * @author Thomas Much
 * @version 2006-06-14
 */
public class QueryEngineResultSetImpl implements QueryEngineResultSet {

	private OntologyManagerImpl omi;
	private TologResultsSet     rs;


	public QueryEngineResultSetImpl(OntologyManagerImpl omi, TologResultsSet rs) {
		this.omi = omi;
		this.rs  = rs;
	}


	public int getRowCount() {
		return rs.getNumRows();
	}


	public int getColumnCount() {
		return rs.getNumCols();
	}

	
	public Collection getRows() {
		return new AbstractSet() {

			public Iterator iterator() {
				return new RowIterator();
			}

			public int size() {
				return getRowCount();
			}
		};
	}

	
	public Collection getColumnNames() {
		return new AbstractSet() {

			public Iterator iterator() {
				return new ColumnNamesIterator();
			}

			public int size() {
				return getColumnCount();
			}
		};
	}
	

	
	private class ColumnNamesIterator implements Iterator {
		private int pos = 0;

		public boolean hasNext() {
			return (pos < getColumnCount());
		}

		public Object next() {
			return rs.getVar(pos++);
		}
		
		public void remove() {
			throw new UnsupportedOperationException();
		}
	}



	private class RowIterator implements Iterator {
		private int pos = 0;

		public boolean hasNext() {
			return (pos < getRowCount());
		}

		public Object next() {
			return TM4JCollectionWrapperSet.getInstance( omi, rs.getRow(pos++) );
		}
		
		public void remove() {
			throw new UnsupportedOperationException();
		}
	}
}
