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.netty.util.internal;
17  
18  import io.netty.util.Recycler;
19  import io.netty.util.ReferenceCountUtil;
20  import io.netty.util.concurrent.Promise;
21  import io.netty.util.internal.ObjectPool.Handle;
22  
23  /**
24   * Some pending write which should be picked up later.
25   */
26  public final class PendingWrite {
27      private static final Recycler<PendingWrite> RECYCLER =
28              new Recycler<PendingWrite>() {
29                  @Override
30                  protected PendingWrite newObject(Handle<PendingWrite> handle) {
31                      return new PendingWrite(handle);
32                  }
33              };
34  
35      /**
36       * Create a new empty {@link RecyclableArrayList} instance
37       */
38      public static PendingWrite newInstance(Object msg, Promise<Void> promise) {
39          PendingWrite pending = RECYCLER.get();
40          pending.msg = msg;
41          pending.promise = promise;
42          return pending;
43      }
44  
45      private final Handle<PendingWrite> handle;
46      private Object msg;
47      private Promise<Void> promise;
48  
49      private PendingWrite(Handle<PendingWrite> handle) {
50          this.handle = handle;
51      }
52  
53      /**
54       * Clear and recycle this instance.
55       */
56      public boolean recycle() {
57          msg = null;
58          promise = null;
59          handle.recycle(this);
60          return true;
61      }
62  
63      /**
64       * Fails the underlying {@link Promise} with the given cause and recycle this instance.
65       */
66      public boolean failAndRecycle(Throwable cause) {
67          ReferenceCountUtil.release(msg);
68          if (promise != null) {
69              promise.setFailure(cause);
70          }
71          return recycle();
72      }
73  
74      /**
75       * Mark the underlying {@link Promise} successfully and recycle this instance.
76       */
77      public boolean successAndRecycle() {
78          if (promise != null) {
79              promise.setSuccess(null);
80          }
81          return recycle();
82      }
83  
84      public Object msg() {
85          return msg;
86      }
87  
88      public Promise<Void> promise() {
89          return promise;
90      }
91  
92      /**
93       * Recycle this instance and return the {@link Promise}.
94       */
95      public Promise<Void> recycleAndGet() {
96          Promise<Void> promise = this.promise;
97          recycle();
98          return promise;
99      }
100 }