1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.codec.dns;
17
18 import io.netty5.util.internal.UnstableApi;
19
20 import static java.util.Objects.requireNonNull;
21
22
23
24
25 @UnstableApi
26 public class DnsOpCode implements Comparable<DnsOpCode> {
27
28
29
30
31 public static final DnsOpCode QUERY = new DnsOpCode(0x00, "QUERY");
32
33
34
35
36 public static final DnsOpCode IQUERY = new DnsOpCode(0x01, "IQUERY");
37
38
39
40
41 public static final DnsOpCode STATUS = new DnsOpCode(0x02, "STATUS");
42
43
44
45
46 public static final DnsOpCode NOTIFY = new DnsOpCode(0x04, "NOTIFY");
47
48
49
50
51 public static final DnsOpCode UPDATE = new DnsOpCode(0x05, "UPDATE");
52
53
54
55
56 public static DnsOpCode valueOf(int b) {
57 switch (b) {
58 case 0x00:
59 return QUERY;
60 case 0x01:
61 return IQUERY;
62 case 0x02:
63 return STATUS;
64 case 0x04:
65 return NOTIFY;
66 case 0x05:
67 return UPDATE;
68 default:
69 break;
70 }
71
72 return new DnsOpCode(b);
73 }
74
75 private final byte byteValue;
76 private final String name;
77 private String text;
78
79 private DnsOpCode(int byteValue) {
80 this(byteValue, "UNKNOWN");
81 }
82
83 public DnsOpCode(int byteValue, String name) {
84 this.byteValue = (byte) byteValue;
85 this.name = requireNonNull(name, "name");
86 }
87
88 public byte byteValue() {
89 return byteValue;
90 }
91
92 @Override
93 public int hashCode() {
94 return byteValue;
95 }
96
97 @Override
98 public boolean equals(Object obj) {
99 if (this == obj) {
100 return true;
101 }
102
103 if (!(obj instanceof DnsOpCode)) {
104 return false;
105 }
106
107 return byteValue == ((DnsOpCode) obj).byteValue;
108 }
109
110 @Override
111 public int compareTo(DnsOpCode o) {
112 return byteValue - o.byteValue;
113 }
114
115 @Override
116 public String toString() {
117 String text = this.text;
118 if (text == null) {
119 this.text = text = name + '(' + (byteValue & 0xFF) + ')';
120 }
121 return text;
122 }
123 }