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.StringUtil;
22
23 import java.net.SocketAddress;
24
25
26
27
28
29
30
31 public class DefaultAddressedEnvelope<M, A extends SocketAddress> implements AddressedEnvelope<M, A> {
32
33 private final M message;
34 private final A sender;
35 private final A recipient;
36
37
38
39
40
41 public DefaultAddressedEnvelope(M message, A recipient, A sender) {
42 if (message == null) {
43 throw new NullPointerException("message");
44 }
45
46 this.message = message;
47 this.sender = sender;
48 this.recipient = recipient;
49 }
50
51
52
53
54
55 public DefaultAddressedEnvelope(M message, A recipient) {
56 this(message, recipient, null);
57 }
58
59 @Override
60 public M content() {
61 return message;
62 }
63
64 @Override
65 public A sender() {
66 return sender;
67 }
68
69 @Override
70 public A recipient() {
71 return recipient;
72 }
73
74 @Override
75 public int refCnt() {
76 if (message instanceof ReferenceCounted) {
77 return ((ReferenceCounted) message).refCnt();
78 } else {
79 return 1;
80 }
81 }
82
83 @Override
84 public AddressedEnvelope<M, A> retain() {
85 ReferenceCountUtil.retain(message);
86 return this;
87 }
88
89 @Override
90 public AddressedEnvelope<M, A> retain(int increment) {
91 ReferenceCountUtil.retain(message, increment);
92 return this;
93 }
94
95 @Override
96 public boolean release() {
97 return ReferenceCountUtil.release(message);
98 }
99
100 @Override
101 public boolean release(int decrement) {
102 return ReferenceCountUtil.release(message, decrement);
103 }
104
105 @Override
106 public String toString() {
107 if (sender != null) {
108 return StringUtil.simpleClassName(this) +
109 '(' + sender + " => " + recipient + ", " + message + ')';
110 } else {
111 return StringUtil.simpleClassName(this) +
112 "(=> " + recipient + ", " + message + ')';
113 }
114 }
115 }