Top Description Fields Constructors Methods
com.google.common.net

public final Class InternetDomainName

extends Object
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@Immutable
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkState

An immutable well-formed internet domain name, such as com or foo.co.uk. Only syntactic analysis is performed; no DNS lookups or other network interactions take place. Thus there is no guarantee that the domain actually exists on the internet.

One common use of this class is to determine whether a given string is likely to represent an addressable domain on the web -- that is, for a candidate string "xxx", might browsing to "http://xxx/" result in a webpage being displayed? In the past, this test was frequently done by determining whether the domain ended with a public suffix but was not itself a public suffix. However, this test is no longer accurate. There are many domains which are both public suffixes and addressable as hosts; "uk.com" is one example. Using the subset of public suffixes that are registry suffixes, one can get a better result, as only a few registry suffixes are addressable. However, the most useful test to determine if a domain is a plausible web host is hasPublicSuffix(). This will return true for many domains which (currently) are not hosts, such as "com", but given that any public suffix may become a host without warning, it is better to err on the side of permissiveness and thus avoid spurious rejection of valid sites. Of course, to actually determine addressability of any host, clients of this class will need to perform their own DNS lookups.

During construction, names are normalized in two ways:

  1. ASCII uppercase characters are converted to lowercase.
  2. Unicode dot separators other than the ASCII period ('.') are converted to the ASCII period.

The normalized values will be returned from toString() and parts(), and will be reflected in the result of equals(Object).

Internationalized domain names such as 网络.cn are supported, as are the equivalent IDNA Punycode-encoded versions.

Author
Catherine Berry
Since
5.0

Field Summary

Modifier and TypeField and Description
private static final CharMatcher
private static final CharMatcher
private static final Joiner
private static final Splitter
private static final CharMatcher
private static final CharMatcher
private static final int
MAX_DOMAIN_PART_LENGTH

Maximum size of a single part of a domain name.

private static final int
MAX_LENGTH

Maximum length of a full domain name, including separators, and leaving room for the root label.

private static final int
MAX_PARTS

Maximum parts (labels) in a domain name.

private final String
name

The full domain name, converted to lower case.

private static final int
NO_SUFFIX_FOUND

Value of publicSuffixIndex() or registrySuffixIndex() which indicates that no relevant suffix was found.

private static final CharMatcher
private final ImmutableList<String>
parts

The parts of the domain name, converted to lower case.

private int
publicSuffixIndexCache

Cached value of #publicSuffixIndex().

private int
registrySuffixIndexCache

Cached value of #registrySuffixIndex().

private static final int
SUFFIX_NOT_INITIALIZED

Value of publicSuffixIndexCache or registrySuffixIndexCache which indicates that they were not initialized yet.

Constructor Summary

AccessConstructor and Description
pack-priv
InternetDomainName(String name)

Constructor used to implement from(String), and from subclasses.

private
InternetDomainName(String name, ImmutableList<String> parts)

Internal constructor that skips validations when creating an instance from parts of an already-validated InternetDomainName, as in ancestor.

Method Summary

Modifier and TypeMethod and Description
private InternetDomainName
ancestor(int levels)

Returns the ancestor of the current domain at the given number of levels "higher" (rightward) in the subdomain list.

public InternetDomainName
child(String leftParts)

Creates and returns a new InternetDomainName by prepending the argument and a dot to the current name.

public boolean
equals(Object
the reference object with which to compare.
object
)

Overrides java.lang.Object.equals.

Equality testing is based on the text supplied by the caller, after normalization as described in the class documentation.
private int
findSuffixOfType(Optional<PublicSuffixType> desiredType)

Returns the index of the leftmost part of the suffix, or -1 if not found.

public static InternetDomainName
from(String
A domain name (not IP address)
domain
)

Returns an instance of InternetDomainName after lenient validation.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public boolean
hasParent()

Indicates whether this domain is composed of two or more parts.

public boolean
hasPublicSuffix()

Indicates whether this domain name ends in a public suffix, including if it is a public suffix itself.

public boolean
hasRegistrySuffix()

Indicates whether this domain name ends in a registry suffix, including if it is a registry suffix itself.

