|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.google.common.net.InternetDomainName
@Beta @GwtCompatible(emulated=true) public final class InternetDomainName
An immutable well-formed internet domain name, as defined by
RFC 1035.
Examples include com
and 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.
Invalid domain names throw IllegalArgumentException
on construction.
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. As a result, the only 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.
Equality of domain names is case-insensitive
with respect to ASCII characters, so for convenience, the name()
and
parts()
methods return string with all ASCII characters converted to
lowercase.
internationalized domain names such as 网络.cn
are
supported, but with much weaker syntactic validation (resulting in false
positive reports of validity).
Method Summary | |
---|---|
InternetDomainName |
child(String leftParts)
Creates and returns a new InternetDomainName by prepending the
argument and a dot to the current name. |
boolean |
equals(Object object)
Indicates whether some other object is "equal to" this one. |
static InternetDomainName |
fromLenient(String domain)
A factory method for creating InternetDomainName objects. |
int |
hashCode()
Returns a hash code value for the object. |
boolean |
hasParent()
Indicates whether this domain is composed of two or more parts. |
boolean |
hasPublicSuffix()
Indicates whether this domain name ends in a public suffix, including if it is a public suffix itself. |
boolean |
isPublicSuffix()
Indicates whether this domain name represents a public suffix, as defined by the Mozilla Foundation's Public Suffix List (PSL). |
boolean |
isTopPrivateDomain()
Indicates whether this domain name is composed of exactly one subdomain component followed by a public suffix. |
boolean |
isUnderPublicSuffix()
Indicates whether this domain name ends in a public suffix, while not being a public suffix itself. |
static boolean |
isValidLenient(String name)
Indicates whether the argument is a syntactically valid domain name. |
String |
name()
Returns the domain name, normalized to all lower case. |
InternetDomainName |
parent()
Returns an InternetDomainName that is the immediate ancestor of
this one; that is, the current domain with the leftmost part removed. |
ImmutableList<String> |
parts()
Returns the individual components of this domain name, normalized to all lower case. |
InternetDomainName |
publicSuffix()
Returns the public suffix portion of the domain name, or null if no public suffix is present. |
InternetDomainName |
topPrivateDomain()
Returns the portion of this domain name that is one level beneath the public suffix. |
String |
toString()
Returns a string representation of the object. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Method Detail |
---|
public static InternetDomainName fromLenient(String domain)
InternetDomainName
objects. Only
lenient validation of the domain is performed. Specifically,
validation against
RFC 3490
("Internationalizing Domain Names in Applications") is not performed.
domain
- A domain name (not IP address)
IllegalArgumentException
- If name is not syntactically validfrom
)public String name()
public ImmutableList<String> parts()
mail.google.com
, this
method returns the list ["mail", "google", "com"]
.
public boolean isPublicSuffix()
com
, co.uk
or pvt.k12.wy.us
. Examples of domain
names that are not public suffixes include google
, google.com
and foo.co.uk
.
true
if this domain name appears exactly on the public
suffix listpublic boolean hasPublicSuffix()
true
for www.google.com
, foo.co.uk
and
com
, but not for google
or google.foo
. This is
the recommended method for determining whether a domain is potentially an
addressable host.
public InternetDomainName publicSuffix()
null
if no public suffix is present.
public boolean isUnderPublicSuffix()
true
for www.google.com
, foo.co.uk
and
bar.ca.us
, but not for google
, com
, or google.foo
.
Warning: a false
result from this method does not imply
that the domain does not represent an addressable host, as many public
suffixes are also addressable hosts. Use hasPublicSuffix()
for
that test.
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.
public boolean isTopPrivateDomain()
true
for google.com
and foo.co.uk
,
but not for www.google.com
or co.uk
.
Warning: A true
result from this method does not imply
that the domain is at the highest level which is addressable as a host, as
many public suffixes are also addressable hosts. For example, the domain
bar.uk.com
has a public suffix of uk.com
, so it would
return true
from this method. But uk.com
is itself an
addressable host.
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.
public InternetDomainName topPrivateDomain()
x.adwords.google.co.uk
it returns
google.co.uk
, since co.uk
is a public suffix.
If isTopPrivateDomain()
is true, the current domain name
instance is returned.
This method should not be used to determine the topmost parent domain
which is addressable as a host, as many public suffixes are also
addressable hosts. For example, the domain foo.bar.uk.com
has
a public suffix of uk.com
, so it would return bar.uk.com
from this method. But uk.com
is itself an addressable host.
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.
IllegalStateException
- if this domain does not end with a
public suffixpublic boolean hasParent()
public InternetDomainName parent()
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
.
IllegalStateException
- if the domain has no parent, as determined
by hasParent()
public InternetDomainName child(String leftParts)
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
.
NullPointerException
- if leftParts is null
IllegalArgumentException
- if the resulting name is not validpublic static boolean isValidLenient(String name)
fromLenient(String)
.
This method is intended for the case where a String
must be
validated as a valid domain name, but no further work with that
String
as an InternetDomainName
will be required. Code like
the following will unnecessarily repeat the work of validation:
if (InternetDomainName.isValid(name)) {
domainName = InternetDomainName.from(name);
} else {
domainName = DEFAULT_DOMAIN;
}
Such code could instead be written as follows: try {
domainName = InternetDomainName.from(name);
} catch (IllegalArgumentException e) {
domainName = DEFAULT_DOMAIN;
}
isValid
)public String toString()
java.lang.Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
toString
in class Object
public boolean equals(@Nullable Object object)
java.lang.Object
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y)
consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals
method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode
method, which states
that equal objects must have equal hash codes.
equals
in class Object
object
- the reference object with which to compare.
true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap
public int hashCode()
java.lang.Object
HashMap
.
The general contract of hashCode
is:
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.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
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.
As much as is reasonably practical, the hashCode method defined by
class Object
does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |