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