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

org.apache.cassandra.utils.concurrent.SyncPromise 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.0
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 SyncFuture} to implement the {@link Promise} interface.
 */
public class SyncPromise extends SyncFuture implements Promise
{
    public static class WithExecutor extends SyncPromise
    {
        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 SyncPromise() {}

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

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

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

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

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

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

    public static  SyncPromise uncancellable(GenericFutureListener> listener)
    {
        return new SyncPromise<>(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 SyncPromise addListener(GenericFutureListener> listener)
    {
        super.addListener(listener);
        return this;
    }

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

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

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

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

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

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

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

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

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





© 2015 - 2024 Weber Informatics LLC | Privacy Policy