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; 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 * 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 * Computes a hashcode based on the value
*/ */
@Override
public public
int hashCode() { int hashCode() {
if (hashcode != 0) { if (hashcode != 0) {
@ -909,6 +929,7 @@ class Name implements Comparable, Serializable {
/** /**
* Are these two Names equivalent? * Are these two Names equivalent?
*/ */
@Override
public public
boolean equals(Object arg) { boolean equals(Object arg) {
if (arg == this) { if (arg == this) {
@ -938,6 +959,7 @@ class Name implements Comparable, Serializable {
* *
* @return The representation of this name as a (printable) String. * @return The representation of this name as a (printable) String.
*/ */
@Override
public public
String toString() { String toString() {
return toString(false); return toString(false);

View File

@ -814,6 +814,15 @@ class NameTest extends TestCase {
assertEquals(exp, n); 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 public
void test_wild_abs() throws TextParseException { void test_wild_abs() throws TextParseException {
Name sub = Name.fromString("a.b.c."); Name sub = Name.fromString("a.b.c.");