package com.mongodb.client.internal;
import com.mongodb.Block;
import com.mongodb.Function;
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoIterable;
import com.mongodb.lang.Nullable;
import com.mongodb.operation.BatchCursor;
import com.mongodb.operation.ReadOperation;
import java.util.Collection;
import static com.mongodb.assertions.Assertions.notNull;
public abstract class MongoIterableImpl<TResult> implements MongoIterable<TResult> {
private final ClientSession clientSession;
private final ReadConcern readConcern;
private final OperationExecutor executor;
private final ReadPreference readPreference;
private final boolean retryReads;
private Integer batchSize;
public MongoIterableImpl(@Nullable final ClientSession clientSession, final OperationExecutor executor, final ReadConcern readConcern,
final ReadPreference readPreference, final boolean retryReads) {
this.clientSession = clientSession;
this.executor = notNull("executor", executor);
this.readConcern = notNull("readConcern", readConcern);
this.readPreference = notNull("readPreference", readPreference);
this.retryReads = notNull("retryReads", retryReads);
}
public abstract ReadOperation<BatchCursor<TResult>> asReadOperation();
@Nullable
ClientSession getClientSession() {
return clientSession;
}
OperationExecutor getExecutor() {
return executor;
}
ReadPreference getReadPreference() {
return readPreference;
}
protected ReadConcern getReadConcern() {
return readConcern;
}
protected boolean getRetryReads() {
return retryReads;
}
@Nullable
public Integer getBatchSize() {
return batchSize;
}
@Override
public MongoIterable<TResult> batchSize(final int batchSize) {
this.batchSize = batchSize;
return this;
}
@Override
public MongoCursor<TResult> iterator() {
return new MongoBatchCursorAdapter<TResult>(execute());
}
@Override
public MongoCursor<TResult> cursor() {
return iterator();
}
@Nullable
@Override
public TResult first() {
MongoCursor<TResult> cursor = iterator();
try {
if (!cursor.hasNext()) {
return null;
}
return cursor.next();
} finally {
cursor.close();
}
}
@Override
public <U> MongoIterable<U> map(final Function<TResult, U> mapper) {
return new MappingIterable<TResult, U>(this, mapper);
}
@Override
public void forEach(final Block<? super TResult> block) {
MongoCursor<TResult> cursor = iterator();
try {
while (cursor.hasNext()) {
block.apply(cursor.next());
}
} finally {
cursor.close();
}
}
@Override
public <A extends Collection<? super TResult>> A into(final A target) {
forEach(new Block<TResult>() {
@Override
public void apply(final TResult t) {
target.add(t);
}
});
return target;
}
private BatchCursor<TResult> execute() {
return executor.execute(asReadOperation(), readPreference, readConcern, clientSession);
}
}