1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.mqtt;
17
18 import io.netty.util.internal.ObjectUtil;
19 import io.netty.util.internal.StringUtil;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25
26
27
28 public final class MqttUnsubAckPayload {
29
30 private final List<MqttReasonCodes.UnsubAck> unsubscribeReasonCodes;
31
32 private static final MqttUnsubAckPayload EMPTY = new MqttUnsubAckPayload();
33
34 public static MqttUnsubAckPayload withEmptyDefaults(MqttUnsubAckPayload payload) {
35 if (payload == null) {
36 return EMPTY;
37 } else {
38 return payload;
39 }
40 }
41
42 public MqttUnsubAckPayload(short... unsubscribeReasonCodes) {
43 ObjectUtil.checkNotNull(unsubscribeReasonCodes, "unsubscribeReasonCodes");
44
45 List<MqttReasonCodes.UnsubAck> list = new ArrayList<MqttReasonCodes.UnsubAck>(unsubscribeReasonCodes.length);
46 for (Short v: unsubscribeReasonCodes) {
47 list.add(MqttReasonCodes.UnsubAck.valueOf((byte) (v & 0xFF)));
48 }
49 this.unsubscribeReasonCodes = Collections.unmodifiableList(list);
50 }
51
52 public MqttUnsubAckPayload(Iterable<Short> unsubscribeReasonCodes) {
53 ObjectUtil.checkNotNull(unsubscribeReasonCodes, "unsubscribeReasonCodes");
54
55 List<MqttReasonCodes.UnsubAck> list = new ArrayList<MqttReasonCodes.UnsubAck>();
56 for (Short v: unsubscribeReasonCodes) {
57 ObjectUtil.checkNotNull(v, "unsubscribeReasonCode");
58 list.add(MqttReasonCodes.UnsubAck.valueOf(v.byteValue()));
59 }
60 this.unsubscribeReasonCodes = Collections.unmodifiableList(list);
61 }
62
63 public List<Short> unsubscribeReasonCodes() {
64 return typedReasonCodesToOrdinal();
65 }
66
67 private List<Short> typedReasonCodesToOrdinal() {
68 List<Short> codes = new ArrayList<Short>(unsubscribeReasonCodes.size());
69 for (MqttReasonCodes.UnsubAck code: unsubscribeReasonCodes) {
70 codes.add((short) (code.byteValue() & 0xFF));
71 }
72 return codes;
73 }
74
75 public List<MqttReasonCodes.UnsubAck> typedReasonCodes() {
76 return unsubscribeReasonCodes;
77 }
78
79 @Override
80 public String toString() {
81 return new StringBuilder(StringUtil.simpleClassName(this))
82 .append('[')
83 .append("unsubscribeReasonCodes=").append(unsubscribeReasonCodes)
84 .append(']')
85 .toString();
86 }
87 }