code: public static int getPort(URI uri, int defaultPort) {
      Objects.requireNonNull(uri, "uri must not be null");
      return uri.getPort() == -1 ? defaultPort : uri.getPort();
    }
docs: Returns the port of a URI. If the port is unset (i.e. {@code -1}) then returns the {@code
  defaultPort}.
  
  @param uri the URI to extract the port from
  @param defaultPort the default to use if the port is unset
  @return the port of a URI or {@code defaultPort} if unset
  @throws NullPointerException if {@code uri} is {@code null}