MCPcopy
hub / github.com/google/guava / fromValid

Method fromValid

guava/src/com/google/common/net/HostSpecifier.java:71–104  ·  view source on GitHub ↗

Returns a {@code HostSpecifier} built from the provided {@code specifier}, which is already known to be valid. If the {@code specifier} might be invalid, use {@link #from(String)} instead. <p>The specifier must be in one of these formats: <ul> <li>A domain name, like {@code google.com} <li>A I

(String specifier)

Source from the content-addressed store, hash-verified

69 * @throws IllegalArgumentException if the specifier is not valid.
70 */
71 public static HostSpecifier fromValid(String specifier) {
72 // Verify that no port was specified, and strip optional brackets from
73 // IPv6 literals.
74 HostAndPort parsedHost = HostAndPort.fromString(specifier);
75 Preconditions.checkArgument(!parsedHost.hasPort());
76 String host = parsedHost.getHost();
77
78 // Try to interpret the specifier as an IP address. Note we build
79 // the address rather than using the .is* methods because we want to
80 // use InetAddresses.toUriString to convert the result to a string in
81 // canonical form.
82 InetAddress addr = null;
83 try {
84 addr = InetAddresses.forString(host);
85 } catch (IllegalArgumentException e) {
86 // It is not an IPv4 or IPv6 literal
87 }
88
89 if (addr != null) {
90 return new HostSpecifier(InetAddresses.toUriString(addr));
91 }
92
93 // It is not any kind of IP address; must be a domain name or invalid.
94
95 // TODO(user): different versions of this for different factories?
96 InternetDomainName domain = InternetDomainName.from(host);
97
98 if (domain.hasPublicSuffix()) {
99 return new HostSpecifier(domain.toString());
100 }
101
102 throw new IllegalArgumentException(
103 "Domain name does not have a recognized public suffix: " + host);
104 }
105
106 /**
107 * Attempts to return a {@code HostSpecifier} for the given string, throwing an exception if

Callers 6

fromMethod · 0.95
isValidMethod · 0.95
specMethod · 0.95
testNullsMethod · 0.95
assertGoodMethod · 0.95
assertBadMethod · 0.95

Calls 9

fromStringMethod · 0.95
checkArgumentMethod · 0.95
hasPortMethod · 0.95
getHostMethod · 0.95
forStringMethod · 0.95
toUriStringMethod · 0.95
fromMethod · 0.95
hasPublicSuffixMethod · 0.95
toStringMethod · 0.95

Tested by 4

specMethod · 0.76
testNullsMethod · 0.76
assertGoodMethod · 0.76
assertBadMethod · 0.76