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    *   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  /**
19   * Represents a class field in DNS protocol
20   */
21  public final class DnsClass implements Comparable<DnsClass> {
22  
23      /**
24       * Default class for DNS entries.
25       */
26      public static final DnsClass IN = new DnsClass(0x0001, "IN");
27      public static final DnsClass CSNET = new DnsClass(0x0002, "CSNET");
28      public static final DnsClass CHAOS = new DnsClass(0x0003, "CHAOS");
29      public static final DnsClass HESIOD = new DnsClass(0x0004, "HESIOD");
30      public static final DnsClass NONE = new DnsClass(0x00fe, "NONE");
31      public static final DnsClass ANY = new DnsClass(0x00ff, "ANY");
32  
33      private static final String EXPECTED =
34              " (expected: " +
35              IN + '(' + IN.intValue() + "), " +
36              CSNET + '(' + CSNET.intValue() + "), " +
37              CHAOS + '(' + CHAOS.intValue() + "), " +
38              HESIOD + '(' + HESIOD.intValue() + "), " +
39              NONE + '(' + NONE.intValue() + "), " +
40              ANY + '(' + ANY.intValue() + "))";
41  
42      public static DnsClass valueOf(String name) {
43          if (IN.name().equals(name)) {
44              return IN;
45          }
46          if (NONE.name().equals(name)) {
47              return NONE;
48          }
49          if (ANY.name().equals(name)) {
50              return ANY;
51          }
52          if (CSNET.name().equals(name)) {
53              return CSNET;
54          }
55          if (CHAOS.name().equals(name)) {
56              return CHAOS;
57          }
58          if (HESIOD.name().equals(name)) {
59              return HESIOD;
60          }
61  
62          throw new IllegalArgumentException("name: " + name + EXPECTED);
63      }
64  
65      public static DnsClass valueOf(int intValue) {
66          switch (intValue) {
67          case 0x0001:
68              return IN;
69          case 0x0002:
70              return CSNET;
71          case 0x0003:
72              return CHAOS;
73          case 0x0004:
74              return HESIOD;
75          case 0x00fe:
76              return NONE;
77          case 0x00ff:
78              return ANY;
79          default:
80              return new DnsClass(intValue, "UNKNOWN");
81          }
82      }
83  
84      /**
85       * Returns an instance of DnsClass for a custom type.
86       *
87       * @param clazz The class
88       * @param name The name
89       */
90      public static DnsClass valueOf(int clazz, String name) {
91          return new DnsClass(clazz, name);
92      }
93  
94      /**
95       * The protocol value of this DNS class
96       */
97      private final int intValue;
98  
99      /**
100      * The name of this DNS class
101      */
102     private final String name;
103 
104     private DnsClass(int intValue, String name) {
105         if ((intValue & 0xffff) != intValue) {
106             throw new IllegalArgumentException("intValue: " + intValue + " (expected: 0 ~ 65535)");
107         }
108 
109         this.intValue = intValue;
110         this.name = name;
111     }
112 
113     /**
114      * Returns the name of this class as used in bind config files
115      */
116     public String name() {
117         return name;
118     }
119 
120     /**
121      * Returns the protocol value represented by this class
122      */
123     public int intValue() {
124         return intValue;
125     }
126 
127     @Override
128     public int hashCode() {
129         return intValue;
130     }
131 
132     @Override
133     public boolean equals(Object o) {
134         return o instanceof DnsClass && ((DnsClass) o).intValue == intValue;
135     }
136 
137     @Override
138     public int compareTo(DnsClass o) {
139         return intValue() - o.intValue();
140     }
141 
142     @Override
143     public String toString() {
144         return name;
145     }
146 }