查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel;
17  
18  import io.netty.util.concurrent.AbstractFuture;
19  import io.netty.util.concurrent.Future;
20  import io.netty.util.concurrent.GenericFutureListener;
21  
22  import java.util.concurrent.TimeUnit;
23  
24  final class VoidChannelPromise extends AbstractFuture<Void> implements ChannelPromise {
25  
26      private final Channel channel;
27      private final boolean fireException;
28  
29      /**
30       * Creates a new instance.
31       *
32       * @param channel the {@link Channel} associated with this future
33       */
34      VoidChannelPromise(Channel channel, boolean fireException) {
35          if (channel == null) {
36              throw new NullPointerException("channel");
37          }
38          this.channel = channel;
39          this.fireException = fireException;
40      }
41  
42      @Override
43      public VoidChannelPromise addListener(GenericFutureListener<? extends Future<? super Void>> listener) {
44          fail();
45          return this;
46      }
47  
48      @Override
49      public VoidChannelPromise addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners) {
50          fail();
51          return this;
52      }
53  
54      @Override
55      public VoidChannelPromise removeListener(GenericFutureListener<? extends Future<? super Void>> listener) {
56          // NOOP
57          return this;
58      }
59  
60      @Override
61      public VoidChannelPromise removeListeners(GenericFutureListener<? extends Future<? super Void>>... listeners) {
62          // NOOP
63          return this;
64      }
65  
66      @Override
67      public VoidChannelPromise await() throws InterruptedException {
68          if (Thread.interrupted()) {
69              throw new InterruptedException();
70          }
71          return this;
72      }
73  
74      @Override
75      public boolean await(long timeout, TimeUnit unit) {
76          fail();
77          return false;
78      }
79  
80      @Override
81      public boolean await(long timeoutMillis) {
82          fail();
83          return false;
84      }
85  
86      @Override
87      public VoidChannelPromise awaitUninterruptibly() {
88          fail();
89          return this;
90      }
91  
92      @Override
93      public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
94          fail();
95          return false;
96      }
97  
98      @Override
99      public boolean awaitUninterruptibly(long timeoutMillis) {
100         fail();
101         return false;
102     }
103 
104     @Override
105     public Channel channel() {
106         return channel;
107     }
108 
109     @Override
110     public boolean isDone() {
111         return false;
112     }
113 
114     @Override
115     public boolean isSuccess() {
116         return false;
117     }
118 
119     @Override
120     public boolean setUncancellable() {
121         return true;
122     }
123 
124     @Override
125     public boolean isCancellable() {
126         return false;
127     }
128 
129     @Override
130     public boolean isCancelled() {
131         return false;
132     }
133 
134     @Override
135     public Throwable cause() {
136         return null;
137     }
138 
139     @Override
140     public VoidChannelPromise sync() {
141         fail();
142         return this;
143     }
144 
145     @Override
146     public VoidChannelPromise syncUninterruptibly() {
147         fail();
148         return this;
149     }
150     @Override
151     public VoidChannelPromise setFailure(Throwable cause) {
152         fireException(cause);
153         return this;
154     }
155 
156     @Override
157     public VoidChannelPromise setSuccess() {
158         return this;
159     }
160 
161     @Override
162     public boolean tryFailure(Throwable cause) {
163         fireException(cause);
164         return false;
165     }
166 
167     /**
168      * {@inheritDoc}
169      *
170      * @param mayInterruptIfRunning this value has no effect in this implementation.
171      */
172     @Override
173     public boolean cancel(boolean mayInterruptIfRunning) {
174         return false;
175     }
176 
177     @Override
178     public boolean trySuccess() {
179         return false;
180     }
181 
182     private static void fail() {
183         throw new IllegalStateException("void future");
184     }
185 
186     @Override
187     public VoidChannelPromise setSuccess(Void result) {
188         return this;
189     }
190 
191     @Override
192     public boolean trySuccess(Void result) {
193         return false;
194     }
195 
196     @Override
197     public Void getNow() {
198         return null;
199     }
200 
201     private void fireException(Throwable cause) {
202         // Only fire the exception if the channel is open and registered
203         // if not the pipeline is not setup and so it would hit the tail
204         // of the pipeline.
205         // See https://github.com/netty/netty/issues/1517
206         if (fireException && channel.isRegistered()) {
207             channel.pipeline().fireExceptionCaught(cause);
208         }
209     }
210 }