URIAuthority implements NamedEndpoint, Serializable { static URIAuthority parse(final CharSequence s, final Tokenizer.Cursor cursor) throws URISyntaxException { final Tokenizer tokenizer = Tokenizer.INSTANCE; String userInfo = null; final int initPos = cursor.getPos(); final String token = tokenizer.parseContent(s, cursor, HOST_SEPARATORS); if (!cursor.atEnd() && s.charAt(cursor.getPos()) == '@') { cursor.updatePos(cursor.getPos() + 1); if (!TextUtils.isBlank(token)) { userInfo = token; } } else { cursor.updatePos(initPos); } final String hostName = tokenizer.parseContent(s, cursor, PORT_SEPARATORS); String portText = null; if (!cursor.atEnd() && s.charAt(cursor.getPos()) == ':') { cursor.updatePos(cursor.getPos() + 1); portText = tokenizer.parseContent(s, cursor, TERMINATORS); } final int port; if (!TextUtils.isBlank(portText)) { try { port = Integer.parseInt(portText); } catch (final NumberFormatException ex) { throw createException(s, cursor, "Authority port is invalid"); } } else { port = -1; } return new URIAuthority(userInfo, hostName.toLowerCase(Locale.ROOT), port, true); } private  URIAuthority(final String userInfo, final String hostname, final int port, final boolean internal);  URIAuthority(final String userInfo, final String hostname, final int port);  URIAuthority(final String hostname, final int port);  URIAuthority(final NamedEndpoint namedEndpoint);  URIAuthority(final String hostname); static URIAuthority create(final String s); String getUserInfo(); @Override String getHostName(); @Override int getPort(); @Override String toString(); @Override boolean equals(final Object obj); @Override int hashCode();  }
@Test public void testParse() throws Exception { MatcherAssert.assertThat(URIAuthority.parse("somehost"), CoreMatchers.equalTo(new URIAuthority("somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("somehost/blah"), CoreMatchers.equalTo(new URIAuthority("somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("somehost?blah"), CoreMatchers.equalTo(new URIAuthority("somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("somehost#blah"), CoreMatchers.equalTo(new URIAuthority("somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("aaaa@:8080"), CoreMatchers.equalTo(new URIAuthority("aaaa", "", 8080))); MatcherAssert.assertThat(URIAuthority.parse("@:"), CoreMatchers.equalTo(new URIAuthority(null, "", -1))); MatcherAssert.assertThat(URIAuthority.parse("somehost:8080"), CoreMatchers.equalTo(new URIAuthority("somehost", 8080))); MatcherAssert.assertThat(URIAuthority.parse("somehost:8080/blah"), CoreMatchers.equalTo(new URIAuthority("somehost", 8080))); MatcherAssert.assertThat(URIAuthority.parse("somehost:8080?blah"), CoreMatchers.equalTo(new URIAuthority("somehost", 8080))); MatcherAssert.assertThat(URIAuthority.parse("somehost:8080#blah"), CoreMatchers.equalTo(new URIAuthority("somehost", 8080))); MatcherAssert.assertThat(URIAuthority.parse("somehost:008080"), CoreMatchers.equalTo(new URIAuthority("somehost", 8080))); try { URIAuthority.create("somehost:aaaaa"); Assert.fail("URISyntaxException expected"); } catch (final URISyntaxException expected) { } try { URIAuthority.create("somehost:90ab"); Assert.fail("URISyntaxException expected"); } catch (final URISyntaxException expected) { } MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost/blah"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost?blah"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost#blah"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:/blah"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:?blah"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:#blah"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", -1))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:8080"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", 8080))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:8080/blah"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", 8080))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:8080?blah"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", 8080))); MatcherAssert.assertThat(URIAuthority.parse("someuser@somehost:8080#blah"), CoreMatchers.equalTo(new URIAuthority("someuser", "somehost", 8080))); MatcherAssert.assertThat(URIAuthority.parse("@somehost:8080"), CoreMatchers.equalTo(new URIAuthority("somehost", 8080))); MatcherAssert.assertThat(URIAuthority.parse("test:test@localhost:38339"), CoreMatchers.equalTo(new URIAuthority("test:test", "localhost", 38339))); try { URIAuthority.create("blah@goggle.com:80@google.com/"); Assert.fail("URISyntaxException expected"); } catch (final URISyntaxException expected) { } }