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.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  /**
27   * Payload of the {@link MqttSubAckMessage}
28   */
29  public class MqttSubAckPayload {
30  
31      private final List<Integer> reasonCodes;
32  
33      public MqttSubAckPayload(int... reasonCodes) {
34          ObjectUtil.checkNotNull(reasonCodes, "reasonCodes");
35  
36          List<Integer> list = new ArrayList<Integer>(reasonCodes.length);
37          for (int v: reasonCodes) {
38              list.add(v);
39          }
40          this.reasonCodes = Collections.unmodifiableList(list);
41      }
42  
43      public MqttSubAckPayload(Iterable<Integer> reasonCodes) {
44          ObjectUtil.checkNotNull(reasonCodes, "reasonCodes");
45          List<Integer> list = new ArrayList<Integer>();
46          for (Integer v: reasonCodes) {
47              if (v == null) {
48                  break;
49              }
50              list.add(v);
51          }
52          this.reasonCodes = Collections.unmodifiableList(list);
53      }
54  
55      public List<Integer> grantedQoSLevels() {
56          List<Integer> qosLevels = new ArrayList<Integer>(reasonCodes.size());
57          for (int code: reasonCodes) {
58              if (code > MqttQoS.EXACTLY_ONCE.value()) {
59                  qosLevels.add(MqttQoS.FAILURE.value());
60              } else {
61                  qosLevels.add(code);
62              }
63          }
64          return qosLevels;
65      }
66  
67      public List<Integer> reasonCodes() {
68          return reasonCodes;
69      }
70  
71      @Override
72      public String toString() {
73          return new StringBuilder(StringUtil.simpleClassName(this))
74              .append('[')
75              .append("reasonCodes=").append(reasonCodes)
76              .append(']')
77              .toString();
78      }
79  }