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  package io.netty.util.concurrent;
17  
18  import java.util.concurrent.Callable;
19  import java.util.concurrent.RunnableFuture;
20  
21  class PromiseTask<V> extends DefaultPromise<V> implements RunnableFuture<V> {
22  
23      static <T> Callable<T> toCallable(Runnable runnable, T result) {
24          return new RunnableAdapter<T>(runnable, result);
25      }
26  
27      private static final class RunnableAdapter<T> implements Callable<T> {
28          final Runnable task;
29          final T result;
30  
31          RunnableAdapter(Runnable task, T result) {
32              this.task = task;
33              this.result = result;
34          }
35  
36          @Override
37          public T call() {
38              task.run();
39              return result;
40          }
41  
42          @Override
43          public String toString() {
44              return "Callable(task: " + task + ", result: " + result + ')';
45          }
46      }
47  
48      protected final Callable<V> task;
49  
50      PromiseTask(EventExecutor executor, Runnable runnable, V result) {
51          this(executor, toCallable(runnable, result));
52      }
53  
54      PromiseTask(EventExecutor executor, Callable<V> callable) {
55          super(executor);
56          task = callable;
57      }
58  
59      @Override
60      public final int hashCode() {
61          return System.identityHashCode(this);
62      }
63  
64      @Override
65      public final boolean equals(Object obj) {
66          return this == obj;
67      }
68  
69      @Override
70      public void run() {
71          try {
72              if (setUncancellableInternal()) {
73                  V result = task.call();
74                  setSuccessInternal(result);
75              }
76          } catch (Throwable e) {
77              setFailureInternal(e);
78          }
79      }
80  
81      @Override
82      public final Promise<V> setFailure(Throwable cause) {
83          throw new IllegalStateException();
84      }
85  
86      protected final Promise<V> setFailureInternal(Throwable cause) {
87          super.setFailure(cause);
88          return this;
89      }
90  
91      @Override
92      public final boolean tryFailure(Throwable cause) {
93          return false;
94      }
95  
96      protected final boolean tryFailureInternal(Throwable cause) {
97          return super.tryFailure(cause);
98      }
99  
100     @Override
101     public final Promise<V> setSuccess(V result) {
102         throw new IllegalStateException();
103     }
104 
105     protected final Promise<V> setSuccessInternal(V result) {
106         super.setSuccess(result);
107         return this;
108     }
109 
110     @Override
111     public final boolean trySuccess(V result) {
112         return false;
113     }
114 
115     protected final boolean trySuccessInternal(V result) {
116         return super.trySuccess(result);
117     }
118 
119     @Override
120     public final boolean setUncancellable() {
121         throw new IllegalStateException();
122     }
123 
124     protected final boolean setUncancellableInternal() {
125         return super.setUncancellable();
126     }
127 
128     @Override
129     protected StringBuilder toStringBuilder() {
130         StringBuilder buf = super.toStringBuilder();
131         buf.setCharAt(buf.length() - 1, ',');
132 
133         return buf.append(" task: ")
134                   .append(task)
135                   .append(')');
136     }
137 }