Added name.parent(n) method to get the parent domain of a domain

This commit is contained in:
nathan 2018-03-04 13:45:24 +01:00
parent 49a8743c4e
commit 1f2b78da7d
2 changed files with 31 additions and 0 deletions

View File

@ -519,6 +519,25 @@ class Name implements Comparable, Serializable {
return newname;
}
/**
* Generates a new Name with the first n labels are removed
*
* @return The parent name
*/
public
Name parent(final int n) {
if (n < 1) {
throw new IllegalArgumentException("must remove 1 or more " + "labels");
}
try {
Name newname = new Name();
newname.append(name, offset(n), getlabels() - n);
return newname;
} catch (NameTooLongException e) {
throw new IllegalStateException("Name.subdomain: concatenate failed");
}
}
/**
* Generates a new Name with the first n labels replaced by a wildcard
*
@ -893,6 +912,7 @@ class Name implements Comparable, Serializable {
/**
* Computes a hashcode based on the value
*/
@Override
public
int hashCode() {
if (hashcode != 0) {
@ -909,6 +929,7 @@ class Name implements Comparable, Serializable {
/**
* Are these two Names equivalent?
*/
@Override
public
boolean equals(Object arg) {
if (arg == this) {
@ -938,6 +959,7 @@ class Name implements Comparable, Serializable {
*
* @return The representation of this name as a (printable) String.
*/
@Override
public
String toString() {
return toString(false);

View File

@ -814,6 +814,15 @@ class NameTest extends TestCase {
assertEquals(exp, n);
}
public
void test_parent() throws TextParseException {
Name dom = Name.fromString("a.b.c.");
Name exp = Name.fromString("b.c.");
Name n = dom.parent(1);
assertEquals(exp, n);
}
public
void test_wild_abs() throws TextParseException {
Name sub = Name.fromString("a.b.c.");