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