package com.google.common.net; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import java.io.Serializable; /* loaded from: classes.dex */ public final class HostAndPort implements Serializable { private static final int NO_PORT = -1; private static final long serialVersionUID = 0; private final boolean hasBracketlessColons; private final String host; private final int port; private HostAndPort(String str, int i, boolean z) { this.host = str; this.port = i; this.hasBracketlessColons = z; } public static HostAndPort fromHost(String str) { HostAndPort fromString = fromString(str); Preconditions.a(!fromString.hasPort(), "Host has a port: %s", str); return fromString; } public static HostAndPort fromParts(String str, int i) { Preconditions.a(isValidPort(i), "Port out of range: %s", i); HostAndPort fromString = fromString(str); Preconditions.a(!fromString.hasPort(), "Host has a port: %s", str); return new HostAndPort(fromString.host, i, fromString.hasBracketlessColons); } public static HostAndPort fromString(String str) { String str2; String str3; Preconditions.a(str); int i = -1; if (str.startsWith("[")) { String[] hostAndPortFromBracketedHost = getHostAndPortFromBracketedHost(str); str3 = hostAndPortFromBracketedHost[0]; str2 = hostAndPortFromBracketedHost[1]; } else { int indexOf = str.indexOf(58); if (indexOf >= 0) { int i2 = indexOf + 1; if (str.indexOf(58, i2) == -1) { str3 = str.substring(0, indexOf); str2 = str.substring(i2); } } r3 = indexOf >= 0; str2 = null; str3 = str; } if (!Strings.a(str2)) { Preconditions.a(!str2.startsWith("+"), "Unparseable port number: %s", str); try { i = Integer.parseInt(str2); Preconditions.a(isValidPort(i), "Port number out of range: %s", str); } catch (NumberFormatException unused) { throw new IllegalArgumentException("Unparseable port number: " + str); } } return new HostAndPort(str3, i, r3); } private static String[] getHostAndPortFromBracketedHost(String str) { Preconditions.a(str.charAt(0) == '[', "Bracketed host-port string must start with a bracket: %s", str); int indexOf = str.indexOf(58); int lastIndexOf = str.lastIndexOf(93); Preconditions.a(indexOf > -1 && lastIndexOf > indexOf, "Invalid bracketed host/port: %s", str); String substring = str.substring(1, lastIndexOf); int i = lastIndexOf + 1; if (i == str.length()) { return new String[]{substring, ""}; } Preconditions.a(str.charAt(i) == ':', "Only a colon may follow a close bracket: %s", str); int i2 = lastIndexOf + 2; for (int i3 = i2; i3 < str.length(); i3++) { Preconditions.a(Character.isDigit(str.charAt(i3)), "Port must be numeric: %s", str); } return new String[]{substring, str.substring(i2)}; } private static boolean isValidPort(int i) { return i >= 0 && i <= 65535; } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof HostAndPort)) { return false; } HostAndPort hostAndPort = (HostAndPort) obj; return Objects.a(this.host, hostAndPort.host) && this.port == hostAndPort.port && this.hasBracketlessColons == hostAndPort.hasBracketlessColons; } public String getHost() { return this.host; } public int getPort() { Preconditions.b(hasPort()); return this.port; } public int getPortOrDefault(int i) { return hasPort() ? this.port : i; } public boolean hasPort() { return this.port >= 0; } public int hashCode() { return Objects.a(this.host, Integer.valueOf(this.port), Boolean.valueOf(this.hasBracketlessColons)); } public HostAndPort requireBracketsForIPv6() { Preconditions.a(!this.hasBracketlessColons, "Possible bracketless IPv6 literal: %s", this.host); return this; } public String toString() { StringBuilder sb = new StringBuilder(this.host.length() + 8); if (this.host.indexOf(58) >= 0) { sb.append('['); sb.append(this.host); sb.append(']'); } else { sb.append(this.host); } if (hasPort()) { sb.append(':'); sb.append(this.port); } return sb.toString(); } public HostAndPort withDefaultPort(int i) { Preconditions.a(isValidPort(i)); return (hasPort() || this.port == i) ? this : new HostAndPort(this.host, i, this.hasBracketlessColons); } }