public static HostAndPort toHostAndPort(NetworkEndpoint networkEndpoint) {
    switch (networkEndpoint.getType()) {
      case IP:
        return HostAndPort.fromHost(networkEndpoint.getIpAddress().getAddress());
      case IP_PORT:
        return HostAndPort.fromParts(
            networkEndpoint.getIpAddress().getAddress(), networkEndpoint.getPort().getPortNumber());
      case HOSTNAME:
      case IP_HOSTNAME:
        return HostAndPort.fromHost(networkEndpoint.getHostname().getName());
      case HOSTNAME_PORT:
      case IP_HOSTNAME_PORT:
        return HostAndPort.fromParts(
            networkEndpoint.getHostname().getName(), networkEndpoint.getPort().getPortNumber());
      case UNRECOGNIZED:
      case TYPE_UNSPECIFIED:
        throw new AssertionError("Type for NetworkEndpoint must be specified.");
    }

    throw new AssertionError(
        String.format(
            "Should never happen. Unchecked NetworkEndpoint type: %s", networkEndpoint.getType()));
  }
@Test
  public void toHostAndPort_withHostname_returnsHostWithHostname() {
    NetworkEndpoint hostnameEndpoint =
        NetworkEndpoint.newBuilder()
            .setType(NetworkEndpoint.Type.HOSTNAME)
            .setHostname(Hostname.newBuilder().setName("localhost"))
            .build();
    assertThat(NetworkEndpointUtils.toHostAndPort(hostnameEndpoint))
        .isEqualTo(HostAndPort.fromHost("localhost"));
  }