io.rtr.alchemy.db.mongo.util.ExperimentIterable Maven / Gradle / Ivy
package io.rtr.alchemy.db.mongo.util;
import io.rtr.alchemy.db.mongo.models.ExperimentEntity;
import io.rtr.alchemy.models.Experiment;
import java.util.Iterator;
/**
* An iterator that iterates over ExperimentEntity results and maps them to actual Experiment
* instances
*/
public class ExperimentIterable implements Iterable {
private final Iterator iterator;
private final Experiment.BuilderFactory factory;
public ExperimentIterable(
final Iterator iterator, final Experiment.BuilderFactory factory) {
this.iterator = iterator;
this.factory = factory;
}
@Override
public Iterator iterator() {
return ExceptionSafeIterator.wrap(
new Iterator() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Experiment next() {
final ExperimentEntity entity = iterator.next();
return entity.toExperiment(factory.createBuilder(entity.name));
}
@Override
public void remove() {
iterator.remove();
}
});
}
}