public boolean

Returns:

true if this domain name appears exactly on the public suffix list
isPublicSuffix
()

Indicates whether this domain name represents a public suffix, as defined by the Mozilla Foundation's Public Suffix List (PSL).

public boolean

Returns:

true if this domain name appears exactly on the public suffix list as part of the registry suffix section (labelled "ICANN").
isRegistrySuffix
()

Indicates whether this domain name represents a registry suffix, as defined by a subset of the Mozilla Foundation's Public Suffix List (PSL).

public boolean
isTopDomainUnderRegistrySuffix()

Indicates whether this domain name is composed of exactly one subdomain component followed by a registry suffix.

public boolean
isTopPrivateDomain()

Indicates whether this domain name is composed of exactly one subdomain component followed by a public suffix.

public boolean
isUnderPublicSuffix()

Indicates whether this domain name ends in a public suffix, while not being a public suffix itself.

public boolean
isUnderRegistrySuffix()

Indicates whether this domain name ends in a registry suffix, while not being a registry suffix itself.

public static boolean
isValid(String name)

Indicates whether the argument is a syntactically valid domain name using lenient validation.

private static boolean
matchesType(Optional<PublicSuffixType> desiredType, Optional<PublicSuffixType> actualType)

If a desiredType is specified, returns true only if the actualType is identical.

public InternetDomainName
parent()

Returns an InternetDomainName that is the immediate ancestor of this one; that is, the current domain with the leftmost part removed.

public ImmutableList<String>
parts()

Returns the individual components of this domain name, normalized to all lower case.

public InternetDomainName
publicSuffix()

Returns the public suffix portion of the domain name, or null if no public suffix is present.

private int
publicSuffixIndex()

The index in the parts() list at which the public suffix begins.

public InternetDomainName
registrySuffix()

Returns the registry suffix portion of the domain name, or null if no registry suffix is present.

private int
registrySuffixIndex()

The index in the parts() list at which the registry suffix begins.

public InternetDomainName
topDomainUnderRegistrySuffix()

Returns the portion of this domain name that is one level beneath the registry suffix.

public InternetDomainName
topPrivateDomain()

Returns the portion of this domain name that is one level beneath the public suffix.

public String
toString()

Overrides java.lang.Object.toString.

Returns the domain name, normalized to all lower case.
private static boolean

Returns:

Whether the part is valid
validatePart
(String
The domain name part to be validated
part
,
boolean
Is this the final (rightmost) domain part?
isFinalPart
)

Helper method for validateSyntax(List).

private static boolean

Returns:

Is the domain name syntactically valid?
validateSyntax
(List<String> parts)

Validation method used by from to ensure that the domain name is syntactically valid according to RFC 1035.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

DASH_MATCHERback to summary
private static final CharMatcher DASH_MATCHER
DIGIT_MATCHERback to summary
private static final CharMatcher DIGIT_MATCHER
DOT_JOINERback to summary
private static final Joiner DOT_JOINER
DOT_SPLITTERback to summary
private static final Splitter DOT_SPLITTER
DOTS_MATCHERback to summary
private static final CharMatcher DOTS_MATCHER
LETTER_MATCHERback to summary
private static final CharMatcher LETTER_MATCHER
MAX_DOMAIN_PART_LENGTHback to summary
private static final int MAX_DOMAIN_PART_LENGTH

Maximum size of a single part of a domain name. See RFC 2181 part 11.

MAX_LENGTHback to summary
private static final int MAX_LENGTH

Maximum length of a full domain name, including separators, and leaving room for the root label. See RFC 2181 part 11.

MAX_PARTSback to summary
private static final int MAX_PARTS

Maximum parts (labels) in a domain name. This value arises from the 255-octet limit described in RFC 2181 part 11 with the fact that the encoding of each part occupies at least two bytes (dot plus label externally, length byte plus label internally). Thus, if all labels have the minimum size of one byte, 127 of them will fit.

nameback to summary
private final String name

The full domain name, converted to lower case.

NO_SUFFIX_FOUNDback to summary
private static final int NO_SUFFIX_FOUND

