View Javadoc
1   /*
2    * Copyright 2020 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  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   * Payload for MQTT unsuback message as in V5.
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  }