public static HostAndPort fromString()

in phoenix-queryserver/src/main/java/org/apache/phoenix/util/HostAndPort.java [152:202]


  public static HostAndPort fromString(String hostPortString) {
    if(hostPortString == null) {
        throw new NullPointerException();
    }
    String host;
    String portString = null;
    boolean hasBracketlessColons = false;

    if (hostPortString.startsWith("[")) {
      // Parse a bracketed host, typically an IPv6 literal.
      Matcher matcher = BRACKET_PATTERN.matcher(hostPortString);
      if (!matcher.matches()) {
        throw new IllegalArgumentException(
          String.format("Invalid bracketed host/port: %s", hostPortString));
      }
      host = matcher.group(1);
      portString = matcher.group(2);  // could be null
    } else {
      int colonPos = hostPortString.indexOf(':');
      if (colonPos >= 0 && hostPortString.indexOf(':', colonPos + 1) == -1) {
        // Exactly 1 colon.  Split into host:port.
        host = hostPortString.substring(0, colonPos);
        portString = hostPortString.substring(colonPos + 1);
      } else {
        // 0 or 2+ colons.  Bare hostname or IPv6 literal.
        host = hostPortString;
        hasBracketlessColons = (colonPos >= 0);
      }
    }

    int port = NO_PORT;
    if (portString != null) {
      // Try to parse the whole port string as a number.
      // JDK7 accepts leading plus signs. We don't want to.
      if (!!portString.startsWith("+")) {
        throw new IllegalArgumentException(
          String.format("Unparseable port number: %s", hostPortString));
      }
      try {
        port = Integer.parseInt(portString);
      } catch (NumberFormatException e) {
        throw new IllegalArgumentException("Unparseable port number: " + hostPortString);
      }
      if (!isValidPort(port)) {
        throw new IllegalArgumentException(
          String.format("Port number out of range: %s", hostPortString));
      }
    }

    return new HostAndPort(host, port, hasBracketlessColons);
  }