Value of publicSuffixIndex() or registrySuffixIndex() which indicates that no relevant suffix was found.

PART_CHAR_MATCHERback to summary
private static final CharMatcher PART_CHAR_MATCHER
partsback to summary
private final ImmutableList<String> parts

The parts of the domain name, converted to lower case.

publicSuffixIndexCacheback to summary
private int publicSuffixIndexCache

Cached value of #publicSuffixIndex(). Do not use directly.

Since this field isn't volatile, if an instance of this class is shared across threads before it is initialized, then each thread is likely to compute their own copy of the value.

Annotations
@SuppressWarnings:Immutable
@LazyInit
registrySuffixIndexCacheback to summary
private int registrySuffixIndexCache

Cached value of #registrySuffixIndex(). Do not use directly.

Since this field isn't volatile, if an instance of this class is shared across threads before it is initialized, then each thread is likely to compute their own copy of the value.

Annotations
@SuppressWarnings:Immutable
@LazyInit
SUFFIX_NOT_INITIALIZEDback to summary
private static final int SUFFIX_NOT_INITIALIZED

Value of publicSuffixIndexCache or registrySuffixIndexCache which indicates that they were not initialized yet.

Constructor Detail

InternetDomainNameback to summary
pack-priv InternetDomainName(String name)

Constructor used to implement from(String), and from subclasses.

InternetDomainNameback to summary
private InternetDomainName(String name, ImmutableList<String> parts)

Internal constructor that skips validations when creating an instance from parts of an already-validated InternetDomainName, as in ancestor.

Method Detail

ancestorback to summary
private InternetDomainName ancestor(int levels)

Returns the ancestor of the current domain at the given number of levels "higher" (rightward) in the subdomain list. The number of levels must be non-negative, and less than N-1, where N is the number of parts in the domain.

Todo

Reasonable candidate for addition to public API.

childback to summary
public InternetDomainName child(String leftParts)

Creates and returns a new InternetDomainName by prepending the argument and a dot to the current name. For example, InternetDomainName.from("foo.com").child("www.bar") returns a new InternetDomainName with the value www.bar.foo.com. Only lenient validation is performed, as described here.

Exceptions
NullPointerException:
if leftParts is null
IllegalArgumentException:
if the resulting name is not valid
equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Equality testing is based on the text supplied by the caller, after normalization as described in the class documentation. For example, a non-ASCII Unicode domain name and the Punycode version of the same domain name would not be considered equal.

Parameters
object:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
findSuffixOfTypeback to summary
private int findSuffixOfType(Optional<PublicSuffixType> desiredType)

Returns the index of the leftmost part of the suffix, or -1 if not found. Note that the value defined as a suffix may not produce true results from isPublicSuffix() or isRegistrySuffix() if the domain ends with an excluded domain pattern such as "nhs.uk".

If a desiredType is specified, this method only finds suffixes of the given type. Otherwise, it finds the first suffix of any type.

fromback to summary
public static InternetDomainName from(String domain)

Returns an instance of InternetDomainName after lenient validation. Specifically, validation against RFC 3490 ("Internationalizing Domain Names in Applications") is skipped, while validation against RFC 1035 is relaxed in the following ways:

  • Any part containing non-ASCII characters is considered valid.
  • Underscores ('_') are permitted wherever dashes ('-') are permitted.
  • Parts other than the final part may start with a digit, as mandated by RFC 1123.
Parameters
domain:String

A domain name (not IP address)

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalArgumentException:
if domain is not syntactically valid according to isValid
Since
10.0 (previously named fromLenient)
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
hasParentback to summary
public boolean hasParent()

Indicates whether this domain is composed of two or more parts.

hasPublicSuffixback to summary
public boolean hasPublicSuffix()

Indicates whether this domain name ends in a public suffix, including if it is a public suffix itself. For example, returns true for www.google.com, foo.co.uk and com, but not for invalid or google.invalid. This is the recommended method for determining whether a domain is potentially an addressable host.

Note that this method is equivalent to hasRegistrySuffix() because all registry suffixes are public suffixes and all public suffixes have registry suffixes.

