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  /**
19   * Model the SubscriptionOption used in Subscribe MQTT v5 packet
20   */
21  public final class MqttSubscriptionOption {
22  
23      public enum RetainedHandlingPolicy {
24          SEND_AT_SUBSCRIBE(0),
25          SEND_AT_SUBSCRIBE_IF_NOT_YET_EXISTS(1),
26          DONT_SEND_AT_SUBSCRIBE(2);
27  
28          private final int value;
29  
30          RetainedHandlingPolicy(int value) {
31              this.value = value;
32          }
33  
34          public int value() {
35              return value;
36          }
37  
38          public static RetainedHandlingPolicy valueOf(int value) {
39              switch (value) {
40              case 0:
41                  return SEND_AT_SUBSCRIBE;
42              case 1:
43                  return SEND_AT_SUBSCRIBE_IF_NOT_YET_EXISTS;
44              case 2:
45                  return DONT_SEND_AT_SUBSCRIBE;
46              default:
47                  throw new IllegalArgumentException("invalid RetainedHandlingPolicy: " + value);
48              }
49          }
50      }
51  
52      private final MqttQoS qos;
53      private final boolean noLocal;
54      private final boolean retainAsPublished;
55      private final RetainedHandlingPolicy retainHandling;
56  
57      public static MqttSubscriptionOption onlyFromQos(MqttQoS qos) {
58          return new MqttSubscriptionOption(qos, false, false, RetainedHandlingPolicy.SEND_AT_SUBSCRIBE);
59      }
60  
61      public MqttSubscriptionOption(MqttQoS qos,
62                                    boolean noLocal,
63                                    boolean retainAsPublished,
64                                    RetainedHandlingPolicy retainHandling) {
65          this.qos = qos;
66          this.noLocal = noLocal;
67          this.retainAsPublished = retainAsPublished;
68          this.retainHandling = retainHandling;
69      }
70  
71      public MqttQoS qos() {
72          return qos;
73      }
74  
75      public boolean isNoLocal() {
76          return noLocal;
77      }
78  
79      public boolean isRetainAsPublished() {
80          return retainAsPublished;
81      }
82  
83      public RetainedHandlingPolicy retainHandling() {
84          return retainHandling;
85      }
86  
87      @Override
88      public boolean equals(Object o) {
89          if (this == o) {
90              return true;
91          }
92          if (o == null || getClass() != o.getClass()) {
93              return false;
94          }
95  
96          MqttSubscriptionOption that = (MqttSubscriptionOption) o;
97  
98          if (noLocal != that.noLocal) {
99              return false;
100         }
101         if (retainAsPublished != that.retainAsPublished) {
102             return false;
103         }
104         if (qos != that.qos) {
105             return false;
106         }
107         return retainHandling == that.retainHandling;
108     }
109 
110     @Override
111     public int hashCode() {
112         int result = qos.hashCode();
113         result = 31 * result + (noLocal ? 1 : 0);
114         result = 31 * result + (retainAsPublished ? 1 : 0);
115         result = 31 * result + retainHandling.hashCode();
116         return result;
117     }
118 
119     @Override
120     public String toString() {
121         return "SubscriptionOption[" +
122                 "qos=" + qos +
123                 ", noLocal=" + noLocal +
124                 ", retainAsPublished=" + retainAsPublished +
125                 ", retainHandling=" + retainHandling +
126                 ']';
127     }
128 }