Add FindOptions (optional) parameter to MongoUtils#findManyAndTransform

This commit is contained in:
Colin McDonald 2016-02-07 21:06:13 -05:00
parent b6d45853fb
commit f5f311ca7b

View File

@ -17,10 +17,11 @@ import java.util.List;
@UtilityClass @UtilityClass
public class MongoUtils { public class MongoUtils {
private static final FindOptions limitOne = new FindOptions().setLimit(1); private static final FindOptions limitOneOptions = new FindOptions().setLimit(1);
private static final FindOptions defaultOptions = new FindOptions();
public static <T> void findOneAndTransform(String collection, JsonObject query, Function<JsonObject, T> transformation, Handler<AsyncResult<T>> callback) { public static <T> void findOneAndTransform(String collection, JsonObject query, Function<JsonObject, T> transformation, Handler<AsyncResult<T>> callback) {
APIv3.getMongoClient().findWithOptions(collection, query, limitOne, res -> { APIv3.getMongoClient().findWithOptions(collection, query, limitOneOptions, res -> {
if (res.succeeded()) { if (res.succeeded()) {
if (!res.result().isEmpty()) { if (!res.result().isEmpty()) {
JsonObject json = res.result().get(0); JsonObject json = res.result().get(0);
@ -37,7 +38,11 @@ public class MongoUtils {
} }
public static <T> void findManyAndTransform(String collection, JsonObject query, Function<JsonObject, T> transformation, Handler<AsyncResult<List<T>>> callback) { public static <T> void findManyAndTransform(String collection, JsonObject query, Function<JsonObject, T> transformation, Handler<AsyncResult<List<T>>> callback) {
APIv3.getMongoClient().find(collection, query, res -> { findManyAndTransform(collection, query, defaultOptions, transformation, callback);
}
public static <T> void findManyAndTransform(String collection, JsonObject query, FindOptions options, Function<JsonObject, T> transformation, Handler<AsyncResult<List<T>>> callback) {
APIv3.getMongoClient().findWithOptions(collection, query, options, res -> {
if (res.succeeded()) { if (res.succeeded()) {
Collection<T> servers = Collections2.transform(res.result(), transformation); Collection<T> servers = Collections2.transform(res.result(), transformation);
callback.handle(Future.succeededFuture(ImmutableList.copyOf(servers))); callback.handle(Future.succeededFuture(ImmutableList.copyOf(servers)));