1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
53
54 public boolean recycle() {
55 msg = null;
56 promise = null;
57 return RECYCLER.recycle(this, handle);
58 }
59
60
61
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
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
91
92 public Promise<Void> recycleAndGet() {
93 Promise<Void> promise = this.promise;
94 recycle();
95 return promise;
96 }
97 }