|
1 | 1 | package javasabr.mqtt.model.reason.code; |
2 | 2 |
|
3 | | -import java.util.stream.Stream; |
4 | | -import javasabr.rlib.common.util.ObjectUtils; |
| 3 | +import javasabr.rlib.common.util.NumberedEnum; |
| 4 | +import javasabr.rlib.common.util.NumberedEnumMap; |
| 5 | +import lombok.AccessLevel; |
5 | 6 | import lombok.Getter; |
6 | 7 | import lombok.RequiredArgsConstructor; |
| 8 | +import lombok.experimental.Accessors; |
| 9 | +import lombok.experimental.FieldDefaults; |
7 | 10 |
|
| 11 | +@Getter |
8 | 12 | @RequiredArgsConstructor |
9 | | -public enum DisconnectReasonCode { |
| 13 | +@Accessors(fluent = true, chain = false) |
| 14 | +@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) |
| 15 | +public enum DisconnectReasonCode implements NumberedEnum<DisconnectReasonCode> { |
10 | 16 | /** |
11 | 17 | * Close the connection normally. Do not send the Will Message. Client or Server. |
12 | 18 | */ |
13 | | - NORMAL_DISCONNECTION((byte) 0x00), |
| 19 | + NORMAL_DISCONNECTION(0x00), |
14 | 20 | /** |
15 | 21 | * The Client wishes to disconnect but requires that the Server also publishes its Will Message. Client. |
16 | 22 | */ |
17 | | - DISCONNECT_WITH_WILL_MESSAGE((byte) 0x04), |
| 23 | + DISCONNECT_WITH_WILL_MESSAGE(0x04), |
18 | 24 |
|
19 | 25 | // ERRORS |
20 | 26 |
|
21 | 27 | /** |
22 | 28 | * The Connection is closed but the sender either does not wish to reveal the reason, or none of the other Reason |
23 | 29 | * Codes apply. Client or Server. |
24 | 30 | */ |
25 | | - UNSPECIFIED_ERROR((byte) 0x80), |
| 31 | + UNSPECIFIED_ERROR(0x80), |
26 | 32 | /** |
27 | 33 | * The received packet does not conform to this specification. Client or Server. |
28 | 34 | */ |
29 | | - MALFORMED_PACKET((byte) 0x81), |
| 35 | + MALFORMED_PACKET(0x81), |
30 | 36 | /** |
31 | 37 | * An unexpected or out of order packet was received. Client or Server. |
32 | 38 | */ |
33 | | - PROTOCOL_ERROR((byte) 0x82), |
| 39 | + PROTOCOL_ERROR(0x82), |
34 | 40 | /** |
35 | 41 | * The packet received is valid but cannot be processed by this implementation. Client or Server. |
36 | 42 | */ |
37 | | - IMPLEMENTATION_SPECIFIC_ERROR((byte) 0x83), |
| 43 | + IMPLEMENTATION_SPECIFIC_ERROR(0x83), |
38 | 44 | /** |
39 | 45 | * The request is not authorized. Server. |
40 | 46 | */ |
41 | | - NOT_AUTHORIZED((byte) 0x87), |
| 47 | + NOT_AUTHORIZED(0x87), |
42 | 48 | /** |
43 | 49 | * The Server is busy and cannot continue processing requests from this Client. Server. |
44 | 50 | */ |
45 | | - SERVER_BUSY((byte) 0x89), |
| 51 | + SERVER_BUSY(0x89), |
46 | 52 | /** |
47 | 53 | * The Server is shutting down. Server. |
48 | 54 | */ |
49 | | - SERVER_SHUTTING_DOWN((byte) 0x8B), |
| 55 | + SERVER_SHUTTING_DOWN(0x8B), |
50 | 56 | /** |
51 | 57 | * The Connection is closed because no packet has been received for 1.5 times the Keepalive time. Server. |
52 | 58 | */ |
53 | | - KEEP_ALIVE_TIMEOUT((byte) 0x8D), |
| 59 | + KEEP_ALIVE_TIMEOUT(0x8D), |
54 | 60 | /** |
55 | 61 | * Another Connection using the same ClientID has connected causing this Connection to be closed. Server. |
56 | 62 | */ |
57 | | - SESSION_TAKEN_OVER((byte) 0x8E), |
| 63 | + SESSION_TAKEN_OVER(0x8E), |
58 | 64 | /** |
59 | 65 | * The Topic Filter is correctly formed, but is not accepted by this Sever. Server. |
60 | 66 | */ |
61 | | - TOPIC_FILTER_INVALID((byte) 0x8F), |
| 67 | + TOPIC_FILTER_INVALID(0x8F), |
62 | 68 | /** |
63 | 69 | * The Topic Name is correctly formed, but is not accepted by this Client or Server. Client or Server. |
64 | 70 | */ |
65 | | - TOPIC_NAME_INVALID((byte) 0x90), |
| 71 | + TOPIC_NAME_INVALID(0x90), |
66 | 72 | /** |
67 | 73 | * The Client or Server has received more than Receive Maximum publication for which it has not sent PUBACK or |
68 | 74 | * PUBCOMP. Client or Server. |
69 | 75 | */ |
70 | | - RECEIVE_MAXIMUM_EXCEEDED((byte) 0x93), |
| 76 | + RECEIVE_MAXIMUM_EXCEEDED(0x93), |
71 | 77 | /** |
72 | 78 | * The Client or Server has received a PUBLISH packet containing a Topic Alias which is greater than the Maximum Topic |
73 | 79 | * Alias it sent in the CONNECT or CONNACK packet. Client or Server. |
74 | 80 | */ |
75 | | - TOPIC_ALIAS_INVALID((byte) 0x94), |
| 81 | + TOPIC_ALIAS_INVALID(0x94), |
76 | 82 | /** |
77 | 83 | * The packet size is greater than Maximum Packet Size for this Client or Server. Client or Server. |
78 | 84 | */ |
79 | | - PACKET_TOO_LARGE((byte) 0x95), |
| 85 | + PACKET_TOO_LARGE(0x95), |
80 | 86 | /** |
81 | 87 | * The received data rate is too high. Client or Server. |
82 | 88 | */ |
83 | | - MESSAGE_RATE_TOO_HIGH((byte) 0x96), |
| 89 | + MESSAGE_RATE_TOO_HIGH(0x96), |
84 | 90 | /** |
85 | 91 | * An implementation or administrative imposed limit has been exceeded. Client or Server. |
86 | 92 | */ |
87 | | - QUOTA_EXCEEDED((byte) 0x97), |
| 93 | + QUOTA_EXCEEDED(0x97), |
88 | 94 | /** |
89 | 95 | * The Connection is closed due to an administrative action. Client or Server. |
90 | 96 | */ |
91 | | - ADMINISTRATIVE_ACTION((byte) 0x98), |
| 97 | + ADMINISTRATIVE_ACTION(0x98), |
92 | 98 | /** |
93 | 99 | * The payload format does not match the one specified by the Payload Format Indicator. Client or Server. |
94 | 100 | */ |
95 | | - PAYLOAD_FORMAT_INVALID((byte) 0x99), |
| 101 | + PAYLOAD_FORMAT_INVALID(0x99), |
96 | 102 | /** |
97 | 103 | * The Server has does not support retained messages. Server. |
98 | 104 | */ |
99 | | - RETAIN_NOT_SUPPORTED((byte) 0x9A), |
| 105 | + RETAIN_NOT_SUPPORTED(0x9A), |
100 | 106 | /** |
101 | 107 | * The Client specified a QoS greater than the QoS specified in a Maximum QoS in the CONNACK. Server. |
102 | 108 | */ |
103 | | - QOS_NOT_SUPPORTED((byte) 0x9B), |
| 109 | + QOS_NOT_SUPPORTED(0x9B), |
104 | 110 | /** |
105 | 111 | * The Client should temporarily change its Server. Server. |
106 | 112 | */ |
107 | | - USE_ANOTHER_SERVER((byte) 0x9C), |
| 113 | + USE_ANOTHER_SERVER(0x9C), |
108 | 114 | /** |
109 | 115 | * The Server is moved and the Client should permanently change its server location. Server. |
110 | 116 | */ |
111 | | - SERVER_MOVED((byte) 0x9D), |
| 117 | + SERVER_MOVED(0x9D), |
112 | 118 | /** |
113 | 119 | * The Server does not support Shared Subscriptions. Server. |
114 | 120 | */ |
115 | | - SHARED_SUBSCRIPTIONS_NOT_SUPPORTED((byte) 0x9E), |
| 121 | + SHARED_SUBSCRIPTIONS_NOT_SUPPORTED(0x9E), |
116 | 122 | /** |
117 | 123 | * This connection is closed because the connection rate is too high. Server. |
118 | 124 | */ |
119 | | - CONNECTION_RATE_EXCEEDED((byte) 0x9F), |
| 125 | + CONNECTION_RATE_EXCEEDED(0x9F), |
120 | 126 | /** |
121 | 127 | * The maximum connection time authorized for this connection has been exceeded. Server. |
122 | 128 | */ |
123 | | - MAXIMUM_CONNECT_TIME((byte) 0xA0), |
| 129 | + MAXIMUM_CONNECT_TIME(0xA0), |
124 | 130 | /** |
125 | 131 | * The Server does not support Subscription Identifiers; the subscription is not accepted. Server. |
126 | 132 | */ |
127 | | - SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED((byte) 0xA1), |
| 133 | + SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED(0xA1), |
128 | 134 | /** |
129 | 135 | * The Server does not support Wildcard Subscriptions; the subscription is not accepted. Server. |
130 | 136 | */ |
131 | | - WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED((byte) 0xA2); |
| 137 | + WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED(0xA2); |
132 | 138 |
|
133 | | - private static final DisconnectReasonCode[] VALUES; |
| 139 | + private static final NumberedEnumMap<DisconnectReasonCode> NUMBERED_MAP = |
| 140 | + new NumberedEnumMap<>(DisconnectReasonCode.class); |
134 | 141 |
|
135 | | - static { |
136 | | - |
137 | | - var maxId = Stream |
138 | | - .of(values()) |
139 | | - .mapToInt(DisconnectReasonCode::getValue) |
140 | | - .map(value -> Byte.toUnsignedInt((byte) value)) |
141 | | - .max() |
142 | | - .orElse(0); |
143 | | - |
144 | | - var values = new DisconnectReasonCode[maxId + 1]; |
145 | | - |
146 | | - for (var value : values()) { |
147 | | - values[Byte.toUnsignedInt(value.value)] = value; |
148 | | - } |
149 | | - |
150 | | - VALUES = values; |
| 142 | + public static DisconnectReasonCode ofCode(int code) { |
| 143 | + return NUMBERED_MAP.require(code); |
151 | 144 | } |
152 | 145 |
|
153 | | - public static DisconnectReasonCode of(int index) { |
154 | | - return ObjectUtils.notNull( |
155 | | - VALUES[index], |
156 | | - index, |
157 | | - arg -> new IndexOutOfBoundsException("Doesn't support reason code: " + arg)); |
158 | | - } |
| 146 | + int code; |
159 | 147 |
|
160 | | - private @Getter |
161 | | - final byte value; |
| 148 | + @Override |
| 149 | + public int number() { |
| 150 | + return code; |
| 151 | + } |
162 | 152 | } |
0 commit comments