MCPcopy Create free account
hub / github.com/antlr/codebuff / fromValid

Method fromValid

corpus/java/training/guava/net/HostSpecifier.java:71–104  ·  view source on GitHub ↗

Returns a HostSpecifier built from the provided specifier, which is already known to be valid. If the specifier might be invalid, use #from(String) instead. The specifier must be in one of these formats: A domain name, like google.com A IPv4 a

(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 final HostAndPort parsedHost = HostAndPort.fromString(specifier);
75 Preconditions.checkArgument(!parsedHost.hasPort());
76 final String host = parsedHost.getHostText();
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 final 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 2

fromMethod · 0.95
isValidMethod · 0.95

Calls 9

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

Tested by

no test coverage detected