[#59] Make ChannelFuture implement Future<Void>
This commit is contained in:
parent
0b11fb2ead
commit
11c742f392
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package io.netty.channel;
|
package io.netty.channel;
|
||||||
|
|
||||||
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -162,7 +163,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
* @apiviz.landmark
|
* @apiviz.landmark
|
||||||
* @apiviz.owns io.netty.channel.ChannelFutureListener - - notifies
|
* @apiviz.owns io.netty.channel.ChannelFutureListener - - notifies
|
||||||
*/
|
*/
|
||||||
public interface ChannelFuture {
|
public interface ChannelFuture extends Future<Void> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a channel where the I/O operation associated with this
|
* Returns a channel where the I/O operation associated with this
|
||||||
@ -175,12 +176,14 @@ public interface ChannelFuture {
|
|||||||
* complete, regardless of whether the operation was successful, failed,
|
* complete, regardless of whether the operation was successful, failed,
|
||||||
* or cancelled.
|
* or cancelled.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
boolean isDone();
|
boolean isDone();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns {@code true} if and only if this future was
|
* Returns {@code true} if and only if this future was
|
||||||
* cancelled by a {@link #cancel()} method.
|
* cancelled by a {@link #cancel()} method.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
boolean isCancelled();
|
boolean isCancelled();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -121,6 +121,11 @@ public abstract class CompleteChannelFuture implements ChannelFuture {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCancelled() {
|
public boolean isCancelled() {
|
||||||
return false;
|
return false;
|
||||||
|
@ -24,7 +24,9 @@ import java.nio.channels.Channels;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default {@link ChannelFuture} implementation. It is recommended to
|
* The default {@link ChannelFuture} implementation. It is recommended to
|
||||||
@ -181,6 +183,32 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get() throws InterruptedException, ExecutionException {
|
||||||
|
await();
|
||||||
|
Throwable cause = cause();
|
||||||
|
if (cause == null) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
throw new ExecutionException(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException,
|
||||||
|
TimeoutException {
|
||||||
|
if (!await(timeout, unit)) {
|
||||||
|
throw new TimeoutException();
|
||||||
|
}
|
||||||
|
|
||||||
|
Throwable cause = cause();
|
||||||
|
if (cause == null) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
throw new ExecutionException(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void rethrowIfFailed() {
|
private void rethrowIfFailed() {
|
||||||
Throwable cause = cause();
|
Throwable cause = cause();
|
||||||
if (cause == null) {
|
if (cause == null) {
|
||||||
@ -388,6 +416,11 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||||
|
return cancel();
|
||||||
|
}
|
||||||
|
|
||||||
private void notifyListeners() {
|
private void notifyListeners() {
|
||||||
// This method doesn't need synchronization because:
|
// This method doesn't need synchronization because:
|
||||||
// 1) This method is always called after synchronized (this) block.
|
// 1) This method is always called after synchronized (this) block.
|
||||||
|
@ -16,6 +16,9 @@
|
|||||||
package io.netty.channel;
|
package io.netty.channel;
|
||||||
|
|
||||||
import java.nio.channels.Channels;
|
import java.nio.channels.Channels;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link CompleteChannelFuture} which is failed already. It is
|
* The {@link CompleteChannelFuture} which is failed already. It is
|
||||||
@ -71,4 +74,15 @@ public class FailedChannelFuture extends CompleteChannelFuture {
|
|||||||
|
|
||||||
throw new ChannelException(cause);
|
throw new ChannelException(cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get() throws InterruptedException, ExecutionException {
|
||||||
|
throw new ExecutionException(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException,
|
||||||
|
TimeoutException {
|
||||||
|
throw new ExecutionException(cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,9 @@
|
|||||||
package io.netty.channel;
|
package io.netty.channel;
|
||||||
|
|
||||||
import java.nio.channels.Channels;
|
import java.nio.channels.Channels;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link CompleteChannelFuture} which is succeeded already. It is
|
* The {@link CompleteChannelFuture} which is succeeded already. It is
|
||||||
@ -52,4 +55,16 @@ public class SucceededChannelFuture extends CompleteChannelFuture {
|
|||||||
public ChannelFuture syncUninterruptibly() {
|
public ChannelFuture syncUninterruptibly() {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get() throws InterruptedException, ExecutionException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException,
|
||||||
|
TimeoutException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package io.netty.channel;
|
package io.netty.channel;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class VoidChannelFuture implements ChannelFuture.Unsafe {
|
public class VoidChannelFuture implements ChannelFuture.Unsafe {
|
||||||
|
|
||||||
@ -120,6 +122,19 @@ public class VoidChannelFuture implements ChannelFuture.Unsafe {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get() throws InterruptedException, ExecutionException {
|
||||||
|
fail();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException,
|
||||||
|
TimeoutException {
|
||||||
|
fail();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setProgress(long amount, long current, long total) {
|
public boolean setProgress(long amount, long current, long total) {
|
||||||
return false;
|
return false;
|
||||||
@ -140,6 +155,11 @@ public class VoidChannelFuture implements ChannelFuture.Unsafe {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private static void fail() {
|
private static void fail() {
|
||||||
throw new IllegalStateException("void future");
|
throw new IllegalStateException("void future");
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,9 @@ package io.netty.channel;
|
|||||||
import static org.easymock.EasyMock.*;
|
import static org.easymock.EasyMock.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -86,5 +88,16 @@ public class CompleteChannelFutureTest {
|
|||||||
public ChannelFuture syncUninterruptibly() {
|
public ChannelFuture syncUninterruptibly() {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get() throws InterruptedException, ExecutionException {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void get(long timeout, TimeUnit unit) throws InterruptedException,
|
||||||
|
ExecutionException, TimeoutException {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user