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

org.apache.cassandra.utils.concurrent.AsyncPromise Maven / Gradle / Ivy

Go to download

The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.

There is a newer version: 5.0.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.cassandra.utils.concurrent;

import java.util.concurrent.Executor;
import java.util.function.Consumer;

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

import io.netty.util.concurrent.Future; // checkstyle: permit this import
import io.netty.util.concurrent.GenericFutureListener;

/**
 * Extends {@link AsyncFuture} to implement the {@link Promise} interface.
 */
public class AsyncPromise extends AsyncFuture implements Promise
{
    public static class WithExecutor extends AsyncPromise
    {
        final Executor notifyExecutor;
        protected WithExecutor(Executor notifyExecutor)
        {
            this.notifyExecutor = notifyExecutor;
        }

        protected WithExecutor(Executor notifyExecutor, FailureHolder initialState)
        {
            super(initialState);
            this.notifyExecutor = notifyExecutor;
        }

        protected WithExecutor(Executor notifyExecutor, GenericFutureListener> listener)
        {
            super(listener);
            this.notifyExecutor = notifyExecutor;
        }

        @Override
        public Executor notifyExecutor()
        {
            return notifyExecutor;
        }
    }

    public AsyncPromise() {}

    AsyncPromise(FailureHolder initialState)
    {
        super(initialState);
    }

    public AsyncPromise(GenericFutureListener> listener)
    {
        super(listener);
    }

    AsyncPromise(FailureHolder initialState, GenericFutureListener> listener)
    {
        super(initialState, listener);
    }

    public static  AsyncPromise withExecutor(Executor executor)
    {
        return new AsyncPromise.WithExecutor<>(executor);
    }

    public static  AsyncPromise uncancellable()
    {
        return new AsyncPromise<>(UNCANCELLABLE);
    }

    public static  AsyncPromise uncancellable(Executor executor)
    {
        return new WithExecutor<>(executor, UNCANCELLABLE);
    }

    public static  AsyncPromise uncancellable(GenericFutureListener> listener)
    {
        return new AsyncPromise<>(UNCANCELLABLE, listener);
    }

    /**
     * Complete the promise successfully if not already complete
     * @throws IllegalStateException if already set
     */
    @Override
    public Promise setSuccess(V v)
    {
        if (!trySuccess(v))
            throw new IllegalStateException("complete already: " + this);
        return this;
    }

    /**
     * Complete the promise successfully if not already complete
     * @return true iff completed promise
     */
    @Override
    public boolean trySuccess(V v)
    {
        return super.trySuccess(v);
    }

    /**
     * Complete the promise abnormally if not already complete
     * @throws IllegalStateException if already set
     */
    @Override
    public Promise setFailure(Throwable throwable)
    {
        if (!tryFailure(throwable))
            throw new IllegalStateException("complete already: " + this);
        return this;
    }

    /**
     * Complete the promise abnormally if not already complete
     * @return true iff completed promise
     */
    @Override
    public boolean tryFailure(Throwable throwable)
    {
        return super.tryFailure(throwable);
    }

    /**
     * Prevent a future caller from cancelling this promise
     * @return true if the promise is now uncancellable (whether or not we did this)
     */
    @Override
    public boolean setUncancellable()
    {
        return super.setUncancellable();
    }

    /**
     * Prevent a future caller from cancelling this promise
     * @return true iff this invocation set it to uncancellable, whether or not now uncancellable
     */
    @Override
    public boolean setUncancellableExclusive()
    {
        return super.setUncancellableExclusive();
    }

    @Override
    public boolean isUncancellable()
    {
        return super.isUncancellable();
    }

    /**
     * waits for completion; in case of failure rethrows the original exception without a new wrapping exception
     * so may cause problems for reporting stack traces
     */
    @Override
    public Promise sync() throws InterruptedException
    {
        super.sync();
        return this;
    }

    /**
     * waits for completion; in case of failure rethrows the original exception without a new wrapping exception
     * so may cause problems for reporting stack traces
     */
    @Override
    public Promise syncUninterruptibly()
    {
        super.syncUninterruptibly();
        return this;
    }

    @Override
    public AsyncPromise addListener(GenericFutureListener> listener)
    {
        super.addListener(listener);
        return this;
    }

    @Override
    public AsyncPromise addListeners(GenericFutureListener>... listeners)
    {
        super.addListeners(listeners);
        return this;
    }

    @Override
    public AsyncPromise removeListener(GenericFutureListener> listener)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public AsyncPromise removeListeners(GenericFutureListener>... listeners)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public AsyncPromise addCallback(FutureCallback callback)
    {
        super.addCallback(callback);
        return this;
    }

    @Override
    public AsyncPromise addCallback(FutureCallback callback, Executor executor)
    {
        super.addCallback(callback, executor);
        return this;
    }

    @Override
    public AsyncPromise addCallback(Consumer onSuccess, Consumer onFailure)
    {
        super.addCallback(onSuccess, onFailure);
        return this;
    }

    /**
     * Wait for this promise to complete
     * @throws InterruptedException if interrupted
     */
    @Override
    public AsyncPromise await() throws InterruptedException
    {
        super.await();
        return this;
    }

    /**
     * Wait uninterruptibly for this promise to complete
     */
    @Override
    public AsyncPromise awaitUninterruptibly()
    {
        super.awaitUninterruptibly();
        return this;
    }

    /**
     * Wait for this promise to complete, throwing any interrupt as an UncheckedInterruptedException
     * @throws UncheckedInterruptedException if interrupted
     */
    @Override
    public AsyncPromise awaitThrowUncheckedOnInterrupt() throws UncheckedInterruptedException
    {
        super.awaitThrowUncheckedOnInterrupt();
        return this;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy