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