From 11c742f392d9168a095f2bee2068f249b2ea539e Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sat, 18 Aug 2012 22:53:58 +0900 Subject: [PATCH] [#59] Make ChannelFuture implement Future --- .../java/io/netty/channel/ChannelFuture.java | 5 ++- .../netty/channel/CompleteChannelFuture.java | 5 +++ .../netty/channel/DefaultChannelFuture.java | 33 +++++++++++++++++++ .../io/netty/channel/FailedChannelFuture.java | 14 ++++++++ .../netty/channel/SucceededChannelFuture.java | 15 +++++++++ .../io/netty/channel/VoidChannelFuture.java | 20 +++++++++++ .../channel/CompleteChannelFutureTest.java | 13 ++++++++ 7 files changed, 104 insertions(+), 1 deletion(-) diff --git a/transport/src/main/java/io/netty/channel/ChannelFuture.java b/transport/src/main/java/io/netty/channel/ChannelFuture.java index bda7155c49..98dca98a07 100644 --- a/transport/src/main/java/io/netty/channel/ChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/ChannelFuture.java @@ -15,6 +15,7 @@ */ package io.netty.channel; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** @@ -162,7 +163,7 @@ import java.util.concurrent.TimeUnit; * @apiviz.landmark * @apiviz.owns io.netty.channel.ChannelFutureListener - - notifies */ -public interface ChannelFuture { +public interface ChannelFuture extends Future { /** * 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, * or cancelled. */ + @Override boolean isDone(); /** * Returns {@code true} if and only if this future was * cancelled by a {@link #cancel()} method. */ + @Override boolean isCancelled(); /** diff --git a/transport/src/main/java/io/netty/channel/CompleteChannelFuture.java b/transport/src/main/java/io/netty/channel/CompleteChannelFuture.java index 8b43222350..8aef62c3a1 100644 --- a/transport/src/main/java/io/netty/channel/CompleteChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/CompleteChannelFuture.java @@ -121,6 +121,11 @@ public abstract class CompleteChannelFuture implements ChannelFuture { return false; } + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + return false; + } + @Override public boolean isCancelled() { return false; diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelFuture.java b/transport/src/main/java/io/netty/channel/DefaultChannelFuture.java index 4dfdba1ecc..2e9da87f11 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelFuture.java @@ -24,7 +24,9 @@ import java.nio.channels.Channels; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * The default {@link ChannelFuture} implementation. It is recommended to @@ -181,6 +183,32 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu 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() { Throwable cause = cause(); if (cause == null) { @@ -388,6 +416,11 @@ public class DefaultChannelFuture extends FlushCheckpoint implements ChannelFutu return true; } + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + return cancel(); + } + private void notifyListeners() { // This method doesn't need synchronization because: // 1) This method is always called after synchronized (this) block. diff --git a/transport/src/main/java/io/netty/channel/FailedChannelFuture.java b/transport/src/main/java/io/netty/channel/FailedChannelFuture.java index 3a796b9c99..573ff09982 100644 --- a/transport/src/main/java/io/netty/channel/FailedChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/FailedChannelFuture.java @@ -16,6 +16,9 @@ package io.netty.channel; 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 @@ -71,4 +74,15 @@ public class FailedChannelFuture extends CompleteChannelFuture { 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); + } } diff --git a/transport/src/main/java/io/netty/channel/SucceededChannelFuture.java b/transport/src/main/java/io/netty/channel/SucceededChannelFuture.java index 244dee5e78..e989d5fe44 100644 --- a/transport/src/main/java/io/netty/channel/SucceededChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/SucceededChannelFuture.java @@ -16,6 +16,9 @@ package io.netty.channel; 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 @@ -52,4 +55,16 @@ public class SucceededChannelFuture extends CompleteChannelFuture { public ChannelFuture syncUninterruptibly() { 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; + } + } diff --git a/transport/src/main/java/io/netty/channel/VoidChannelFuture.java b/transport/src/main/java/io/netty/channel/VoidChannelFuture.java index d98509e932..c9c8a7f2f2 100644 --- a/transport/src/main/java/io/netty/channel/VoidChannelFuture.java +++ b/transport/src/main/java/io/netty/channel/VoidChannelFuture.java @@ -15,7 +15,9 @@ */ package io.netty.channel; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class VoidChannelFuture implements ChannelFuture.Unsafe { @@ -120,6 +122,19 @@ public class VoidChannelFuture implements ChannelFuture.Unsafe { 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 public boolean setProgress(long amount, long current, long total) { return false; @@ -140,6 +155,11 @@ public class VoidChannelFuture implements ChannelFuture.Unsafe { return false; } + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + return false; + } + private static void fail() { throw new IllegalStateException("void future"); } diff --git a/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java b/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java index 378a8ef561..577b56aa7e 100644 --- a/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java +++ b/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java @@ -18,7 +18,9 @@ package io.netty.channel; import static org.easymock.EasyMock.*; import static org.junit.Assert.*; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import org.junit.Before; import org.junit.Test; @@ -86,5 +88,16 @@ public class CompleteChannelFutureTest { public ChannelFuture syncUninterruptibly() { 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(); + } } }