1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.channel;
17
18 import io.netty5.util.internal.StringUtil;
19
20 import java.net.SocketAddress;
21
22 import static java.util.Objects.requireNonNull;
23
24
25
26
27
28
29
30 public class DefaultAddressedEnvelope<M, A extends SocketAddress> implements AddressedEnvelope<M, A> {
31
32 private final M message;
33 private final A sender;
34 private final A recipient;
35
36
37
38
39
40 public DefaultAddressedEnvelope(M message, A recipient, A sender) {
41 requireNonNull(message, "message");
42
43 if (recipient == null && sender == null) {
44 throw new NullPointerException("recipient and sender");
45 }
46
47 this.message = message;
48 this.sender = sender;
49 this.recipient = recipient;
50 }
51
52
53
54
55
56 public DefaultAddressedEnvelope(M message, A recipient) {
57 this(message, recipient, null);
58 }
59
60 @Override
61 public M content() {
62 return message;
63 }
64
65 @Override
66 public A sender() {
67 return sender;
68 }
69
70 @Override
71 public A recipient() {
72 return recipient;
73 }
74
75 @Override
76 public String toString() {
77 if (sender != null) {
78 return StringUtil.simpleClassName(this) +
79 '(' + sender + " => " + recipient + ", " + message + ')';
80 } else {
81 return StringUtil.simpleClassName(this) +
82 "(=> " + recipient + ", " + message + ')';
83 }
84 }
85 }