mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-05-14 20:20:38 +01:00
committed by
Cody Henthorne
parent
98d9b12438
commit
ebccc6db30
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.signal.core.util.concurrent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
/**
|
||||
* A future that allows you to have multiple ways to compute a result. If one fails, the calculation
|
||||
* will fall back to the next in the list.
|
||||
*
|
||||
* You will only see a failure if the last attempt in the list fails.
|
||||
*/
|
||||
public final class CascadingFuture<T> implements ListenableFuture<T> {
|
||||
|
||||
private static final String TAG = CascadingFuture.class.getSimpleName();
|
||||
|
||||
private SettableFuture<T> result;
|
||||
|
||||
public CascadingFuture(List<Callable<ListenableFuture<T>>> callables, ExceptionChecker exceptionChecker) {
|
||||
if (callables.isEmpty()) {
|
||||
throw new IllegalArgumentException("Must have at least one callable!");
|
||||
}
|
||||
|
||||
this.result = new SettableFuture<>();
|
||||
|
||||
doNext(new ArrayList<>(callables), exceptionChecker);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||
return result.cancel(mayInterruptIfRunning);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return result.isCancelled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDone() {
|
||||
return result.isDone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() throws ExecutionException, InterruptedException {
|
||||
return result.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException {
|
||||
return result.get(timeout, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(Listener<T> listener) {
|
||||
result.addListener(listener);
|
||||
}
|
||||
|
||||
private void doNext(List<Callable<ListenableFuture<T>>> callables, ExceptionChecker exceptionChecker) {
|
||||
Callable<ListenableFuture<T>> callable = callables.remove(0);
|
||||
try {
|
||||
ListenableFuture<T> future = callable.call();
|
||||
|
||||
future.addListener(new ListenableFuture.Listener<T>() {
|
||||
@Override
|
||||
public void onSuccess(T value) {
|
||||
result.set(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(ExecutionException e) {
|
||||
if (callables.isEmpty() || !exceptionChecker.shouldContinue(e)) {
|
||||
result.setException(e.getCause());
|
||||
} else if (!result.isCancelled()) {
|
||||
doNext(callables, exceptionChecker);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
if (callables.isEmpty() || !exceptionChecker.shouldContinue(e)) {
|
||||
result.setException(e.getCause());
|
||||
} else if (!result.isCancelled()) {
|
||||
doNext(callables, exceptionChecker);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface ExceptionChecker {
|
||||
boolean shouldContinue(Exception e);
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.signal.core.util.concurrent;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
/**
|
||||
* Lets you perform a simple transform on the result of a future that maps it to a different value.
|
||||
*/
|
||||
class FutureMapTransformer<Input, Output> implements ListenableFuture<Output> {
|
||||
|
||||
private final ListenableFuture<Input> future;
|
||||
private final FutureTransformers.Transformer<Input, Output> transformer;
|
||||
|
||||
FutureMapTransformer(ListenableFuture<Input> future, FutureTransformers.Transformer<Input, Output> transformer) {
|
||||
this.future = future;
|
||||
this.transformer = transformer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(Listener<Output> listener) {
|
||||
future.addListener(new Listener<Input>() {
|
||||
@Override
|
||||
public void onSuccess(Input result) {
|
||||
try {
|
||||
listener.onSuccess(transformer.transform(result));
|
||||
} catch (Exception e) {
|
||||
listener.onFailure(new ExecutionException(e));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(ExecutionException e) {
|
||||
listener.onFailure(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cancel(boolean b) {
|
||||
return future.cancel(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return future.isCancelled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDone() {
|
||||
return future.isDone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Output get() throws InterruptedException, ExecutionException {
|
||||
Input input = future.get();
|
||||
try {
|
||||
return transformer.transform(input);
|
||||
} catch (Exception e) {
|
||||
throw new ExecutionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Output get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||
Input input = future.get(l, timeUnit);
|
||||
try {
|
||||
return transformer.transform(input);
|
||||
} catch (Exception e) {
|
||||
throw new ExecutionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.signal.core.util.concurrent;
|
||||
|
||||
public final class FutureTransformers {
|
||||
|
||||
public static <Input, Output> ListenableFuture<Output> map(ListenableFuture<Input> future, Transformer<Input, Output> transformer) {
|
||||
return new FutureMapTransformer<>(future, transformer);
|
||||
}
|
||||
|
||||
public interface Transformer<Input, Output> {
|
||||
Output transform(Input a) throws Exception;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package org.signal.core.util;
|
||||
|
||||
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
public class LinkedBlockingLifoQueue<E> extends LinkedBlockingDeque<E> {
|
||||
@Override
|
||||
public void put(E runnable) throws InterruptedException {
|
||||
super.putFirst(runnable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E runnable) {
|
||||
super.addFirst(runnable);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean offer(E runnable) {
|
||||
super.addFirst(runnable);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user