View Javadoc
1   /*
2    * Copyright 2018 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 io.netty5.util.internal.StringUtil;
19  
20  import java.util.concurrent.Callable;
21  
22  import static java.util.Objects.requireNonNull;
23  
24  final class RunnableFutureAdapter<V> implements RunnableFuture<V> {
25  
26      private final Promise<V> promise;
27      private final Future<V> future;
28      private final Callable<V> task;
29  
30      RunnableFutureAdapter(Promise<V> promise, Callable<V> task) {
31          this.promise = requireNonNull(promise, "promise");
32          this.task = requireNonNull(task, "task");
33          future = promise.asFuture();
34      }
35  
36      @Override
37      public EventExecutor executor() {
38          return promise.executor();
39      }
40  
41      @Override
42      public boolean isSuccess() {
43          return promise.isSuccess();
44      }
45  
46      @Override
47      public boolean isFailed() {
48          return promise.isFailed();
49      }
50  
51      @Override
52      public boolean isCancellable() {
53          return promise.isCancellable();
54      }
55  
56      @Override
57      public Throwable cause() {
58          return promise.cause();
59      }
60  
61      @Override
62      public RunnableFuture<V> addListener(FutureListener<? super V> listener) {
63          future.addListener(listener);
64          return this;
65      }
66  
67      @Override
68      public <C> RunnableFuture<V> addListener(C context, FutureContextListener<? super C, ? super V> listener) {
69          future.addListener(context, listener);
70          return this;
71      }
72  
73      @Override
74      public V getNow() {
75          return promise.getNow();
76      }
77  
78      @Override
79      public void run() {
80          try {
81              if (promise.setUncancellable()) {
82                  V result = task.call();
83                  promise.setSuccess(result);
84              }
85          } catch (Throwable e) {
86              promise.setFailure(e);
87          }
88      }
89  
90      @Override
91      public boolean cancel() {
92          return future.cancel();
93      }
94  
95      @Override
96      public boolean isCancelled() {
97          return promise.isCancelled();
98      }
99  
100     @Override
101     public boolean isDone() {
102         return promise.isDone();
103     }
104 
105     @Override
106     public FutureCompletionStage<V> asStage() {
107         return future.asStage();
108     }
109 
110     @Override
111     public String toString() {
112         StringBuilder buf = new StringBuilder(64)
113                 .append(StringUtil.simpleClassName(this))
114                 .append('@')
115                 .append(Integer.toHexString(hashCode()));
116 
117         if (!isDone()) {
118             buf.append("(incomplete)");
119         } else {
120             Throwable cause = cause();
121             if (cause != null) {
122                 buf.append("(failure: ")
123                         .append(cause)
124                         .append(')');
125             } else {
126                 Object result = getNow();
127                 if (result == null) {
128                     buf.append("(success)");
129                 } else {
130                     buf.append("(success: ")
131                             .append(result)
132                             .append(')');
133                 }
134             }
135         }
136 
137         return buf.append(" task: ")
138                 .append(task)
139                 .append(')').toString();
140     }
141 }