com.landawn.abacus.util.Futures Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (C) 2017 HaiYang Li
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.landawn.abacus.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.landawn.abacus.util.Throwables.Function;
import com.landawn.abacus.util.Throwables.TriFunction;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.Tuple.Tuple4;
import com.landawn.abacus.util.Tuple.Tuple5;
import com.landawn.abacus.util.Tuple.Tuple6;
import com.landawn.abacus.util.Tuple.Tuple7;
/**
*
* @author Haiyang Li
* @since 0.9
*/
public final class Futures {
private Futures() {
// singleton.
}
/**
*
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param zipFunctionForGet
* @return
*/
public static ContinuableFuture compose(final Future cf1, final Future cf2,
final Throwables.BiFunction super Future, ? super Future, R, Exception> zipFunctionForGet) {
return compose(cf1, cf2, zipFunctionForGet,
(Function, Future, Long, TimeUnit>, R, Exception>) t -> zipFunctionForGet.apply(t._1, t._2));
}
/**
*
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param zipFunctionForGet
* @param zipFunctionTimeoutGet
* @return
*/
public static ContinuableFuture compose(final Future cf1, final Future cf2,
final Throwables.BiFunction super Future, ? super Future, R, Exception> zipFunctionForGet,
final Throwables.Function super Tuple4, Future, Long, TimeUnit>, R, Exception> zipFunctionTimeoutGet) {
final List> cfs = Arrays.asList(cf1, cf2);
return compose(cfs, c -> zipFunctionForGet.apply((Future) c.get(0), (Future) c.get(1)),
(Function>, Long, TimeUnit>, R, Exception>) t -> zipFunctionTimeoutGet
.apply(Tuple.of((Future) t._1.get(0), (Future) t._1.get(1), t._2, t._3)));
}
/**
*
* @param
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param cf3
* @param zipFunctionForGet
* @return
*/
public static ContinuableFuture compose(final Future cf1, final Future cf2, final Future cf3,
final Throwables.TriFunction super Future, ? super Future, ? super Future, R, Exception> zipFunctionForGet) {
return compose(cf1, cf2, cf3, zipFunctionForGet, t -> zipFunctionForGet.apply(t._1, t._2, t._3));
}
/**
*
* @param
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param cf3
* @param zipFunctionForGet
* @param zipFunctionTimeoutGet
* @return
*/
public static ContinuableFuture compose(final Future cf1, final Future cf2, final Future cf3,
final Throwables.TriFunction super Future, ? super Future, ? super Future, R, Exception> zipFunctionForGet,
final Throwables.Function super Tuple5, Future, Future, Long, TimeUnit>, R, Exception> zipFunctionTimeoutGet) {
final List> cfs = Arrays.asList(cf1, cf2, cf3);
return compose(cfs, c -> zipFunctionForGet.apply((Future) c.get(0), (Future) c.get(1), (Future) c.get(2)),
(Function>, Long, TimeUnit>, R, Exception>) t -> zipFunctionTimeoutGet
.apply(Tuple.of((Future) t._1.get(0), (Future) t._1.get(1), (Future) t._1.get(2), t._2, t._3)));
}
/**
*
* @param
* @param
* @param
* @param cfs
* @param zipFunctionForGet
* @return
*/
public static >, R> ContinuableFuture compose(final FC cfs,
final Throwables.Function super FC, R, Exception> zipFunctionForGet) {
return compose(cfs, zipFunctionForGet, (Function, R, Exception>) t -> zipFunctionForGet.apply(t._1));
}
/**
*
* @param
* @param
* @param
* @param cfs
* @param zipFunctionForGet
* @param zipFunctionTimeoutGet
* @return
*/
public static >, R> ContinuableFuture compose(final FC cfs,
final Throwables.Function super FC, R, Exception> zipFunctionForGet,
final Throwables.Function super Tuple3, R, Exception> zipFunctionTimeoutGet) {
N.checkArgument(N.notNullOrEmpty(cfs), "'cfs' can't be null or empty");
N.checkArgNotNull(zipFunctionForGet);
N.checkArgNotNull(zipFunctionTimeoutGet);
return ContinuableFuture.wrap(new Future() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean res = true;
RuntimeException exception = null;
for (Future extends T> future : cfs) {
try {
res = res & future.cancel(mayInterruptIfRunning);
} catch (RuntimeException e) {
if (exception == null) {
exception = e;
} else {
exception.addSuppressed(e);
}
}
}
if (exception != null) {
throw exception;
}
return res;
}
@Override
public boolean isCancelled() {
for (Future> future : cfs) {
if (future.isCancelled()) {
return true;
}
}
return false;
}
@Override
public boolean isDone() {
for (Future> future : cfs) {
if (!future.isDone()) {
return false;
}
}
return true;
}
@Override
public R get() throws InterruptedException, ExecutionException {
try {
return zipFunctionForGet.apply(cfs);
} catch (Exception e) {
throw ExceptionUtil.toRuntimeException(e);
}
}
@Override
public R get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
final Tuple3 t = Tuple.of(cfs, timeout, unit);
try {
return zipFunctionTimeoutGet.apply(t);
} catch (Exception e) {
throw ExceptionUtil.toRuntimeException(e);
}
}
});
}
/**
*
* @param
* @param
* @param
* @param cf1
* @param cf2
* @return
*/
public static ContinuableFuture> combine(final Future extends T1> cf1, final Future extends T2> cf2) {
return allOf(Arrays.asList(cf1, cf2)).map(t -> Tuple.of((T1) t.get(0), (T2) t.get(1)));
}
/**
*
* @param
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param cf3
* @return
*/
public static ContinuableFuture> combine(final Future extends T1> cf1, final Future extends T2> cf2,
final Future extends T3> cf3) {
return allOf(Arrays.asList(cf1, cf2, cf3)).map(t -> Tuple.of((T1) t.get(0), (T2) t.get(1), (T3) t.get(2)));
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param cf3
* @param cf4
* @return
*/
public static ContinuableFuture> combine(final Future extends T1> cf1, final Future extends T2> cf2,
final Future extends T3> cf3, final Future extends T4> cf4) {
return allOf(Arrays.asList(cf1, cf2, cf3, cf4)).map(t -> Tuple.of((T1) t.get(0), (T2) t.get(1), (T3) t.get(2), (T4) t.get(3)));
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param cf3
* @param cf4
* @param cf5
* @return
*/
public static ContinuableFuture> combine(final Future extends T1> cf1, final Future extends T2> cf2,
final Future extends T3> cf3, final Future extends T4> cf4, final Future extends T5> cf5) {
return allOf(Arrays.asList(cf1, cf2, cf3, cf4, cf5)).map(t -> Tuple.of((T1) t.get(0), (T2) t.get(1), (T3) t.get(2), (T4) t.get(3), (T5) t.get(4)));
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param cf3
* @param cf4
* @param cf5
* @param cf6
* @return
*/
public static ContinuableFuture> combine(final Future extends T1> cf1,
final Future extends T2> cf2, final Future extends T3> cf3, final Future extends T4> cf4, final Future extends T5> cf5,
final Future extends T6> cf6) {
return allOf(Arrays.asList(cf1, cf2, cf3, cf4, cf5, cf6))
.map(t -> Tuple.of((T1) t.get(0), (T2) t.get(1), (T3) t.get(2), (T4) t.get(3), (T5) t.get(4), (T6) t.get(5)));
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param cf3
* @param cf4
* @param cf5
* @param cf6
* @param cf7
* @return
*/
public static ContinuableFuture> combine(final Future extends T1> cf1,
final Future extends T2> cf2, final Future extends T3> cf3, final Future extends T4> cf4, final Future extends T5> cf5,
final Future extends T6> cf6, final Future extends T7> cf7) {
return allOf(Arrays.asList(cf1, cf2, cf3, cf4, cf5, cf6, cf7))
.map(t -> Tuple.of((T1) t.get(0), (T2) t.get(1), (T3) t.get(2), (T4) t.get(3), (T5) t.get(4), (T6) t.get(5), (T7) t.get(6)));
}
/**
*
* @param
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param action
* @return
*/
public static ContinuableFuture combine(final Future extends T1> cf1, final Future extends T2> cf2,
final Throwables.BiFunction super T1, ? super T2, R, E> action) {
return allOf(Arrays.asList(cf1, cf2)).map(t -> action.apply((T1) t.get(0), (T2) t.get(1)));
}
/**
*
* @param
* @param
* @param
* @param
* @param
* @param cf1
* @param cf2
* @param cf3
* @param action
* @return
*/
public static ContinuableFuture combine(final Future extends T1> cf1, final Future extends T2> cf2,
final Future extends T3> cf3, final TriFunction super T1, ? super T2, ? super T3, R, E> action) {
return allOf(Arrays.asList(cf1, cf2, cf3)).map(t -> action.apply((T1) t.get(0), (T2) t.get(1), (T3) t.get(2)));
}
/**
*
* @param
* @param
* @param
* @param cfs
* @param action
* @return
*/
public static ContinuableFuture combine(final Collection extends Future extends T>> cfs,
final Throwables.Function, R, E> action) {
final ContinuableFuture> f = allOf(cfs);
return f.map(action);
}
// public static Future combine(final List extends Future extends T>> cfs, final Try.Function, R, E> action) {
// final Future> future = allOf(cfs);
// return future.thenApply(action);
// }
/**
* Returns a new Future that is completed when all of
* the given Futures complete. If any of the given
* Futures complete exceptionally, then the returned
* Future also does so.
*
* @param
* @param cfs
* @return
*/
@SafeVarargs
public static ContinuableFuture> allOf(final Future extends T>... cfs) {
return allOf2(Arrays.asList(cfs));
}
/**
* Returns a new Future that is completed when all of
* the given Futures complete. If any of the given
* Futures complete exceptionally, then the returned
* Future also does so.
*
* @param
* @param cfs
* @return
*/
public static ContinuableFuture> allOf(final Collection extends Future extends T>> cfs) {
return allOf2(cfs);
}
/**
* All of 2.
*
* @param
* @param cfs
* @return
*/
private static ContinuableFuture> allOf2(final Collection extends Future extends T>> cfs) {
N.checkArgument(N.notNullOrEmpty(cfs), "'cfs' can't be null or empty");
return ContinuableFuture.wrap(new Future>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean res = true;
RuntimeException exception = null;
for (Future extends T> future : cfs) {
try {
res = res & future.cancel(mayInterruptIfRunning);
} catch (RuntimeException e) {
if (exception == null) {
exception = e;
} else {
exception.addSuppressed(e);
}
}
}
if (exception != null) {
throw exception;
}
return res;
}
@Override
public boolean isCancelled() {
for (Future> future : cfs) {
if (future.isCancelled()) {
return true;
}
}
return false;
}
@Override
public boolean isDone() {
for (Future> future : cfs) {
if (!future.isDone()) {
return false;
}
}
return true;
}
@Override
public List get() throws InterruptedException, ExecutionException {
final List result = new ArrayList<>(cfs.size());
for (Future extends T> future : cfs) {
result.add(future.get());
}
return result;
}
@Override
public List get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
final long timeoutInMillis = unit.toMillis(timeout);
final long now = System.currentTimeMillis();
final long endTime = timeoutInMillis > Long.MAX_VALUE - now ? Long.MAX_VALUE : now + timeoutInMillis;
final List result = new ArrayList<>(cfs.size());
for (Future extends T> future : cfs) {
result.add(future.get(N.max(0, endTime - System.currentTimeMillis()), TimeUnit.MILLISECONDS));
}
return result;
}
});
}
/**
* Returns a new Future that, when any of the given Futures complete normally.
* If all of the given Futures complete exceptionally, then the returned Future also does so.
*
* @param
* @param cfs
* @return
*/
@SafeVarargs
public static ContinuableFuture anyOf(final Future extends T>... cfs) {
return anyOf2(Arrays.asList(cfs));
}
/**
* Returns a new Future that, when any of the given Futures complete normally.
* If all of the given Futures complete exceptionally, then the returned Future also does so.
*
* @param
* @param cfs
* @return
*/
public static ContinuableFuture anyOf(final Collection extends Future extends T>> cfs) {
return anyOf2(cfs);
}
/**
* Any of 2.
*
* @param
* @param cfs
* @return
*/
private static ContinuableFuture anyOf2(final Collection extends Future extends T>> cfs) {
N.checkArgument(N.notNullOrEmpty(cfs), "'cfs' can't be null or empty");
return ContinuableFuture.wrap(new Future() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean res = true;
RuntimeException exception = null;
for (Future extends T> future : cfs) {
try {
res = res & future.cancel(mayInterruptIfRunning);
} catch (RuntimeException e) {
if (exception == null) {
exception = e;
} else {
exception.addSuppressed(e);
}
}
}
if (exception != null) {
throw exception;
}
return res;
}
@Override
public boolean isCancelled() {
for (Future> future : cfs) {
if (!future.isCancelled()) {
return false;
}
}
return true;
}
@Override
public boolean isDone() {
for (Future> future : cfs) {
if (future.isDone()) {
return true;
}
}
return false;
}
@Override
public T get() throws InterruptedException, ExecutionException {
final Iterator> iter = iteratte(cfs);
Pair result = null;
while (iter.hasNext()) {
result = iter.next();
if (result.right == null) {
return result.left;
}
}
return handle(result);
}
@Override
public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
final Iterator> iter = iteratte(cfs, timeout, unit);
Pair result = null;
while (iter.hasNext()) {
result = iter.next();
if (result.right == null) {
return result.left;
}
}
return handle(result);
}
});
}
/**
*
* @param
* @param cfs
* @return
*/
@SafeVarargs
public static ObjIterator iterate(final Future extends T>... cfs) {
return iterate02(Arrays.asList(cfs));
}
/**
*
* @param
* @param cfs
* @return
*/
public static ObjIterator iterate(final Collection extends Future extends T>> cfs) {
return iterate02(cfs);
}
/**
*
* @param
* @param cfs
* @param totalTimeoutForAll
* @param unit
* @return
* @see {@code ExecutorCompletionService}
*/
public static ObjIterator iterate(final Collection extends Future extends T>> cfs, final long totalTimeoutForAll, final TimeUnit unit) {
return iterate02(cfs, totalTimeoutForAll, unit);
}
/**
*
* @param
* @param cfs
* @return
*/
private static ObjIterator iterate02(final Collection extends Future extends T>> cfs) {
return iterate02(cfs, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
/**
*
* @param
* @param cfs
* @param totalTimeoutForAll
* @param unit
* @return
*/
private static ObjIterator iterate02(final Collection extends Future extends T>> cfs, final long totalTimeoutForAll, final TimeUnit unit) {
return new ObjIterator<>() {
private final Iterator> iter = iterate22(cfs, totalTimeoutForAll, unit);
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public T next() {
final Pair result = iter.next();
if (result.right != null) {
throw ExceptionUtil.toRuntimeException(result.right);
}
return result.left;
}
};
}
/**
*
* @param
* @param cfs
* @return
*/
@SafeVarargs
public static ObjIterator> iteratte(final Future extends T>... cfs) {
return iterate22(Arrays.asList(cfs));
}
/**
*
* @param
* @param cfs
* @return
*/
public static ObjIterator> iteratte(final Collection extends Future extends T>> cfs) {
return iterate22(cfs);
}
/**
*
* @param
* @param cfs
* @param totalTimeoutForAll
* @param unit
* @return
* @see {@code ExecutorCompletionService}
*/
public static ObjIterator> iteratte(final Collection extends Future extends T>> cfs, final long totalTimeoutForAll,
final TimeUnit unit) {
return iterate22(cfs, totalTimeoutForAll, unit);
}
/**
*
* @param
* @param cfs
* @return
*/
private static ObjIterator> iterate22(final Collection extends Future extends T>> cfs) {
return iterate22(cfs, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
/**
*
* @param
* @param cfs
* @param totalTimeoutForAll
* @param unit
* @return
*/
private static ObjIterator> iterate22(final Collection extends Future extends T>> cfs, final long totalTimeoutForAll,
final TimeUnit unit) {
N.checkArgPositive(totalTimeoutForAll, "totalTimeoutForAll");
N.checkArgNotNull(unit, "unit");
final long now = System.currentTimeMillis();
final long totalTimeoutForAllInMillis = totalTimeoutForAll == Long.MAX_VALUE ? Long.MAX_VALUE : unit.toMillis(totalTimeoutForAll);
return new ObjIterator<>() {
private final Set> activeFutures = N.newSetFromMap(new IdentityHashMap, Boolean>());
{
activeFutures.addAll(cfs);
}
@Override
public boolean hasNext() {
return activeFutures.size() > 0;
}
@Override
public Pair next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
while (true) {
for (Future extends T> cf : activeFutures) {
if (cf.isDone()) {
try {
return Pair. of(cf.get(), null);
} catch (Exception e) {
return Pair.of(null, e);
} finally {
activeFutures.remove(cf);
}
}
}
if (System.currentTimeMillis() - now >= totalTimeoutForAllInMillis) {
return Pair. of(null, new TimeoutException());
}
N.sleepUninterruptibly(1);
}
}
};
}
/**
*
* @param
* @param result
* @return
* @throws InterruptedException the interrupted exception
* @throws ExecutionException the execution exception
*/
private static R handle(final Pair result) throws InterruptedException, ExecutionException {
if (result.right != null) {
if (result.right instanceof InterruptedException) {
throw ((InterruptedException) result.right);
} else if (result.right instanceof ExecutionException) {
throw ((ExecutionException) result.right);
} else {
throw ExceptionUtil.toRuntimeException(result.right);
}
}
return result.left;
}
}