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<Short> 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<Short> list = new ArrayList<Short>(unsubscribeReasonCodes.length);
46          for (Short v: unsubscribeReasonCodes) {
47              list.add(v);
48          }
49          this.unsubscribeReasonCodes = Collections.unmodifiableList(list);
50      }
51  
52      public MqttUnsubAckPayload(Iterable<Short> unsubscribeReasonCodes) {
53          ObjectUtil.checkNotNull(unsubscribeReasonCodes, "unsubscribeReasonCodes");
54  
55          List<Short> list = new ArrayList<Short>();
56          for (Short v: unsubscribeReasonCodes) {
57              ObjectUtil.checkNotNull(v, "unsubscribeReasonCode");
58              list.add(v);
59          }
60          this.unsubscribeReasonCodes = Collections.unmodifiableList(list);
61      }
62  
63      public List<Short> unsubscribeReasonCodes() {
64          return unsubscribeReasonCodes;
65      }
66  
67      @Override
68      public String toString() {
69          return new StringBuilder(StringUtil.simpleClassName(this))
70                  .append('[')
71                  .append("unsubscribeReasonCodes=").append(unsubscribeReasonCodes)
72                  .append(']')
73                  .toString();
74      }
75  }