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  /**
20   * MQTT Message Types.
21   */
22  public enum MqttMessageType {
23      CONNECT(1),
24      CONNACK(2),
25      PUBLISH(3),
26      PUBACK(4),
27      PUBREC(5),
28      PUBREL(6),
29      PUBCOMP(7),
30      SUBSCRIBE(8),
31      SUBACK(9),
32      UNSUBSCRIBE(10),
33      UNSUBACK(11),
34      PINGREQ(12),
35      PINGRESP(13),
36      DISCONNECT(14),
37      AUTH(15);
38  
39      private static final MqttMessageType[] VALUES;
40  
41      static {
42          // this prevent values to be assigned with the wrong order
43          // and ensure valueOf to work fine
44          final MqttMessageType[] values = values();
45          VALUES = new MqttMessageType[values.length + 1];
46          for (MqttMessageType mqttMessageType : values) {
47              final int value = mqttMessageType.value;
48              if (VALUES[value] != null) {
49                  throw new AssertionError("value already in use: " + value);
50              }
51              VALUES[value] = mqttMessageType;
52          }
53      }
54  
55      private final int value;
56  
57      MqttMessageType(int value) {
58          this.value = value;
59      }
60  
61      public int value() {
62          return value;
63      }
64  
65      public static MqttMessageType valueOf(int type) {
66          if (type <= 0 || type >= VALUES.length) {
67              throw new IllegalArgumentException("unknown message type: " + type);
68          }
69          return VALUES[type];
70      }
71  }
72