All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cassandraunit.shaded.com.google.common.util.concurrent.TrustedListenableFutureTask Maven / Gradle / Ivy

There is a newer version: 4.3.1.0
Show newest version
/*
 * Copyright (C) 2014 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 com.google.common.util.concurrent;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.GwtCompatible;
import com.google.j2objc.annotations.WeakOuter;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.RunnableFuture;
import javax.annotation.Nullable;

/**
 * A {@link RunnableFuture} that also implements the {@link ListenableFuture} interface.
 *
 * 

This should be used in preference to {@link ListenableFutureTask} when possible for * performance reasons. */ @GwtCompatible class TrustedListenableFutureTask extends AbstractFuture.TrustedFuture implements RunnableFuture { /** * Creates a {@code ListenableFutureTask} that will upon running, execute the given * {@code Callable}. * * @param callable the callable task */ static TrustedListenableFutureTask create(Callable callable) { return new TrustedListenableFutureTask(callable); } /** * Creates a {@code ListenableFutureTask} that will upon running, execute the given * {@code Runnable}, and arrange that {@code get} will return the given result on successful * completion. * * @param runnable the runnable task * @param result the result to return on successful completion. If you don't need a particular * result, consider using constructions of the form: * {@code ListenableFuture f = ListenableFutureTask.create(runnable, * null)} */ static TrustedListenableFutureTask create(Runnable runnable, @Nullable V result) { return new TrustedListenableFutureTask(Executors.callable(runnable, result)); } /* * 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. */ private TrustedFutureInterruptibleTask task; TrustedListenableFutureTask(Callable callable) { this.task = new TrustedFutureInterruptibleTask(callable); } @Override public void run() { TrustedFutureInterruptibleTask localTask = task; if (localTask != null) { localTask.run(); } } @Override protected void afterDone() { super.afterDone(); if (wasInterrupted()) { TrustedFutureInterruptibleTask localTask = task; if (localTask != null) { localTask.interruptTask(); } } this.task = null; } @Override public String toString() { return super.toString() + " (delegate = " + task + ")"; } @WeakOuter private final class TrustedFutureInterruptibleTask extends InterruptibleTask { private final Callable callable; TrustedFutureInterruptibleTask(Callable callable) { this.callable = checkNotNull(callable); } @Override void runInterruptibly() { // Ensure we haven't been cancelled or already run. if (!isDone()) { try { set(callable.call()); } catch (Throwable t) { setException(t); } } } @Override boolean wasInterrupted() { return TrustedListenableFutureTask.this.wasInterrupted(); } @Override public String toString() { return callable.toString(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy