netty5/codec-dns/src/main/java/io/netty/handler/codec/dns/DnsQuestion.java
Tim Boudreau 9734170b7d Use value types for class and type in DNS entries to make them immune to parameter order bugs
Motivation:

DNS class and type were represented as integers rather than an enum or a
similar dedicated value type.  This can be a potential source of a
parameter order bug which might be difficult to track down.

Modifications:

Add DnsClass and DnsType to replace integer parameters

Result:

Type safety and less error-proneness
2014-07-23 14:40:52 -07:00

73 lines
2.3 KiB
Java

/*
* Copyright 2013 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.dns;
import static io.netty.handler.codec.dns.DnsClass.IN;
/**
* The DNS question class which represents a question being sent to a server via
* a query, or the question being duplicated and sent back in a response.
* Usually a message contains a single question, and DNS servers often don't
* support multiple questions in a single query.
*/
public final class DnsQuestion extends DnsEntry {
/**
* Constructs a question with the default class IN (Internet).
*
* @param name
* the domain name being queried i.e. "www.example.com"
* @param type
* the question type, which represents the type of
* {@link DnsResource} record that should be returned
*/
public DnsQuestion(String name, DnsType type) {
this(name, type, IN);
}
/**
* Constructs a question with the given class.
*
* @param name
* the domain name being queried i.e. "www.example.com"
* @param type
* the question type, which represents the type of
* {@link DnsResource} record that should be returned
* @param qClass
* the class of a DNS record
*/
public DnsQuestion(String name, DnsType type, DnsClass qClass) {
super(name, type, qClass);
if (name.isEmpty()) {
throw new IllegalArgumentException("name must not be left blank.");
}
}
@Override
public boolean equals(Object other) {
if (!(other instanceof DnsQuestion)) {
return false;
}
return super.equals(other);
}
@Override
public int hashCode() {
return super.hashCode();
}
}