View Javadoc
1   /*
2    * Copyright 2013 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    *   https://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  
17  package io.netty.util.concurrent;
18  
19  import io.netty.util.internal.ObjectUtil;
20  
21  import java.util.concurrent.TimeUnit;
22  
23  /**
24   * A skeletal {@link Future} implementation which represents a {@link Future} which has been completed already.
25   */
26  public abstract class CompleteFuture<V> extends AbstractFuture<V> {
27  
28      private final EventExecutor executor;
29  
30      /**
31       * Creates a new instance.
32       *
33       * @param executor the {@link EventExecutor} associated with this future
34       */
35      protected CompleteFuture(EventExecutor executor) {
36          this.executor = executor;
37      }
38  
39      /**
40       * Return the {@link EventExecutor} which is used by this {@link CompleteFuture}.
41       */
42      protected EventExecutor executor() {
43          return executor;
44      }
45  
46      @Override
47      public Future<V> addListener(GenericFutureListener<? extends Future<? super V>> listener) {
48          DefaultPromise.notifyListener(executor(), this, ObjectUtil.checkNotNull(listener, "listener"));
49          return this;
50      }
51  
52      @Override
53      public Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {
54          for (GenericFutureListener<? extends Future<? super V>> l:
55                  ObjectUtil.checkNotNull(listeners, "listeners")) {
56  
57              if (l == null) {
58                  break;
59              }
60              DefaultPromise.notifyListener(executor(), this, l);
61          }
62          return this;
63      }
64  
65      @Override
66      public Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener) {
67          // NOOP
68          return this;
69      }
70  
71      @Override
72      public Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {
73          // NOOP
74          return this;
75      }
76  
77      @Override
78      public Future<V> await() throws InterruptedException {
79          if (Thread.interrupted()) {
80              throw new InterruptedException();
81          }
82          return this;
83      }
84  
85      @Override
86      public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
87          if (Thread.interrupted()) {
88              throw new InterruptedException();
89          }
90          return true;
91      }
92  
93      @Override
94      public Future<V> sync() throws InterruptedException {
95          return this;
96      }
97  
98      @Override
99      public Future<V> syncUninterruptibly() {
100         return this;
101     }
102 
103     @Override
104     public boolean await(long timeoutMillis) throws InterruptedException {
105         if (Thread.interrupted()) {
106             throw new InterruptedException();
107         }
108         return true;
109     }
110 
111     @Override
112     public Future<V> awaitUninterruptibly() {
113         return this;
114     }
115 
116     @Override
117     public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
118         return true;
119     }
120 
121     @Override
122     public boolean awaitUninterruptibly(long timeoutMillis) {
123         return true;
124     }
125 
126     @Override
127     public boolean isDone() {
128         return true;
129     }
130 
131     @Override
132     public boolean isCancellable() {
133         return false;
134     }
135 
136     @Override
137     public boolean isCancelled() {
138         return false;
139     }
140 
141     /**
142      * {@inheritDoc}
143      *
144      * @param mayInterruptIfRunning this value has no effect in this implementation.
145      */
146     @Override
147     public boolean cancel(boolean mayInterruptIfRunning) {
148         return false;
149     }
150 }