Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2006 The Guava Authors
*
* 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 org.testifyproject.guava.common.util.concurrent;
import static org.testifyproject.guava.common.base.Preconditions.checkNotNull;
import static org.testifyproject.guava.common.util.concurrent.Futures.getDone;
import static org.testifyproject.guava.common.util.concurrent.MoreExecutors.directExecutor;
import static org.testifyproject.guava.common.util.concurrent.MoreExecutors.rejectionPropagatingExecutor;
import static org.testifyproject.guava.common.util.concurrent.Platform.isInstanceOfThrowableClass;
import org.testifyproject.guava.common.annotations.GwtCompatible;
import org.testifyproject.guava.common.base.Function;
import org.testifyproject.guava.errorprone.annotations.ForOverride;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
/**
* Implementations of {@code Futures.catching*}.
*/
@GwtCompatible
abstract class AbstractCatchingFuture
extends AbstractFuture.TrustedFuture implements Runnable {
static ListenableFuture create(
ListenableFuture extends V> input,
Class exceptionType,
Function super X, ? extends V> fallback,
Executor executor) {
CatchingFuture future = new CatchingFuture<>(input, exceptionType, fallback);
input.addListener(future, rejectionPropagatingExecutor(executor, future));
return future;
}
static ListenableFuture create(
ListenableFuture extends V> input,
Class exceptionType,
AsyncFunction super X, ? extends V> fallback,
Executor executor) {
AsyncCatchingFuture future = new AsyncCatchingFuture<>(input, exceptionType, fallback);
input.addListener(future, rejectionPropagatingExecutor(executor, future));
return future;
}
/*
* In certain circumstances, this field might theoretically not be visible to an afterDone() call
* triggered by cancel(). For details, see the comments on the fields of TimeoutFuture.
*/
@Nullable ListenableFuture extends V> inputFuture;
@Nullable Class exceptionType;
@Nullable F fallback;
AbstractCatchingFuture(
ListenableFuture extends V> inputFuture, Class exceptionType, F fallback) {
this.inputFuture = checkNotNull(inputFuture);
this.exceptionType = checkNotNull(exceptionType);
this.fallback = checkNotNull(fallback);
}
@Override
public final void run() {
ListenableFuture extends V> localInputFuture = inputFuture;
Class localExceptionType = exceptionType;
F localFallback = fallback;
if (localInputFuture == null
| localExceptionType == null
| localFallback == null
| isCancelled()) {
return;
}
inputFuture = null;
exceptionType = null;
fallback = null;
// For an explanation of the cases here, see the comments on AbstractTransformFuture.run.
V sourceResult = null;
Throwable throwable = null;
try {
sourceResult = getDone(localInputFuture);
} catch (ExecutionException e) {
throwable = checkNotNull(e.getCause());
} catch (Throwable e) { // this includes cancellation exception
throwable = e;
}
if (throwable == null) {
set(sourceResult);
return;
}
if (!isInstanceOfThrowableClass(throwable, localExceptionType)) {
setException(throwable);
// TODO(cpovirk): Test that fallback is not run in this case.
return;
}
@SuppressWarnings("unchecked") // verified safe by isInstanceOfThrowableClass
X castThrowable = (X) throwable;
T fallbackResult;
try {
fallbackResult = doFallback(localFallback, castThrowable);
} catch (Throwable t) {
setException(t);
return;
}
setResult(fallbackResult);
}
@Override
protected String pendingToString() {
ListenableFuture extends V> localInputFuture = inputFuture;
Class localExceptionType = exceptionType;
F localFallback = fallback;
if (localInputFuture != null && localExceptionType != null && localFallback != null) {
return "input=["
+ localInputFuture
+ "], exceptionType=["
+ localExceptionType
+ "], fallback=["
+ localFallback
+ "]";
}
return null;
}
/** Template method for subtypes to actually run the fallback. */
@ForOverride
@Nullable
abstract T doFallback(F fallback, X throwable) throws Exception;
/** Template method for subtypes to actually set the result. */
@ForOverride
abstract void setResult(@Nullable T result);
@Override
protected final void afterDone() {
maybePropagateCancellationTo(inputFuture);
this.inputFuture = null;
this.exceptionType = null;
this.fallback = null;
}
/**
* An {@link AbstractCatchingFuture} that delegates to an {@link AsyncFunction} and
* {@link #setFuture(ListenableFuture)}.
*/
private static final class AsyncCatchingFuture
extends AbstractCatchingFuture<
V, X, AsyncFunction super X, ? extends V>, ListenableFuture extends V>> {
AsyncCatchingFuture(
ListenableFuture extends V> input,
Class exceptionType,
AsyncFunction super X, ? extends V> fallback) {
super(input, exceptionType, fallback);
}
@Override
ListenableFuture extends V> doFallback(
AsyncFunction super X, ? extends V> fallback, X cause) throws Exception {
ListenableFuture extends V> replacement = fallback.apply(cause);
checkNotNull(
replacement,
"AsyncFunction.apply returned null instead of a Future. "
+ "Did you mean to return immediateFuture(null)?");
return replacement;
}
@Override
void setResult(ListenableFuture extends V> result) {
setFuture(result);
}
}
/**
* An {@link AbstractCatchingFuture} that delegates to a {@link Function} and {@link
* #set(Object)}.
*/
private static final class CatchingFuture
extends AbstractCatchingFuture, V> {
CatchingFuture(
ListenableFuture extends V> input,
Class exceptionType,
Function super X, ? extends V> fallback) {
super(input, exceptionType, fallback);
}
@Override
@Nullable
V doFallback(Function super X, ? extends V> fallback, X cause) throws Exception {
return fallback.apply(cause);
}
@Override
void setResult(@Nullable V result) {
set(result);
}
}
}