View Javadoc
1   /*
2    * Copyright 2022 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  /**
19   * Provides a way to create {@link Future} and {@link Promise} instances.
20   */
21  public interface FuturePromiseFactory {
22  
23      /**
24       * Return a new {@link Promise}.
25       */
26      <V> Promise<V> newPromise();
27  
28      /**
29       * Create a new {@link Future} which is marked as succeeded already. So {@link Future#isSuccess()}
30       * will return {@code true}. All {@link FutureListener} added to it will be notified directly. Also
31       * every call of blocking methods will just return without blocking.
32       */
33      default <V> Future<V> newSucceededFuture(V result) {
34          return this.<V>newPromise().setSuccess(result).asFuture();
35      }
36  
37      /**
38       * Create a new {@link Future} which is marked as succeeded already. So {@link Future#isSuccess()}
39       * will return {@code true}. All {@link FutureListener} added to it will be notified directly. Also
40       * every call of blocking methods will just return without blocking.
41       */
42      default Future<Void> newSucceededFuture() {
43          return newSucceededFuture(null);
44      }
45  
46      /**
47       * Create a new {@link Future} which is marked as failed already. So {@link Future#isSuccess()}
48       * will return {@code false}. All {@link FutureListener} added to it will be notified directly. Also
49       * every call of blocking methods will just return without blocking.
50       */
51      default <V> Future<V> newFailedFuture(Throwable cause) {
52          return this.<V>newPromise().setFailure(cause).asFuture();
53      }
54  }