All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadeutils.mongo.MongoResultSet Maven / Gradle / Ivy

The newest version!
package jadeutils.mongo;

import jadeutils.mongo.impl.MongoUtil;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.DBCursor;

public class MongoResultSet {
	private DBCursor cursor;
	private Class entryClass;

	public MongoResultSet(Class entryClass, DBCursor cursor) {
		this.cursor = cursor;
		this.entryClass = entryClass;
	}

	public MongoResultSet skip(int n) {
		return new MongoResultSet(this.entryClass, this.cursor.skip(n));
	}

	public MongoResultSet limit(int n) {
		return new MongoResultSet(this.entryClass, this.cursor.limit(n));
	}

	public MongoResultSet sort(Condition orderBy)
			throws IllegalArgumentException, IllegalAccessException //
	{
		return new MongoResultSet(this.entryClass,
				this.cursor.sort(MongoUtil.parseCondition(orderBy)));
	}

	public boolean hasNext() {
		if (null == cursor) {
			return false;
		} else {
			boolean result = cursor.hasNext();
			if (result) {
				return result;
			} else {
				cursor.close();
				return false;
			}
		}
	}

	@SuppressWarnings("unchecked")
	public T next() throws InstantiationException, IllegalAccessException {
		T obj = null;
		if (cursor.hasNext()) {
			obj = (T) MongoUtil.genModelFromRec(entryClass, cursor.next());
		} else {
			cursor.close();
		}
		return obj;
	}

	public List toList() throws InstantiationException,
			IllegalAccessException {
		List list = new ArrayList();
		while (this.hasNext()) {
			list.add(this.next());
		}
		return list;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy