View Javadoc
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.channel.ChannelFlushPromiseNotifier.FlushCheckpoint;
19  import io.netty.util.concurrent.DefaultPromise;
20  import io.netty.util.concurrent.EventExecutor;
21  import io.netty.util.concurrent.Future;
22  import io.netty.util.concurrent.GenericFutureListener;
23  
24  /**
25   * The default {@link ChannelPromise} implementation.  It is recommended to use {@link Channel#newPromise()} to create
26   * a new {@link ChannelPromise} rather than calling the constructor explicitly.
27   */
28  public class DefaultChannelPromise extends DefaultPromise<Void> implements ChannelPromise, FlushCheckpoint {
29  
30      private final Channel channel;
31      private long checkpoint;
32  
33      /**
34       * Creates a new instance.
35       *
36       * @param channel
37       *        the {@link Channel} associated with this future
38       */
39      public DefaultChannelPromise(Channel channel) {
40          this.channel = channel;
41      }
42  
43      /**
44       * Creates a new instance.
45       *
46       * @param channel
47       *        the {@link Channel} associated with this future
48       */
49      public DefaultChannelPromise(Channel channel, EventExecutor executor) {
50          super(executor);
51          this.channel = channel;
52      }
53  
54      @Override
55      protected EventExecutor executor() {
56          EventExecutor e = super.executor();
57          if (e == null) {
58              return channel().eventLoop();
59          } else {
60              return e;
61          }
62      }
63  
64      @Override
65      public Channel channel() {
66          return channel;
67      }
68  
69      @Override
70      public ChannelPromise setSuccess() {
71          return setSuccess(null);
72      }
73  
74      @Override
75      public ChannelPromise setSuccess(Void result) {
76          super.setSuccess(result);
77          return this;
78      }
79  
80      @Override
81      public boolean trySuccess() {
82          return trySuccess(null);
83      }
84  
85      @Override
86      public ChannelPromise setFailure(Throwable cause) {
87          super.setFailure(cause);
88          return this;
89      }
90  
91      @Override
92      public ChannelPromise addListener(GenericFutureListener<? extends Future<? super Void>> listener) {
93          super.addListener(listener);
94          return this;
95      }
96  
97      @Override
98      public ChannelPromise addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners) {
99          super.addListeners(listeners);
100         return this;
101     }
102 
103     @Override
104     public ChannelPromise removeListener(GenericFutureListener<? extends Future<? super Void>> listener) {
105         super.removeListener(listener);
106         return this;
107     }
108 
109     @Override
110     public ChannelPromise removeListeners(GenericFutureListener<? extends Future<? super Void>>... listeners) {
111         super.removeListeners(listeners);
112         return this;
113     }
114 
115     @Override
116     public ChannelPromise sync() throws InterruptedException {
117         super.sync();
118         return this;
119     }
120 
121     @Override
122     public ChannelPromise syncUninterruptibly() {
123         super.syncUninterruptibly();
124         return this;
125     }
126 
127     @Override
128     public ChannelPromise await() throws InterruptedException {
129         super.await();
130         return this;
131     }
132 
133     @Override
134     public ChannelPromise awaitUninterruptibly() {
135         super.awaitUninterruptibly();
136         return this;
137     }
138 
139     @Override
140     public long flushCheckpoint() {
141         return checkpoint;
142     }
143 
144     @Override
145     public void flushCheckpoint(long checkpoint) {
146         this.checkpoint = checkpoint;
147     }
148 
149     @Override
150     public ChannelPromise promise() {
151         return this;
152     }
153 
154     @Override
155     protected void checkDeadLock() {
156         if (channel().isRegistered()) {
157             super.checkDeadLock();
158         }
159     }
160 
161     @Override
162     public ChannelPromise unvoid() {
163         return this;
164     }
165 
166     @Override
167     public boolean isVoid() {
168         return false;
169     }
170 }