View Javadoc
1   /*
2    * Copyright 2016 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.internal;
17  
18  import io.netty5.util.concurrent.Promise;
19  import io.netty5.util.internal.logging.InternalLogger;
20  
21  /**
22   * Internal utilities to notify {@link Promise}s.
23   */
24  public final class PromiseNotificationUtil {
25  
26      private PromiseNotificationUtil() { }
27  
28      /**
29       * Try to cancel the {@link Promise} and log if {@code logger} is not {@code null} in case this fails.
30       */
31      public static void tryCancel(Promise<?> p, InternalLogger logger) {
32          if (!p.cancel() && logger != null) {
33              Throwable err = p.cause();
34              if (err == null) {
35                  logger.warn("Failed to cancel promise because it has succeeded already: {}", p);
36              } else {
37                  logger.warn(
38                          "Failed to cancel promise because it has failed already: {}, unnotified cause:",
39                          p, err);
40              }
41          }
42      }
43  
44      /**
45       * Try to mark the {@link Promise} as success and log if {@code logger} is not {@code null} in case this fails.
46       */
47      public static <V> void trySuccess(Promise<? super V> p, V result, InternalLogger logger) {
48          if (!p.trySuccess(result) && logger != null) {
49              Throwable err = p.cause();
50              if (err == null) {
51                  logger.warn("Failed to mark a promise as success because it has succeeded already: {}", p);
52              } else {
53                  logger.warn(
54                          "Failed to mark a promise as success because it has failed already: {}, unnotified cause:",
55                          p, err);
56              }
57          }
58      }
59  
60      /**
61       * Try to mark the {@link Promise} as failure and log if {@code logger} is not {@code null} in case this fails.
62       */
63      public static void tryFailure(Promise<?> p, Throwable cause, InternalLogger logger) {
64          if (!p.tryFailure(cause) && logger != null) {
65              Throwable err = p.cause();
66              if (err == null) {
67                  logger.warn("Failed to mark a promise as failure because it has succeeded already: {}", p, cause);
68              } else if (logger.isWarnEnabled()) {
69                  logger.warn(
70                          "Failed to mark a promise as failure because it has failed already: {}, unnotified cause: {}",
71                          p, ThrowableUtil.stackTraceToString(err), cause);
72              }
73          }
74      }
75  }