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

org.zodiac.sdk.simplenetty.concurrent.DefaultChannelPromise Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.sdk.simplenetty.concurrent;

import java.util.List;
import java.util.Map;

import org.zodiac.sdk.simplenetty.channel.Channel;
import org.zodiac.sdk.simplenetty.core.EventExecutor;
import org.zodiac.sdk.simplenetty.listener.Listener;

public class DefaultChannelPromise extends AbstractChannelFuture implements ChannelPromise {

    public DefaultChannelPromise(EventExecutor executor, Channel channel, boolean isCancellable) {
        super(executor, channel, isCancellable);
    }

    @Override
    public ChannelPromise addListener(Listener listener, int op) {
        super.addListener(listener, op);
        return this;
    }

    @Override
    public ChannelPromise addListeners(Map, Integer> listeners) {
        super.addListeners(listeners);
        return this;
    }

    @Override
    public ChannelPromise removeListener(Listener listener) {
        super.removeListener(listener);
        return this;
    }

    @Override
    public ChannelPromise removeListeners(List> listeners) {
        super.removeListeners(listeners);
        return this;
    }

    @Override
    public ChannelPromise sync() throws InterruptedException {
        super.sync();
        return this;
    }

    @Override
    public ChannelPromise syncUninterruptibly() {
        super.syncUninterruptibly();
        return this;
    }

    @Override
    public ChannelPromise await() throws InterruptedException {
        super.await();
        return this;
    }

    @Override
    public ChannelPromise awaitUninterruptibly() {
        super.awaitUninterruptibly();
        return this;
    }

    @Override
    public boolean setUnCancellable() {
        super.isCancellable = false;
        return true;
    }

    @Override
    public ChannelPromise success(V result) {
        if (setSuccess0(result)) {
            return this;
        } else {
            String msg;
            int state = super.executor.getState();
            if (state == EventExecutor.NEW) {
                msg = "task has not start running";
            } else {
                msg = "task has been completed";
            }
            throw new IllegalStateException(msg);
        }
    }

    @Override
    public boolean trySuccess(V result) {
        return this.setSuccess0(result);
    }

    private boolean setSuccess0(V result) {
        boolean success = super.executor.setState(EventExecutor.RUNNING, EventExecutor.NORMAL);
        if (success) {
            super.executor.setResult(result);
        }
        return success;
    }

    @Override
    public ChannelPromise setFail(Throwable throwable) {
        if (setFail0(throwable)) {
            return this;
        } else {
            throw new IllegalStateException("task has been completed");
        }
    }

    @Override
    public boolean tryFail(Throwable throwable) {
        return this.setFail0(throwable);
    }

    private boolean setFail0(Throwable throwable) {
        boolean success = super.executor.setState(EventExecutor.RUNNING, EventExecutor.EXCEPTIONAL);
        if (success) {
            super.executor.setThrowable(throwable);
        }
        return success;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy