1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.channel;
18
19 import io.netty.util.ReferenceCountUtil;
20 import io.netty.util.ReferenceCounted;
21 import io.netty.util.internal.ObjectUtil;
22 import io.netty.util.internal.StringUtil;
23
24 import java.net.SocketAddress;
25
26
27
28
29
30
31
32 public class DefaultAddressedEnvelope<M, A extends SocketAddress> implements AddressedEnvelope<M, A> {
33
34 private final M message;
35 private final A sender;
36 private final A recipient;
37
38
39
40
41
42 public DefaultAddressedEnvelope(M message, A recipient, A sender) {
43 ObjectUtil.checkNotNull(message, "message");
44 if (recipient == null && sender == null) {
45 throw new NullPointerException("recipient and sender");
46 }
47
48 this.message = message;
49 this.sender = sender;
50 this.recipient = recipient;
51 }
52
53
54
55
56
57 public DefaultAddressedEnvelope(M message, A recipient) {
58 this(message, recipient, null);
59 }
60
61 @Override
62 public M content() {
63 return message;
64 }
65
66 @Override
67 public A sender() {
68 return sender;
69 }
70
71 @Override
72 public A recipient() {
73 return recipient;
74 }
75
76 @Override
77 public int refCnt() {
78 if (message instanceof ReferenceCounted) {
79 return ((ReferenceCounted) message).refCnt();
80 } else {
81 return 1;
82 }
83 }
84
85 @Override
86 public AddressedEnvelope<M, A> retain() {
87 ReferenceCountUtil.retain(message);
88 return this;
89 }
90
91 @Override
92 public AddressedEnvelope<M, A> retain(int increment) {
93 ReferenceCountUtil.retain(message, increment);
94 return this;
95 }
96
97 @Override
98 public boolean release() {
99 return ReferenceCountUtil.release(message);
100 }
101
102 @Override
103 public boolean release(int decrement) {
104 return ReferenceCountUtil.release(message, decrement);
105 }
106
107 @Override
108 public AddressedEnvelope<M, A> touch() {
109 ReferenceCountUtil.touch(message);
110 return this;
111 }
112
113 @Override
114 public AddressedEnvelope<M, A> touch(Object hint) {
115 ReferenceCountUtil.touch(message, hint);
116 return this;
117 }
118
119 @Override
120 public String toString() {
121 if (sender != null) {
122 return StringUtil.simpleClassName(this) +
123 '(' + sender + " => " + recipient + ", " + message + ')';
124 } else {
125 return StringUtil.simpleClassName(this) +
126 "(=> " + recipient + ", " + message + ')';
127 }
128 }
129 }