Since
6.0
hasRegistrySuffixback to summary
public boolean hasRegistrySuffix()

Indicates whether this domain name ends in a registry suffix, including if it is a registry suffix itself. For example, returns true for www.google.com, foo.co.uk and com, but not for invalid or google.invalid.

Note that this method is equivalent to hasPublicSuffix() because all registry suffixes are public suffixes and all public suffixes have registry suffixes.

Since
23.3
isPublicSuffixback to summary
public boolean isPublicSuffix()

Indicates whether this domain name represents a public suffix, as defined by the Mozilla Foundation's Public Suffix List (PSL). A public suffix is one under which Internet users can directly register names, such as com, co.uk or pvt.k12.wy.us. Examples of domain names that are not public suffixes include google.com, foo.co.uk, and myblog.blogspot.com.

Public suffixes are a proper superset of registry suffixes. The list of public suffixes additionally contains privately owned domain names under which Internet users can register subdomains. An example of a public suffix that is not a registry suffix is blogspot.com. Note that it is true that all public suffixes have registry suffixes, since domain name registries collectively control all internet domain names.

For considerations on whether the public suffix or registry suffix designation is more suitable for your application, see this article.

Returns:boolean

true if this domain name appears exactly on the public suffix list

Since
6.0
isRegistrySuffixback to summary
public boolean isRegistrySuffix()

Indicates whether this domain name represents a registry suffix, as defined by a subset of the Mozilla Foundation's Public Suffix List (PSL). A registry suffix is one under which Internet users can directly register names via a domain name registrar, and have such registrations lawfully protected by internet-governing bodies such as ICANN. Examples of registry suffixes include com, co.uk, and pvt.k12.wy.us. Examples of domain names that are not registry suffixes include google.com and foo.co.uk.

Registry suffixes are a proper subset of public suffixes. The list of public suffixes additionally contains privately owned domain names under which Internet users can register subdomains. An example of a public suffix that is not a registry suffix is blogspot.com. Note that it is true that all public suffixes have registry suffixes, since domain name registries collectively control all internet domain names.

For considerations on whether the public suffix or registry suffix designation is more suitable for your application, see this article.

Returns:boolean

true if this domain name appears exactly on the public suffix list as part of the registry suffix section (labelled "ICANN").

Since
23.3
isTopDomainUnderRegistrySuffixback to summary
public boolean isTopDomainUnderRegistrySuffix()

Indicates whether this domain name is composed of exactly one subdomain component followed by a registry suffix. For example, returns true for google.com, foo.co.uk, and blogspot.com, but not for www.google.com, co.uk, or myblog.blogspot.com.

Warning

This method should not be used to determine the probable highest level parent domain for which cookies may be set. Use topPrivateDomain() for that purpose.

Since
23.3
isTopPrivateDomainback to summary
public boolean isTopPrivateDomain()

Indicates whether this domain name is composed of exactly one subdomain component followed by a public suffix. For example, returns true for google.com foo.co.uk, and myblog.blogspot.com, but not for www.google.com, co.uk, or blogspot.com.

This method can be used to determine whether a domain is probably the highest level for which cookies may be set, though even that depends on individual browsers' implementations of cookie controls. See RFC 2109 for details.

Since
6.0
isUnderPublicSuffixback to summary
public boolean isUnderPublicSuffix()

Indicates whether this domain name ends in a public suffix, while not being a public suffix itself. For example, returns true for www.google.com, foo.co.uk and myblog.blogspot.com, but not for com, co.uk, google.invalid, or blogspot.com.

This method can be used to determine whether it will probably be possible to set cookies on the domain, though even that depends on individual browsers' implementations of cookie controls. See RFC 2109 for details.

Since
6.0
isUnderRegistrySuffixback to summary
public boolean isUnderRegistrySuffix()

Indicates whether this domain name ends in a registry suffix, while not being a registry suffix itself. For example, returns true for www.google.com, foo.co.uk and blogspot.com, but not for com, co.uk, or google.invalid.

Since
23.3
isValidback to summary
public static boolean isValid(String name)

