View Javadoc
1   /*
2    * Copyright 2013 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.StringUtil;
19  
20  /**
21   * A class representing entries in a DNS packet (questions, and all resource
22   * records). Contains data shared by entries such as name, type, and class.
23   */
24  public class DnsEntry {
25  
26      private final String name;
27      private final DnsType type;
28      private final DnsClass dnsClass;
29  
30      // only allow to extend from same package
31      DnsEntry(String name, DnsType type, DnsClass dnsClass) {
32          if (name == null) {
33              throw new NullPointerException("name");
34          }
35          if (type == null) {
36              throw new NullPointerException("type");
37          }
38          if (dnsClass == null) {
39              throw new NullPointerException("dnsClass");
40          }
41  
42          this.name = name;
43          this.type = type;
44          this.dnsClass = dnsClass;
45      }
46  
47      /**
48       * Returns the name of this entry (the domain).
49       */
50      public String name() {
51          return name;
52      }
53  
54      /**
55       * Returns the type of resource record to be received.
56       */
57      public DnsType type() {
58          return type;
59      }
60  
61      /**
62       * Returns the class for this entry. Default is IN (Internet).
63       */
64      public DnsClass dnsClass() {
65          return dnsClass;
66      }
67  
68      @Override
69      public int hashCode() {
70          return (name.hashCode() * 31 + type.hashCode()) * 31 + dnsClass.hashCode();
71      }
72  
73      @Override
74      public String toString() {
75          return new StringBuilder(128).append(StringUtil.simpleClassName(this))
76                                       .append("(name: ").append(name)
77                                       .append(", type: ").append(type)
78                                       .append(", class: ").append(dnsClass)
79                                       .append(')').toString();
80      }
81  
82      @Override
83      public boolean equals(Object o) {
84          if (this == o) {
85              return true;
86          }
87          if (!(o instanceof DnsEntry)) {
88              return false;
89          }
90  
91          DnsEntry that = (DnsEntry) o;
92          return type().intValue() == that.type().intValue() &&
93                 dnsClass().intValue() == that.dnsClass().intValue() &&
94                 name().equals(that.name());
95      }
96  }