|
17 | 17 | import static com.google.common.base.Preconditions.checkArgument;
|
18 | 18 | import static com.google.common.base.Preconditions.checkNotNull;
|
19 | 19 | import static com.google.common.base.Preconditions.checkState;
|
| 20 | +import static com.google.common.base.Strings.isNullOrEmpty; |
20 | 21 |
|
21 | 22 | import com.google.common.annotations.GwtCompatible;
|
22 | 23 | import com.google.common.annotations.GwtIncompatible;
|
23 | 24 | import com.google.common.annotations.J2ktIncompatible;
|
24 |
| -import com.google.common.base.CharMatcher; |
25 | 25 | import com.google.common.base.Objects;
|
26 |
| -import com.google.common.base.Strings; |
| 26 | +import com.google.common.primitives.Ints; |
27 | 27 | import com.google.errorprone.annotations.CanIgnoreReturnValue;
|
28 | 28 | import com.google.errorprone.annotations.Immutable;
|
29 | 29 | import java.io.Serializable;
|
@@ -188,19 +188,12 @@ public static HostAndPort fromString(String hostPortString) {
|
188 | 188 | }
|
189 | 189 | }
|
190 | 190 |
|
191 |
| - int port = NO_PORT; |
192 |
| - if (!Strings.isNullOrEmpty(portString)) { |
193 |
| - // Try to parse the whole port string as a number. |
194 |
| - // Java accepts leading plus signs. We don't want to. |
195 |
| - checkArgument( |
196 |
| - !portString.startsWith("+") && CharMatcher.ascii().matchesAllOf(portString), |
197 |
| - "Unparseable port number: %s", |
198 |
| - hostPortString); |
199 |
| - try { |
200 |
| - port = Integer.parseInt(portString); |
201 |
| - } catch (NumberFormatException e) { |
202 |
| - throw new IllegalArgumentException("Unparseable port number: " + hostPortString); |
203 |
| - } |
| 191 | + Integer port; |
| 192 | + if (isNullOrEmpty(portString)) { |
| 193 | + port = NO_PORT; |
| 194 | + } else { |
| 195 | + port = Ints.tryParse(portString); |
| 196 | + checkArgument(port != null, "Unparseable port number: %s", hostPortString); |
204 | 197 | checkArgument(isValidPort(port), "Port number out of range: %s", hostPortString);
|
205 | 198 | }
|
206 | 199 |
|
|
0 commit comments