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