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

io.rtr.alchemy.db.mongo.util.ExperimentIterable Maven / Gradle / Ivy

There is a newer version: 2.2.14
Show newest version
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();
                    }
                });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy