View Javadoc
1   /*
2    * Copyright 2015 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    *   http://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.dns;
17  
18  import io.netty.util.internal.UnstableApi;
19  
20  import static io.netty.util.internal.ObjectUtil.checkNotNull;
21  
22  /**
23   * The DNS {@code OpCode} as defined in <a href="https://tools.ietf.org/html/rfc2929">RFC2929</a>.
24   */
25  @UnstableApi
26  public class DnsOpCode implements Comparable<DnsOpCode> {
27  
28      /**
29       * The 'Query' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
30       */
31      public static final DnsOpCode QUERY = new DnsOpCode(0x00, "QUERY");
32  
33      /**
34       * The 'IQuery' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
35       */
36      public static final DnsOpCode IQUERY = new DnsOpCode(0x01, "IQUERY");
37  
38      /**
39       * The 'Status' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
40       */
41      public static final DnsOpCode STATUS = new DnsOpCode(0x02, "STATUS");
42  
43      /**
44       * The 'Notify' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc1996">RFC1996</a>.
45       */
46      public static final DnsOpCode NOTIFY = new DnsOpCode(0x04, "NOTIFY");
47  
48      /**
49       * The 'Update' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
50       */
51      public static final DnsOpCode UPDATE = new DnsOpCode(0x05, "UPDATE");
52  
53      /**
54       * Returns the {@link DnsOpCode} instance of the specified byte value.
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          }
69  
70          return new DnsOpCode(b);
71      }
72  
73      private final byte byteValue;
74      private final String name;
75      private String text;
76  
77      private DnsOpCode(int byteValue) {
78          this(byteValue, "UNKNOWN");
79      }
80  
81      public DnsOpCode(int byteValue, String name) {
82          this.byteValue = (byte) byteValue;
83          this.name = checkNotNull(name, "name");
84      }
85  
86      public byte byteValue() {
87          return byteValue;
88      }
89  
90      @Override
91      public int hashCode() {
92          return byteValue;
93      }
94  
95      @Override
96      public boolean equals(Object obj) {
97          if (this == obj) {
98              return true;
99          }
100 
101         if (!(obj instanceof DnsOpCode)) {
102             return false;
103         }
104 
105         return byteValue == ((DnsOpCode) obj).byteValue;
106     }
107 
108     @Override
109     public int compareTo(DnsOpCode o) {
110         return byteValue - o.byteValue;
111     }
112 
113     @Override
114     public String toString() {
115         String text = this.text;
116         if (text == null) {
117             this.text = text = name + '(' + (byteValue & 0xFF) + ')';
118         }
119         return text;
120     }
121 }