com.landawn.abacus.android.util.Futures Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* 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.android.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.N;
import com.landawn.abacus.util.ObjIterator;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.Throwables;
import com.landawn.abacus.util.Tuple;
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;
// TODO: Auto-generated Javadoc
/**
*
* @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, new Throwables.Function, Future, Long, TimeUnit>, R, Exception>() {
@Override
public R apply(Tuple4, Future, Long, TimeUnit> t) throws Exception {
return 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, new Throwables.Function>, R, Exception>() {
@Override
public R apply(List> c) throws Exception {
return zipFunctionForGet.apply((Future) c.get(0), (Future) c.get(1));
}
}, new Throwables.Function>, Long, TimeUnit>, R, Exception>() {
@Override
public R apply(Tuple3>, Long, TimeUnit> t) throws Exception {
return 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, new Throwables.Function, Future, Future, Long, TimeUnit>, R, Exception>() {
@Override
public R apply(Tuple5, Future, Future, Long, TimeUnit> t) throws Exception {
return 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, new Throwables.Function>, R, Exception>() {
@Override
public R apply(List> c) throws Exception {
return zipFunctionForGet.apply((Future) c.get(0), (Future) c.get(1), (Future) c.get(2));
}
}, new Throwables.Function>, Long, TimeUnit>, R, Exception>() {
@Override
public R apply(Tuple3>, Long, TimeUnit> t) throws Exception {
return 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, new Throwables.Function, R, Exception>() {
@Override
public R apply(Tuple3 t) throws Exception {
return 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() == false) {
return false;
}
}
return true;
}
@Override
public R get() throws InterruptedException, ExecutionException {
try {
return zipFunctionForGet.apply(cfs);
} catch (InterruptedException | ExecutionException e) {
throw e;
} catch (Exception e) {
throw N.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 (InterruptedException | ExecutionException | TimeoutException e) {
throw e;
} catch (Exception e) {
throw N.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(new Throwables.Function, Tuple2, E>() {
@Override
public Tuple2 apply(List