me.hltj.vertx.FutureUtils Maven / Gradle / Ivy
Show all versions of vertx-future-utils Show documentation
/*
* vertx-future-utils - Convenient Utilities for Vert.x Future
* https://github.com/hltj/vertx-future-utils
*
* Copyright (C) 2020 JiaYanwei https://hltj.me
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* Please contact me (jiaywe#at#gmail.com, replace the '#at#' with 'at')
* if you need additional information or have any questions.
*/
package me.hltj.vertx;
import io.vertx.core.*;
import me.hltj.vertx.future.*;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Convenient Utilities for Vert.x {@link Future}.
*
*
* @author JiaYanwei
*/
public final class FutureUtils {
/**
* Convert a callback style Vert.x call to {@link Future} result style.
*
* @param consumer callback style Vert.x call
* @param the type parameter of the {@code AsyncResult}
* @return the {@code Future}
*/
public static Future futurize(Consumer>> consumer) {
Promise promise = Promise.promise();
consumer.accept(promise);
return promise.future();
}
/**
* If a {@link Future} succeed with null, map it with the default value.
*
* @param future the {@code Future}
* @param v0 the default value
* @param the type parameter of the {@code Future}
* @return the result {@code Future}
*/
public static Future defaultWith(Future future, T v0) {
return future.map(x -> x == null ? v0 : x);
}
/**
* If a {@link Future} succeed with null, map it with the default value.
*
* @param future the {@code Future}
* @param supplier a supplier to get the default value
* @param the type parameter of the {@code Future}
* @return the result {@code Future}
*/
public static Future defaultWith(Future future, Supplier supplier) {
return future.map(x -> x == null ? supplier.get() : x);
}
/**
* If a {@link Future} failed or succeed with null,
* replace it with a {@link Future} that succeed with the default value.
*
* @param future the {@code Future}
* @param v0 the default value
* @param the type parameter of the {@code Future}
* @return the result {@code Future}
*/
public static Future fallbackWith(Future future, T v0) {
return defaultWith(future.otherwise(v0), v0);
}
/**
* If a {@link Future} failed or succeed with null,
* replace it with a {@link Future} that succeed with the default value.
*
* @param future the {@code Future}
* @param function a function to get the default value
* @param the type parameter of the {@code Future}
* @return the result {@code Future}
*/
public static Future fallbackWith(Future future, Function, T> function) {
return fallbackWith(future, function.compose(Optional::of), () -> function.apply(Optional.empty()));
}
/**
* If a {@link Future} failed or succeed with null,
* replace it with a {@link Future} that succeed with the default value.
*
* @param future the {@code Future}
* @param mapper a function to get the default value on failure
* @param supplier a function to get the default value for replacing null
* @param the type parameter of the {@code Future}
* @return the result {@code Future}
*/
public static Future fallbackWith(Future future, Function mapper, Supplier supplier) {
return defaultWith(future.otherwise(mapper), supplier);
}
/**
* Wraps an evaluation result within {@link Future}.
*
* @param supplier the evaluation
* @param the result type of the evaluation
* @return succeed {@code Future} for main scenario and failed {@code Future} if a non-checked exception thrown
*/
public static Future wrap(Supplier supplier) {
try {
return Future.succeededFuture(supplier.get());
} catch (Throwable t) {
return Future.failedFuture(t);
}
}
/**
* Wraps a {@code function} application result within {@link Future}.
*
* @param v a value
* @param function a function applied to the value
* @param the type of the value
* @param the result type of the function
* @return succeed {@code Future} for main scenario and failed {@code Future} if a non-checked exception thrown
*/
public static Future wrap(T v, Function function) {
return wrap(() -> function.apply(v));
}
/**
* Alias for {@link FutureUtils#joinWrap(Supplier)}.
*/
public static Future flatWrap(Supplier> supplier) {
return joinWrap(supplier);
}
/**
* Wraps an evaluation result within {@link Future}, where the evaluation result itself is a {@link Future},
* the result will be join (also known as {@code flatten}) before return.
*
* @param supplier the evaluation
* @param the type parameter for the result {@code Future}
* @return the evaluated {@code Future} for main scenario and failed {@code Future} if a non-checked exception
* thrown
*/
public static Future joinWrap(Supplier> supplier) {
try {
return supplier.get();
} catch (Throwable t) {
return Future.failedFuture(t);
}
}
/**
* Alias for {@link FutureUtils#joinWrap(Object, Function)}.
*/
public static Future flatWrap(T v, Function> function) {
return joinWrap(v, function);
}
/**
* Wraps a {@code function} application result within {@link Future}, where the {@code function} itself return
* a {@link Future}, the result will be join (also known as {@code flatten}) before return.
*
* @param v a value
* @param function a function applied to the value
* @param the type of the value
* @param the type parameter for the result {@code Future}
* @return the function returned {@code Future} for main scenario and failed {@code Future} if a non-checked
* exception thrown
*/
public static Future joinWrap(T v, Function> function) {
return joinWrap(() -> function.apply(v));
}
/**
* Create a future tuple with two {@link Future}s.
*/
public static FutureTuple2 tuple(Future future0, Future future1) {
return FutureTuple2.of(future0, future1);
}
/**
* Create a future tuple with 3 {@link Future}s.
*/
public static FutureTuple3 tuple(
Future future0, Future future1, Future future2
) {
return FutureTuple3.of(future0, future1, future2);
}
/**
* Create a future tuple with 4 {@link Future}s.
*/
public static FutureTuple4 tuple(
Future future0, Future future1, Future future2, Future future3
) {
return FutureTuple4.of(future0, future1, future2, future3);
}
/**
* Create a future tuple with 5 {@link Future}s.
*/
public static FutureTuple5 tuple(
Future future0, Future future1, Future future2, Future future3, Future future4
) {
return FutureTuple5.of(future0, future1, future2, future3, future4);
}
/**
* Create a future tuple with 6 {@link Future}s.
*/
public static FutureTuple6 tuple(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5
) {
return FutureTuple6.of(future0, future1, future2, future3, future4, future5);
}
/**
* Create a future tuple with 7 {@link Future}s.
*/
public static FutureTuple7 tuple(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5, Future future6
) {
return FutureTuple7.of(future0, future1, future2, future3, future4, future5, future6);
}
/**
* Create a future tuple with 8 {@link Future}s.
*/
public static FutureTuple8 tuple(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5, Future future6, Future future7
) {
return FutureTuple8.of(future0, future1, future2, future3, future4, future5, future6, future7);
}
/**
* Create a future tuple with 9 {@link Future}s.
*/
public static FutureTuple9 tuple(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5, Future future6, Future future7, Future future8
) {
return FutureTuple9.of(future0, future1, future2, future3, future4, future5, future6, future7, future8);
}
/**
* Create a composite future tuple with two {@link Future}s and {@link CompositeFuture#all(Future, Future)}.
*/
public static CompositeFutureTuple2 all(Future future0, Future future1) {
return FutureTuple2.of(future0, future1).all();
}
/**
* Create a composite future tuple with 3 {@link Future}s and {@link CompositeFuture#all(Future, Future, Future)}.
*/
public static CompositeFutureTuple3 all(
Future future0, Future future1, Future future2
) {
return FutureTuple3.of(future0, future1, future2).all();
}
/**
* Create a composite future tuple with 4 {@link Future}s
* and {@link CompositeFuture#all(Future, Future, Future, Future)}.
*/
public static CompositeFutureTuple4 all(
Future future0, Future future1, Future future2, Future future3
) {
return FutureTuple4.of(future0, future1, future2, future3).all();
}
/**
* Create a composite future tuple with 5 {@link Future}s
* and {@link CompositeFuture#all(Future, Future, Future, Future, Future)}.
*/
public static CompositeFutureTuple5 all(
Future future0, Future future1, Future future2, Future future3, Future future4
) {
return FutureTuple5.of(future0, future1, future2, future3, future4).all();
}
/**
* Create a composite future tuple with 6 {@link Future}s
* and {@link CompositeFuture#all(Future, Future, Future, Future, Future, Future)}.
*/
public static CompositeFutureTuple6 all(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5
) {
return FutureTuple6.of(future0, future1, future2, future3, future4, future5).all();
}
/**
* Create a composite future tuple with 7 {@link Future}s and {@link CompositeFuture#all(List)}.
*/
public static CompositeFutureTuple7 all(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5, Future future6
) {
return FutureTuple7.of(future0, future1, future2, future3, future4, future5, future6).all();
}
/**
* Create a composite future tuple with 8 {@link Future}s and {@link CompositeFuture#all(List)}.
*/
public static CompositeFutureTuple8 all(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5, Future future6, Future future7
) {
return FutureTuple8.of(future0, future1, future2, future3, future4, future5, future6, future7).all();
}
/**
* Create a composite future tuple with 9 {@link Future}s and {@link CompositeFuture#all(List)}.
*/
public static CompositeFutureTuple9 all(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5, Future future6, Future future7, Future future8
) {
return FutureTuple9.of(future0, future1, future2, future3, future4, future5, future6, future7, future8).all();
}
/**
* Create a composite future tuple with two {@link Future}s {@link CompositeFuture#any(Future, Future)}.
*/
public static CompositeFutureTuple2 any(Future future0, Future future1) {
return FutureTuple2.of(future0, future1).any();
}
/**
* Create a composite future tuple with 3 {@link Future}s and {@link CompositeFuture#any(Future, Future, Future)}.
*/
public static CompositeFutureTuple3 any(
Future future0, Future future1, Future future2
) {
return FutureTuple3.of(future0, future1, future2).any();
}
/**
* Create a composite future tuple with 4 {@link Future}s
* and {@link CompositeFuture#any(Future, Future, Future, Future)}.
*/
public static CompositeFutureTuple4 any(
Future future0, Future future1, Future future2, Future future3
) {
return FutureTuple4.of(future0, future1, future2, future3).any();
}
/**
* Create a composite future tuple with 5 {@link Future}s
* and {@link CompositeFuture#any(Future, Future, Future, Future, Future)}.
*/
public static CompositeFutureTuple5 any(
Future future0, Future future1, Future future2, Future future3, Future future4
) {
return FutureTuple5.of(future0, future1, future2, future3, future4).any();
}
/**
* Create a composite future tuple with 6 {@link Future}s
* and {@link CompositeFuture#any(Future, Future, Future, Future, Future, Future)}.
*/
public static CompositeFutureTuple6 any(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5
) {
return FutureTuple6.of(future0, future1, future2, future3, future4, future5).any();
}
/**
* Create a composite future tuple with 7 {@link Future}s and {@link CompositeFuture#any(List)}.
*/
public static CompositeFutureTuple7 any(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5, Future future6
) {
return FutureTuple7.of(future0, future1, future2, future3, future4, future5, future6).any();
}
/**
* Create a composite future tuple with 8 {@link Future}s and {@link CompositeFuture#any(List)}.
*/
public static CompositeFutureTuple8 any(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5, Future future6, Future future7
) {
return FutureTuple8.of(future0, future1, future2, future3, future4, future5, future6, future7).any();
}
/**
* Create a composite future tuple with 9 {@link Future}s and {@link CompositeFuture#any(List)}.
*/
public static CompositeFutureTuple9 any(
Future future0, Future future1, Future future2, Future future3, Future future4,
Future future5, Future future6, Future future7, Future future8
) {
return FutureTuple9.of(future0, future1, future2, future3, future4, future5, future6, future7, future8).any();
}
/**
* Create a composite future tuple with two {@link Future}s and {@link CompositeFuture#join(Future, Future)}.
*/
public static CompositeFutureTuple2 join(Future future0, Future future1) {
return FutureTuple2.of(future0, future1).join();
}
/**
* Create a composite future tuple with 3 {@link Future}s and {@link CompositeFuture#join(Future, Future, Future)}.
*/
public static CompositeFutureTuple3 join(
Future future0, Future future1, Future future2
) {
return FutureTuple3.of(future0, future1, future2).join();
}
/**
* Create a composite future tuple with 4 {@link Future}s
* and {@link CompositeFuture#join(Future, Future, Future, Future)}.
*/
public static CompositeFutureTuple4 join(
Future future0, Future future1, Future future2, Future future3
) {
return FutureTuple4.of(future0, future1, future2, future3).join();
}
/**
* Create a composite future tuple with 5 {@link Future}s
* and {@link CompositeFuture#join(Future, Future, Future, Future, Future)}.
*/
public static CompositeFutureTuple5 join(
Future