1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.mqtt;
18
19 import io.netty.util.internal.ObjectUtil;
20 import io.netty.util.internal.StringUtil;
21
22 import java.sql.Array;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27
28
29
30
31 public class MqttSubAckPayload {
32
33 private final List<MqttReasonCodes.SubAck> reasonCodes;
34
35 public MqttSubAckPayload(int... reasonCodes) {
36 ObjectUtil.checkNotNull(reasonCodes, "reasonCodes");
37
38 List<MqttReasonCodes.SubAck> list = new ArrayList<MqttReasonCodes.SubAck>(reasonCodes.length);
39 for (int v: reasonCodes) {
40 list.add(MqttReasonCodes.SubAck.valueOf((byte) (v & 0xFF)));
41 }
42 this.reasonCodes = Collections.unmodifiableList(list);
43 }
44
45 public MqttSubAckPayload(MqttReasonCodes.SubAck... reasonCodes) {
46 ObjectUtil.checkNotNull(reasonCodes, "reasonCodes");
47
48 List<MqttReasonCodes.SubAck> list = new ArrayList<MqttReasonCodes.SubAck>(reasonCodes.length);
49 list.addAll(Arrays.asList(reasonCodes));
50 this.reasonCodes = Collections.unmodifiableList(list);
51 }
52
53 public MqttSubAckPayload(Iterable<Integer> reasonCodes) {
54 ObjectUtil.checkNotNull(reasonCodes, "reasonCodes");
55 List<MqttReasonCodes.SubAck> list = new ArrayList<MqttReasonCodes.SubAck>();
56 for (Integer v: reasonCodes) {
57 if (v == null) {
58 break;
59 }
60 list.add(MqttReasonCodes.SubAck.valueOf(v.byteValue()));
61 }
62 this.reasonCodes = Collections.unmodifiableList(list);
63 }
64
65 public List<Integer> grantedQoSLevels() {
66 List<Integer> qosLevels = new ArrayList<Integer>(reasonCodes.size());
67 for (MqttReasonCodes.SubAck code: reasonCodes) {
68 if ((code.byteValue() & 0xFF) > MqttQoS.EXACTLY_ONCE.value()) {
69 qosLevels.add(MqttQoS.FAILURE.value());
70 } else {
71 qosLevels.add(code.byteValue() & 0xFF);
72 }
73 }
74 return qosLevels;
75 }
76
77 public List<Integer> reasonCodes() {
78 return typedReasonCodesToOrdinal();
79 }
80
81 private List<Integer> typedReasonCodesToOrdinal() {
82 List<Integer> intCodes = new ArrayList<Integer>(reasonCodes.size());
83 for (MqttReasonCodes.SubAck code: reasonCodes) {
84 intCodes.add(code.byteValue() & 0xFF);
85 }
86 return intCodes;
87 }
88
89 public List<MqttReasonCodes.SubAck> typedReasonCodes() {
90 return reasonCodes;
91 }
92
93 @Override
94 public String toString() {
95 return new StringBuilder(StringUtil.simpleClassName(this))
96 .append('[')
97 .append("reasonCodes=").append(reasonCodes)
98 .append(']')
99 .toString();
100 }
101 }