com.eightkdata.mongowp.server.api.pojos.TransformationMongoCursor Maven / Gradle / Ivy
package com.eightkdata.mongowp.server.api.pojos;
import com.eightkdata.mongowp.exceptions.MongoException;
import com.google.common.base.Function;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import java.util.Iterator;
import java.util.List;
/**
*
*/
public class TransformationMongoCursor implements MongoCursor {
private final MongoCursor innerCursor;
private final Function transformationFun;
private TransformationMongoCursor(MongoCursor innerCursor, Function transformationFun) {
this.innerCursor = innerCursor;
this.transformationFun = transformationFun;
}
public static TransformationMongoCursor create(
MongoCursor innerCursor,
Function transformationFun) {
return new TransformationMongoCursor(innerCursor, transformationFun);
}
@Override
public String getDatabase() {
return innerCursor.getDatabase();
}
@Override
public String getCollection() {
return innerCursor.getCollection();
}
@Override
public long getId() {
return innerCursor.getId();
}
@Override
public void setMaxBatchSize(int newBatchSize) {
innerCursor.setMaxBatchSize(newBatchSize);
}
@Override
public int getMaxBatchSize() {
return innerCursor.getMaxBatchSize();
}
@Override
public boolean isTailable() {
return innerCursor.isTailable();
}
@Override
public boolean isDead() {
return innerCursor.isDead();
}
@Override
public Batch fetchBatch() throws MongoException, DeadCursorException {
return new TransformationBatch(innerCursor.fetchBatch(), transformationFun);
}
@Override
public O getOne() throws MongoException, DeadCursorException {
I one = innerCursor.getOne();
close();
return transformationFun.apply(one);
}
@Override
public void close() {
innerCursor.close();
}
@Override
public Iterator iterator() {
return Iterators.transform(innerCursor.iterator(), transformationFun);
}
private static class TransformationBatch implements Batch {
private final Batch innerBatch;
private final Function transformationFun;
public TransformationBatch(Batch innerBatch, Function transformationFun) {
this.innerBatch = innerBatch;
this.transformationFun = transformationFun;
}
@Override
public void remove() throws UnsupportedOperationException {
innerBatch.remove();
}
@Override
public O next() {
return transformationFun.apply(innerBatch.next());
}
@Override
public boolean hasNext() {
return innerBatch.hasNext();
}
@Override
public int getBatchSize() {
return innerBatch.getBatchSize();
}
@Override
public long getFetchTime() {
return innerBatch.getFetchTime();
}
@Override
public List extends O> asList() {
return Lists.transform(innerBatch.asList(), transformationFun);
}
@Override
public void close() {
innerBatch.close();;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy