View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * Payload of the {@link MqttSubAckMessage}
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 }