class URIBuilder {

    public static URI toURI(String urlStr) {
        try {
            final URL url = new URL(urlStr);
            final Integer port = Optional.of(url.getPort()).filter(p -> !p.equals(-1)).orElse(url.getDefaultPort());
            final URI uri = url.toURI();
            return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), port, uri.getPath(), uri.getQuery(),
                    uri.getFragment());

        } catch (MalformedURLException | URISyntaxException e) {
           throw new IllegalArgumentException("Not valid URI: " + urlStr, e);
        }
    }

    private  URIBuilder();

}