Indicates whether the argument is a syntactically valid domain name using lenient validation. Specifically, validation against RFC 3490 ("Internationalizing Domain Names in Applications") is skipped.

The following two code snippets are equivalent:

domainName = InternetDomainName.isValid(name)
    ? InternetDomainName.from(name)
    : DEFAULT_DOMAIN;
try {
  domainName = InternetDomainName.from(name);
} catch (IllegalArgumentException e) {
  domainName = DEFAULT_DOMAIN;
}
Since
8.0 (previously named isValidLenient)
matchesTypeback to summary
private static boolean matchesType(Optional<PublicSuffixType> desiredType, Optional<PublicSuffixType> actualType)

If a desiredType is specified, returns true only if the actualType is identical. Otherwise, returns true as long as actualType is present.

parentback to summary
public InternetDomainName parent()

Returns an InternetDomainName that is the immediate ancestor of this one; that is, the current domain with the leftmost part removed. For example, the parent of www.google.com is google.com.

Exceptions
IllegalStateException:
if the domain has no parent, as determined by hasParent
partsback to summary
public ImmutableList<String> parts()

Returns the individual components of this domain name, normalized to all lower case. For example, for the domain name mail.google.com, this method returns the list ["mail", "google", "com"].

publicSuffixback to summary
public InternetDomainName publicSuffix()

Returns the public suffix portion of the domain name, or null if no public suffix is present.

Annotations
@CheckForNull
Since
6.0
publicSuffixIndexback to summary
private int publicSuffixIndex()

The index in the parts() list at which the public suffix begins. For example, for the domain name myblog.blogspot.co.uk, the value would be 1 (the index of the blogspot part). The value is negative (specifically, NO_SUFFIX_FOUND) if no public suffix was found.

registrySuffixback to summary
public InternetDomainName registrySuffix()

Returns the registry suffix portion of the domain name, or null if no registry suffix is present.

Annotations
@CheckForNull
Since
23.3
registrySuffixIndexback to summary
private int registrySuffixIndex()

The index in the parts() list at which the registry suffix begins. For example, for the domain name myblog.blogspot.co.uk, the value would be 2 (the index of the co part). The value is negative (specifically, NO_SUFFIX_FOUND) if no registry suffix was found.

topDomainUnderRegistrySuffixback to summary
public InternetDomainName topDomainUnderRegistrySuffix()

Returns the portion of this domain name that is one level beneath the registry suffix. For example, for x.adwords.google.co.uk it returns google.co.uk, since co.uk is a registry suffix. Similarly, for myblog.blogspot.com it returns blogspot.com, since com is a registry suffix.

If isTopDomainUnderRegistrySuffix() is true, the current domain name instance is returned.

Warning

This method should not be used to determine whether a domain is probably the highest level for which cookies may be set. Use isTopPrivateDomain() for that purpose.

Exceptions
IllegalStateException:
if this domain does not end with a registry suffix
Since
23.3
topPrivateDomainback to summary
public InternetDomainName topPrivateDomain()

Returns the portion of this domain name that is one level beneath the public suffix. For example, for x.adwords.google.co.uk it returns google.co.uk, since co.uk is a public suffix. Similarly, for myblog.blogspot.com it returns the same domain, myblog.blogspot.com, since blogspot.com is a public suffix.

If isTopPrivateDomain() is true, the current domain name instance is returned.

This method can be used to determine the probable highest level parent domain for which cookies may be set, though even that depends on individual browsers' implementations of cookie controls.

Exceptions
IllegalStateException:
if this domain does not end with a public suffix
Since
6.0
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the domain name, normalized to all lower case.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override
validatePartback to summary
private static boolean validatePart(String part, boolean isFinalPart)

Helper method for validateSyntax(List). Validates that one part of a domain name is valid.

Parameters
part:String

The domain name part to be validated

isFinalPart:boolean

Is this the final (rightmost) domain part?

Returns:boolean

Whether the part is valid

validateSyntaxback to summary
private static boolean validateSyntax(List<String> parts)

Validation method used by from to ensure that the domain name is syntactically valid according to RFC 1035.

Returns:boolean

Is the domain name syntactically valid?