org.zodiac.sdk.simplenetty.concurrent.DefaultChannelPromise Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zodiac-sdk-nio Show documentation
Show all versions of zodiac-sdk-nio Show documentation
Zodiac SDK NIO2(New Non-Blocking IO)
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;
}
}