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  package io.netty5.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<>(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     @Override
116     public final boolean setUncancellable() {
117         throw new IllegalStateException();
118     }
119 
120     protected final boolean setUncancellableInternal() {
121         return super.setUncancellable();
122     }
123 
124     @Override
125     public boolean cancel(boolean mayInterruptIfRunning) {
126         return cancel();
127     }
128 
129     @Override
130     protected StringBuilder toStringBuilder() {
131         StringBuilder buf = super.toStringBuilder();
132         buf.setCharAt(buf.length() - 1, ',');
133 
134         return buf.append(" task: ")
135                   .append(task)
136                   .append(')');
137     }
138 }