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 2017, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package nl.topicus.jdbc.shaded.com.google.api.core;
import static nl.topicus.jdbc.shaded.com.google.common.util.concurrent.MoreExecutors.directExecutor;
import nl.topicus.jdbc.shaded.com.google.common.base.Function;
import nl.topicus.jdbc.shaded.com.google.common.collect.Iterables;
import nl.topicus.jdbc.shaded.com.google.common.util.concurrent.AsyncFunction;
import nl.topicus.jdbc.shaded.com.google.common.util.concurrent.FutureCallback;
import nl.topicus.jdbc.shaded.com.google.common.util.concurrent.Futures;
import nl.topicus.jdbc.shaded.com.google.common.util.concurrent.ListenableFuture;
import java.util.List;
import java.util.concurrent.Executor;
import nl.topicus.jdbc.shaded.javax.annotation.Nullable;
/** Static utility methods for the {@link ApiFuture} interface. */
public final class ApiFutures {
private ApiFutures() {}
public static void addCallback(
final ApiFuture future, final ApiFutureCallback callback) {
addCallback(future, callback, directExecutor());
}
public static void addCallback(
final ApiFuture future, final ApiFutureCallback callback, Executor executor) {
Futures.addCallback(
listenableFutureForApiFuture(future),
new FutureCallback() {
@Override
public void onFailure(Throwable t) {
callback.onFailure(t);
}
@Override
public void onSuccess(V v) {
callback.onSuccess(v);
}
},
executor);
}
public static ApiFuture catching(
ApiFuture input,
Class exceptionType,
ApiFunction callback) {
ListenableFuture catchingFuture =
Futures.catching(
listenableFutureForApiFuture(input),
exceptionType,
new GaxFunctionToGuavaFunction(callback));
return new ListenableFutureToApiFuture(catchingFuture);
}
public static ApiFuture immediateFuture(V value) {
return new ListenableFutureToApiFuture<>(Futures.immediateFuture(value));
}
public static ApiFuture immediateFailedFuture(Throwable throwable) {
return new ListenableFutureToApiFuture(Futures.immediateFailedFuture(throwable));
}
public static ApiFuture immediateCancelledFuture() {
return new ListenableFutureToApiFuture(Futures.immediateCancelledFuture());
}
public static ApiFuture transform(
ApiFuture input, final ApiFunction function) {
return new ListenableFutureToApiFuture<>(
Futures.transform(
listenableFutureForApiFuture(input), new GaxFunctionToGuavaFunction(function)));
}
public static ApiFuture> allAsList(
Iterable> futures) {
return new ListenableFutureToApiFuture<>(
Futures.allAsList(
Iterables.transform(
futures,
new Function, ListenableFuture>() {
public ListenableFuture apply(ApiFuture apiFuture) {
return listenableFutureForApiFuture(apiFuture);
}
})));
}
public static ApiFuture transformAsync(
ApiFuture input, final ApiAsyncFunction function) {
ListenableFuture listenableInput = listenableFutureForApiFuture(input);
ListenableFuture listenableOutput =
Futures.transformAsync(
listenableInput,
new AsyncFunction() {
@Override
public ListenableFuture apply(I input) throws Exception {
return listenableFutureForApiFuture(function.apply(input));
}
});
return new ListenableFutureToApiFuture<>(listenableOutput);
}
private static ListenableFuture listenableFutureForApiFuture(ApiFuture apiFuture) {
ListenableFuture listenableFuture;
if (apiFuture instanceof AbstractApiFuture) {
// prefer to use the wrapped ListenableFuture to reduce the number of layers
listenableFuture = ((AbstractApiFuture) apiFuture).getInternalListenableFuture();
} else {
listenableFuture = new ApiFutureToListenableFuture(apiFuture);
}
return listenableFuture;
}
private static class GaxFunctionToGuavaFunction
implements nl.topicus.jdbc.shaded.com.google.common.base.Function {
private ApiFunction f;
public GaxFunctionToGuavaFunction(ApiFunction f) {
this.f = f;
}
@Nullable
@Override
public V apply(@Nullable X input) {
return f.apply(input);
}
}
}