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 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 handle;
44      private Object msg;
45      private Promise<Void> promise;
46  
47      private PendingWrite(Recycler.Handle 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          return RECYCLER.recycle(this, handle);
58      }
59  
60      /**
61       * Fails the underlying {@link Promise} with the given cause and recycle this instance.
62       */
63      public boolean failAndRecycle(Throwable cause) {
64          ReferenceCountUtil.release(msg);
65          if (promise != null) {
66              promise.setFailure(cause);
67          }
68          return recycle();
69      }
70  
71      /**
72       * Mark the underlying {@link Promise} successfully and recycle this instance.
73       */
74      public boolean successAndRecycle() {
75          if (promise != null) {
76              promise.setSuccess(null);
77          }
78          return recycle();
79      }
80  
81      public Object msg() {
82          return msg;
83      }
84  
85      public Promise<Void> promise() {
86          return promise;
87      }
88  
89      /**
90       * Recycle this instance and return the {@link Promise}.
91       */
92      public Promise<Void> recycleAndGet() {
93          Promise<Void> promise = this.promise;
94          recycle();
95          return promise;
96      }
97  }