io.vertx.up.unity.Async Maven / Gradle / Ivy
package io.vertx.up.unity;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.up.commune.Envelop;
import io.vertx.up.log.Annal;
import io.vertx.up.fn.Fn;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;
@SuppressWarnings("all")
class Async {
private static final Annal LOGGER = Annal.get(Async.class);
static Future toSingle(
final String pojo,
final CompletableFuture completableFuture
) {
final Future future = Future.future();
final Future jsonFuture = Async.toJsonFuture(pojo, completableFuture);
// future.complete(To.toEnvelop(jsonFuture.result()));
jsonFuture.setHandler(item -> future.complete(To.toEnvelop(item.result())));
return future;
}
static Future toUnique(
final String pojo,
final CompletableFuture> completableFuture
) {
final Future future = Future.future();
final Future arrayFuture = Async.toArrayFuture(pojo, completableFuture);
// future.complete(To.toEnvelop(To.toUnique(arrayFuture.result(), pojo)));
arrayFuture.setHandler(item -> future.complete(To.toEnvelop(To.toUnique(item.result(), pojo))));
return future;
}
static Future toMulti(
final String pojo,
final CompletableFuture> completableFuture
) {
final Future future = Future.future();
final Future arrayFuture = Async.toArrayFuture(pojo, completableFuture);
// future.complete(To.toEnvelop(arrayFuture.result()));
arrayFuture.setHandler(item -> future.complete(To.toEnvelop(item.result())));
return future;
}
static Future toFuture(
final CompletableFuture completableFuture
) {
final Future future = Future.future();
completableFuture.thenAcceptAsync(future::complete)
.exceptionally((ex) -> {
LOGGER.jvm(ex);
future.fail(ex);
return null;
});
return future;
}
@SuppressWarnings("all")
static Future toJsonFuture(
final String pojo,
final CompletableFuture completableFuture
) {
final Future future = Future.future();
Fn.safeSemi(null == completableFuture, null,
() -> future.complete(new JsonObject()),
() -> completableFuture.thenAcceptAsync((item) -> Fn.safeSemi(
null == item, null,
() -> future.complete(new JsonObject()),
() -> future.complete(To.toJson(item, pojo))
)).exceptionally((ex) -> {
LOGGER.jvm(ex);
future.fail(ex);
return null;
}));
return future;
}
@SuppressWarnings("all")
static Future toArrayFuture(
final String pojo,
final CompletableFuture> completableFuture
) {
final Future future = Future.future();
Fn.safeSemi(null == completableFuture, null,
() -> future.complete(new JsonArray()),
() -> completableFuture.thenAcceptAsync((item) -> Fn.safeSemi(
null == item, null,
() -> future.complete(new JsonArray()),
() -> future.complete(To.toArray(item, pojo))
)).exceptionally((ex) -> {
LOGGER.jvm(ex);
future.fail(ex);
return null;
}));
return future;
}
@SuppressWarnings("all")
static Future toUpsertFuture(final T entity, final String pojo,
final Supplier> supplier,
final Function updateFun) {
return Fn.match(
Fn.fork(() -> Future.succeededFuture(To.toJson(entity, pojo))
.compose(item -> null == updateFun ?
Future.succeededFuture(item) :
Future.succeededFuture(updateFun.apply(item))
)
),
Fn.branch(null == entity, supplier));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy