Improve documentation of TLD extraction routines

Some confusion came up in #9 over the proper way to extract TLDs from
hostnames. This should hopefully alleviate that.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=118417091
This commit is contained in:
jart 2016-03-28 18:12:09 -07:00 committed by Justine Tunney
parent ec2daec412
commit 4442f4c31b
2 changed files with 20 additions and 5 deletions

View file

@ -20,16 +20,21 @@ import com.google.common.base.Strings;
import com.google.common.net.InternetDomainName; import com.google.common.net.InternetDomainName;
/** /**
* Contains static utility methods for dealing with domains. * Utility class for dealing with domains.
*/ */
public class DomainUtils { public final class DomainUtils {
/** /**
* Returns the canonicalized TLD part of a valid domain name (just an SLD, no subdomains) by * Returns the canonicalized TLD part of a valid domain name (just an SLD, no subdomains) by
* stripping off the leftmost part. * stripping off the leftmost part.
* *
* <p>This function is compatible with multi-part tlds. * <p>This function is compatible with multi-part tlds, e.g. {@code co.uk}. This function will
* also work on domains for which the registry is not authoritative. If you are certain that the
* input will be under a TLD this registry controls, then it is preferable to use
* {@link com.google.domain.registry.model.registry.Registries#findTldForName(InternetDomainName)
* Registries#findTldForName}, which will work on hostnames in addition to domains.
* *
* @param fullyQualifiedDomainName must be a punycode SLD (not a host or unicode)
* @throws IllegalArgumentException if there is no TLD * @throws IllegalArgumentException if there is no TLD
*/ */
public static String getTldFromDomainName(String fullyQualifiedDomainName) { public static String getTldFromDomainName(String fullyQualifiedDomainName) {
@ -40,4 +45,6 @@ public class DomainUtils {
checkArgument(domainName.hasParent(), "fullyQualifiedDomainName does not have a TLD"); checkArgument(domainName.hasParent(), "fullyQualifiedDomainName does not have a TLD");
return domainName.parent().toString(); return domainName.parent().toString();
} }
private DomainUtils() {}
} }

View file

@ -86,8 +86,16 @@ public final class Registries {
} }
/** /**
* Returns the TLD which the domain name or hostname falls under, no matter how many levels of * Returns TLD which the domain name or hostname falls under, no matter how many levels of
* subdomains there are. * sublabels there are.
*
* <p><b>Note:</b> This routine will only work on names under TLDs for which this registry is
* authoritative. To extract TLDs from domains (not hosts) that other registries control, use
* {@link com.google.domain.registry.model.domain.DomainUtils#getTldFromDomainName(String)
* DomainUtils#getTldFromDomainName}.
*
* @param domainName domain name or host name (but not TLD) under an authoritative TLD
* @return TLD or absent if {@code domainName} has no labels under an authoritative TLD
*/ */
public static Optional<InternetDomainName> findTldForName(InternetDomainName domainName) { public static Optional<InternetDomainName> findTldForName(InternetDomainName domainName) {
ImmutableSet<String> tlds = getTlds(); ImmutableSet<String> tlds = getTlds();