Added more CompletableFuture utils
This commit is contained in:
parent
894351e633
commit
4026d9bc9d
@ -12,16 +12,107 @@ import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
import org.warp.commonutils.functional.BiCompletableFunction;
|
||||
import org.warp.commonutils.functional.CompletableFunction;
|
||||
import org.warp.commonutils.functional.IOCompletableFunction;
|
||||
import org.warp.commonutils.functional.IOSupplier;
|
||||
import org.warp.commonutils.functional.TriCompletableFunction;
|
||||
import org.warp.commonutils.type.FloatPriorityQueue;
|
||||
import org.warp.commonutils.type.ScoredValue;
|
||||
|
||||
public class CompletableFutureUtils {
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <T> CompletableFuture<T> getCompletableFuture(Supplier<CompletableFuture<T>> completableFutureSupplier) {
|
||||
CompletableFuture<T> cf;
|
||||
try {
|
||||
cf = completableFutureSupplier.get();
|
||||
} catch (Exception ex) {
|
||||
cf = CompletableFuture.failedFuture(ex);
|
||||
}
|
||||
return cf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <F, T> CompletableFuture<T> getCompletableFuture(CompletableFunction<F, T> completableFutureFunction, F value) {
|
||||
return getCompletableFuture(() -> completableFutureFunction.apply(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <F, T> CompletableFuture<T> getCompletableFutureSupply(CompletableFunction<F, T> completableFutureFunction, Supplier<F> valueSupplier) {
|
||||
return getCompletableFuture(() -> completableFutureFunction.apply(valueSupplier.get()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <F1, F2, T> CompletableFuture<T> getCompletableFuture(BiCompletableFunction<F1, F2, T> completableFutureFunction, F1 value1, F2 value2) {
|
||||
return getCompletableFuture(() -> completableFutureFunction.apply(value1, value2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <F1, F2, T> CompletableFuture<T> getCompletableFutureSupply(BiCompletableFunction<F1, F2, T> completableFutureFunction, Supplier<F1> value1Supplier, Supplier<F2> value2Supplier) {
|
||||
return getCompletableFuture(() -> completableFutureFunction.apply(value1Supplier.get(), value2Supplier.get()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <F1, F2, F3, T> CompletableFuture<T> getCompletableFuture(TriCompletableFunction<F1, F2, F3, T> completableFutureFunction, F1 value1, F2 value2, F3 value3) {
|
||||
return getCompletableFuture(() -> completableFutureFunction.apply(value1, value2, value3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <F1, F2, F3, T> CompletableFuture<T> getCompletableFutureSupply(TriCompletableFunction<F1, F2, F3, T> completableFutureFunction, Supplier<F1> value1Supplier, Supplier<F2> value2Supplier, Supplier<F3> value3Supplier) {
|
||||
return getCompletableFuture(() -> completableFutureFunction.apply(value1Supplier.get(), value2Supplier.get(), value3Supplier.get()));
|
||||
}
|
||||
|
||||
////
|
||||
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <T> CompletableFuture<T> getCompletableFutureIO(IOSupplier<CompletableFuture<T>> completableFutureSupplier) {
|
||||
CompletableFuture<T> cf;
|
||||
try {
|
||||
cf = completableFutureSupplier.get();
|
||||
} catch (Exception ex) {
|
||||
cf = CompletableFuture.failedFuture(ex);
|
||||
}
|
||||
return cf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <F, T> CompletableFuture<T> getCompletableFutureIO(IOCompletableFunction<F, T> completableFutureFunction, F value) {
|
||||
return getCompletableFutureIO(() -> completableFutureFunction.apply(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
public static <F, T> CompletableFuture<T> getCompletableFutureIOSupply(IOCompletableFunction<F, T> completableFutureFunction, IOSupplier<F> valueSupplier) {
|
||||
return getCompletableFutureIO(() -> completableFutureFunction.apply(valueSupplier.get()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregate multiple {@link CompletableFuture} lists into a single {@link CompletableFuture} list
|
||||
*
|
||||
@ -244,12 +335,12 @@ public class CompletableFutureUtils {
|
||||
futureList.join().parallelStream().map(CompletableFuture::join).forEach(consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use CompletableFutureUtils.getCompletableFuture(supplier);
|
||||
*/
|
||||
@Deprecated
|
||||
public static <T> CompletableFuture<T> catchUncheckedExceptions(Supplier<CompletableFuture<T>> supplier) {
|
||||
try {
|
||||
return supplier.get();
|
||||
} catch (Exception exception) {
|
||||
return CompletableFuture.failedFuture(exception);
|
||||
}
|
||||
return getCompletableFuture(supplier);
|
||||
}
|
||||
|
||||
public static CompletableFuture<Void> runSequence(Collection<CompletableFuture<?>> collection) {
|
||||
@ -307,4 +398,10 @@ public class CompletableFutureUtils {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <U> CompletableFuture<U> composeAsync(
|
||||
Supplier<? extends CompletionStage<U>> supp,
|
||||
Executor executor) {
|
||||
return CompletableFuture.completedFuture(null).thenComposeAsync((_x) -> supp.get(), executor);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package org.warp.commonutils.concurrency.future;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.locks.StampedLock;
|
||||
import java.util.function.Supplier;
|
||||
@ -17,18 +16,11 @@ public class FutureLockUtils {
|
||||
} else {
|
||||
lockValue = 0;
|
||||
}
|
||||
try {
|
||||
return r.get().whenComplete((x, y) -> {
|
||||
if (lock != null) {
|
||||
lock.unlockRead(lockValue);
|
||||
}
|
||||
});
|
||||
} catch (Throwable ex) {
|
||||
return CompletableFutureUtils.getCompletableFuture(r).whenComplete((result, err) -> {
|
||||
if (lock != null) {
|
||||
lock.unlockRead(lockValue);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static <T> CompletableFuture<T> writeLock(@Nullable StampedLock lock, @NotNull Supplier<CompletableFuture<T>> r) {
|
||||
@ -38,59 +30,38 @@ public class FutureLockUtils {
|
||||
} else {
|
||||
lockValue = 0;
|
||||
}
|
||||
try {
|
||||
return r.get().whenComplete((x, y) -> {
|
||||
if (lock != null) {
|
||||
lock.unlockWrite(lockValue);
|
||||
}
|
||||
});
|
||||
} catch (Throwable ex) {
|
||||
return CompletableFutureUtils.getCompletableFuture(r).whenComplete((result, err) -> {
|
||||
if (lock != null) {
|
||||
lock.unlockWrite(lockValue);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static <T> CompletableFuture<T> readLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) throws IOException {
|
||||
public static <T> CompletableFuture<T> readLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) {
|
||||
long lockValue;
|
||||
if (lock != null) {
|
||||
lockValue = lock.readLock();
|
||||
} else {
|
||||
lockValue = 0;
|
||||
}
|
||||
try {
|
||||
return r.get().whenComplete((x, y) -> {
|
||||
if (lock != null) {
|
||||
lock.unlockRead(lockValue);
|
||||
}
|
||||
});
|
||||
} catch (Throwable ex) {
|
||||
return CompletableFutureUtils.getCompletableFutureIO(r).whenComplete((result, err) -> {
|
||||
if (lock != null) {
|
||||
lock.unlockRead(lockValue);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static <T> CompletableFuture<T> writeLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) throws IOException {
|
||||
public static <T> CompletableFuture<T> writeLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) {
|
||||
long lockValue;
|
||||
if (lock != null) {
|
||||
lockValue = lock.writeLock();
|
||||
} else {
|
||||
lockValue = 0;
|
||||
}
|
||||
try {
|
||||
return r.get().whenComplete((x, y) -> {
|
||||
if (lock != null) {
|
||||
lock.unlockWrite(lockValue);
|
||||
}
|
||||
});
|
||||
} catch (Throwable ex) {
|
||||
return CompletableFutureUtils.getCompletableFutureIO(r).whenComplete((result, err) -> {
|
||||
if (lock != null) {
|
||||
lock.unlockWrite(lockValue);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package org.warp.commonutils.functional;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface BiCompletableFunction<T1, T2, U> {
|
||||
CompletableFuture<U> apply(T1 value1, T2 value2);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package org.warp.commonutils.functional;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface CompletableFunction<T, U> {
|
||||
CompletableFuture<U> apply(T value);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package org.warp.commonutils.functional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface IOCompletableFunction<T, U> {
|
||||
CompletableFuture<U> apply(T value) throws IOException;
|
||||
}
|
@ -4,5 +4,5 @@ import java.io.IOException;
|
||||
|
||||
public interface IOFunction<T, U> {
|
||||
|
||||
U run(T data) throws IOException;
|
||||
U apply(T data) throws IOException;
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package org.warp.commonutils.functional;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface TriCompletableFunction<T1, T2, T3, U> {
|
||||
CompletableFuture<U> apply(T1 value1, T2 value2, T3 value3);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user