com.googlecode.mjorm.ObjectIterator Maven / Gradle / Ivy
package com.googlecode.mjorm;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
/**
* An ObjectIterator provides mapping features for
* {@link DBCursors}s. Basically it returns java
* objects rather than {@link DBObject}s.
* @param
*/
public class ObjectIterator
implements Iterable,
Iterator {
private DBCursor cursor;
private final ObjectMapper objectMapper;
private final Class clazz;
/**
* Creates the {@link ObjectIterator}.
* @param cursor the cursor to wrap
* @param objectMapper the {@link ObjectMapper} to use
* @param clazz the class we're returning
*/
public ObjectIterator(
DBCursor cursor, ObjectMapper objectMapper, Class clazz) {
this.cursor = cursor;
this.objectMapper = objectMapper;
this.clazz = clazz;
}
/**
* For for loops.
*/
public Iterator iterator() {
return this;
}
/**
* Reads all of the objects behind this cursor
* and returns them in a {@link List}.
* @param ret the list to read the objects into
* @return the {@link List} of objects.
*/
public List readAll(List ret) {
while (hasNext()) {
ret.add(next());
}
return ret;
}
/**
* Reads all of the objects behind this cursor
* and returns them in a {@link List}.
* @return the {@link List} of objects.
*/
public List readAll() {
return readAll(new ArrayList());
}
/**
* {@inheritDoc}
*/
public boolean hasNext() {
return cursor.hasNext();
}
/**
* {@inheritDoc}
*/
public E next() {
try {
return objectMapper.map(cursor.next(), clazz);
} catch (Exception e) {
throw new MjormException(e);
}
}
/**
* {@inheritDoc}
*/
public E current() {
try {
return objectMapper.map(cursor.curr(), clazz);
} catch (Exception e) {
throw new MjormException(e);
}
}
/**
* {@see DBCursor#remove()}.
*/
public void remove() {
cursor.remove();
}
/**
* Returns the underlying {@link DBCursor}.
* @return the {@link DBCursor}
*/
public DBCursor getDBCursor() {
return cursor;
}
}