package eu.dnetlib.xml.database.exist;

import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;

/**
 * Synchronized version of ExistResultSet. Allows only one thread to perform operations to this result set.
 * 
 * @author marko
 * 
 */
public class SyncExistResultSet extends ExistResultSet {

	/**
	 * needed for synchrony.
	 */
	private ExistDatabase database;

	/**
	 * needs database isntance for synchronization.
	 * 
	 * @param res
	 *            resource set
	 * @param existDatabase
	 *            database instance
	 */
	public SyncExistResultSet(final ResourceSet res, final ExistDatabase existDatabase) {
		super(res);
		this.database = existDatabase;
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistResultSet#get(int)
	 */
	@Override
	public String get(final int index) throws XMLDBException {
		synchronized (database) {
			return super.get(index);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistResultSet#getSize()
	 */
	@Override
	public long getSize() throws XMLDBException {
		synchronized (database) {
			return super.getSize();
		}
	}

	public ExistDatabase getDatabase() {
		return database;
	}

	public void setDatabase(final ExistDatabase database) {
		this.database = database;
	}

}
