public final class

SettableFuture<V>

extends AbstractFuture<java.lang.Object>

 java.lang.Object

androidx.concurrent.futures.AbstractFuture<java.lang.Object>

↳androidx.concurrent.futures.SettableFuture<V>

Gradle dependencies

compile group: 'androidx.concurrent', name: 'futures', version: '1.0.0-alpha01'

  • groupId: androidx.concurrent
  • artifactId: futures
  • version: 1.0.0-alpha01

Artifact androidx.concurrent:futures:1.0.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

A whose result can be set by a SettableFuture.set(V), SettableFuture.setException(Throwable) or SettableFuture call. It can also, like any other Future, be cancelled. If your needs are more complex than SettableFuture supports, use AbstractFuture, which offers an extensible version of the API.

Summary

Methods
public static SettableFuture<java.lang.Object>create()

Creates a new SettableFuture that can be completed or cancelled by a later method call.

protected booleanset(java.lang.Object value)

Sets the result of this Future unless this Future has already been cancelled or set (including set asynchronously).

protected booleansetException(java.lang.Throwable throwable)

Sets the failed result of this Future unless this Future has already been cancelled or set (including set asynchronously).

protected booleansetFuture(<any> future)

Sets the result of this Future to match the supplied input Future once the supplied Future is done, unless this Future has already been cancelled or set (including "set asynchronously," defined below).

from AbstractFuture<V>addListener, afterDone, cancel, get, get, interruptTask, isCancelled, isDone, pendingToString, toString, wasInterrupted
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Methods

public static SettableFuture<java.lang.Object> create()

Creates a new SettableFuture that can be completed or cancelled by a later method call.

protected boolean set(java.lang.Object value)

Sets the result of this Future unless this Future has already been cancelled or set (including set asynchronously). When a call to this method returns, the Future is guaranteed to be done only if the call was accepted (in which case it returns true). If it returns false, the Future may have previously been set asynchronously, in which case its result may not be known yet. That result, though not yet known, cannot be overridden by a call to a set* method, only by a call to AbstractFuture.cancel(boolean).

Parameters:

value: the value to be used as the result

Returns:

true if the attempt was accepted, completing the Future

protected boolean setException(java.lang.Throwable throwable)

Sets the failed result of this Future unless this Future has already been cancelled or set (including set asynchronously). When a call to this method returns, the Future is guaranteed to be done only if the call was accepted (in which case it returns true). If it returns false, the Future may have previously been set asynchronously, in which case its result may not be known yet. That result, though not yet known, cannot be overridden by a call to a set* method, only by a call to AbstractFuture.cancel(boolean).

Parameters:

throwable: the exception to be used as the failed result

Returns:

true if the attempt was accepted, completing the Future

protected boolean setFuture(<any> future)

Sets the result of this Future to match the supplied input Future once the supplied Future is done, unless this Future has already been cancelled or set (including "set asynchronously," defined below).

If the supplied future is done when this method is called and the call is accepted, then this future is guaranteed to have been completed with the supplied future by the time this method returns. If the supplied future is not done and the call is accepted, then the future will be set asynchronously. Note that such a result, though not yet known, cannot be overridden by a call to a set* method, only by a call to AbstractFuture.cancel(boolean).

If the call setFuture(delegate) is accepted and this Future is later cancelled, cancellation will be propagated to delegate. Additionally, any call to setFuture after any cancellation will propagate cancellation to the supplied Future.

Note that, even if the supplied future is cancelled and it causes this future to complete, it will never trigger interruption behavior. In particular, it will not cause this future to invoke the AbstractFuture.interruptTask() method, and the AbstractFuture.wasInterrupted() method will not return true.

Parameters:

future: the future to delegate to

Returns:

true if the attempt was accepted, indicating that the Future was not previously cancelled or set.

Since: 19.0

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * 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 androidx.concurrent.futures;


import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;

import com.google.common.util.concurrent.ListenableFuture;

/**
 * A {@link ListenableFuture} whose result can be set by a {@link #set(Object)}, {@link
 * #setException(Throwable)} or {@link #setFuture(ListenableFuture)} call. It can also, like any
 * other {@code Future}, be {@linkplain #cancel cancelled}.
 *
 * If your needs are more complex than {@code SettableFuture} supports, use {@link
 * AbstractFuture}, which offers an extensible version of the API.
 *
 * @author Sven Mawson
 * @since 9.0 (in 1.0 as {@code ValueFuture})
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public final class SettableFuture<V> extends AbstractFuture<V> {
    /**
     * Creates a new {@code SettableFuture} that can be completed or cancelled by a later method
     * call.
     */
    public static <V> SettableFuture<V> create() {
        return new SettableFuture<V>();
    }

    @Override
    public boolean set(@Nullable V value) {
        return super.set(value);
    }

    @Override
    public boolean setException(Throwable throwable) {
        return super.setException(throwable);
    }

    @Override
    public boolean setFuture(ListenableFuture<? extends V> future) {
        return super.setFuture(future);
    }

    private SettableFuture() {
    }
}