@Test public void testAppendParametersToUrl() { assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "123?pageIndex=19", UrlUtil.appendParametersToUrl("123", "pageIndex", "19") ); assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "http: UrlUtil.appendParametersToUrl( "http: ) ); assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "http: UrlUtil.appendParametersToUrl( "http: ) ); assertEquals( "123?pageIndex=19&friends=true", UrlUtil.appendParametersToUrl("123", "pageIndex", "19", "friends", "true") ); assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "http: UrlUtil.appendParametersToUrl("http: ); assertEquals( "http: UrlUtil.appendParametersToUrl( "http: ) ); assertEquals( "http: UrlUtil.appendParametersToUrl( "http: "compact", null, "friends", "true" ) ); }
@SuppressWarnings("OverlyComplexMethod") public static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts) { int partCount = parameterParts.length; if (!isValidUri(url) || partCount == 0) { return url; } if (partCount % 2 != 0) { throw new IllegalArgumentException("Expected even number of parameter parts."); } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); StringBuilder resultUrl; String urlPostfix; if (questionSignPos == -1 && sharpPos == -1) { resultUrl = new StringBuilder(url); urlPostfix = ""; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { resultUrl = new StringBuilder(url.substring(0, sharpPos)); urlPostfix = url.substring(sharpPos); } else { resultUrl = new StringBuilder(url.substring(0, questionSignPos)); urlPostfix = url.length() > questionSignPos + 1 ? '&' + url.substring(questionSignPos + 1) : ""; } boolean firstParameter = true; for (int partIndex = 0; partIndex < partCount; partIndex += 2) { String parameterName = parameterParts[partIndex]; if (StringUtil.isBlank(parameterName)) { continue; } String parameterValue = parameterParts[partIndex + 1]; String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; resultUrl.append(firstParameter ? '?' : '&').append(parameter); firstParameter = false; } return firstParameter ? url : resultUrl.append(urlPostfix).toString(); }
UrlUtil { @SuppressWarnings("OverlyComplexMethod") public static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts) { int partCount = parameterParts.length; if (!isValidUri(url) || partCount == 0) { return url; } if (partCount % 2 != 0) { throw new IllegalArgumentException("Expected even number of parameter parts."); } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); StringBuilder resultUrl; String urlPostfix; if (questionSignPos == -1 && sharpPos == -1) { resultUrl = new StringBuilder(url); urlPostfix = ""; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { resultUrl = new StringBuilder(url.substring(0, sharpPos)); urlPostfix = url.substring(sharpPos); } else { resultUrl = new StringBuilder(url.substring(0, questionSignPos)); urlPostfix = url.length() > questionSignPos + 1 ? '&' + url.substring(questionSignPos + 1) : ""; } boolean firstParameter = true; for (int partIndex = 0; partIndex < partCount; partIndex += 2) { String parameterName = parameterParts[partIndex]; if (StringUtil.isBlank(parameterName)) { continue; } String parameterValue = parameterParts[partIndex + 1]; String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; resultUrl.append(firstParameter ? '?' : '&').append(parameter); firstParameter = false; } return firstParameter ? url : resultUrl.append(urlPostfix).toString(); } }
UrlUtil { @SuppressWarnings("OverlyComplexMethod") public static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts) { int partCount = parameterParts.length; if (!isValidUri(url) || partCount == 0) { return url; } if (partCount % 2 != 0) { throw new IllegalArgumentException("Expected even number of parameter parts."); } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); StringBuilder resultUrl; String urlPostfix; if (questionSignPos == -1 && sharpPos == -1) { resultUrl = new StringBuilder(url); urlPostfix = ""; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { resultUrl = new StringBuilder(url.substring(0, sharpPos)); urlPostfix = url.substring(sharpPos); } else { resultUrl = new StringBuilder(url.substring(0, questionSignPos)); urlPostfix = url.length() > questionSignPos + 1 ? '&' + url.substring(questionSignPos + 1) : ""; } boolean firstParameter = true; for (int partIndex = 0; partIndex < partCount; partIndex += 2) { String parameterName = parameterParts[partIndex]; if (StringUtil.isBlank(parameterName)) { continue; } String parameterValue = parameterParts[partIndex + 1]; String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; resultUrl.append(firstParameter ? '?' : '&').append(parameter); firstParameter = false; } return firstParameter ? url : resultUrl.append(urlPostfix).toString(); } private  UrlUtil(); }
UrlUtil { @SuppressWarnings("OverlyComplexMethod") public static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts) { int partCount = parameterParts.length; if (!isValidUri(url) || partCount == 0) { return url; } if (partCount % 2 != 0) { throw new IllegalArgumentException("Expected even number of parameter parts."); } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); StringBuilder resultUrl; String urlPostfix; if (questionSignPos == -1 && sharpPos == -1) { resultUrl = new StringBuilder(url); urlPostfix = ""; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { resultUrl = new StringBuilder(url.substring(0, sharpPos)); urlPostfix = url.substring(sharpPos); } else { resultUrl = new StringBuilder(url.substring(0, questionSignPos)); urlPostfix = url.length() > questionSignPos + 1 ? '&' + url.substring(questionSignPos + 1) : ""; } boolean firstParameter = true; for (int partIndex = 0; partIndex < partCount; partIndex += 2) { String parameterName = parameterParts[partIndex]; if (StringUtil.isBlank(parameterName)) { continue; } String parameterValue = parameterParts[partIndex + 1]; String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; resultUrl.append(firstParameter ? '?' : '&').append(parameter); firstParameter = false; } return firstParameter ? url : resultUrl.append(urlPostfix).toString(); } private  UrlUtil(); @SuppressWarnings("unused") static Set<String> getParameterNames(@Nonnull String url); @SuppressWarnings("IfStatementWithIdenticalBranches") static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName); static String replaceParameterInUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); static String appendParameterToUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); @SuppressWarnings("OverlyComplexMethod") static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts); @SuppressWarnings("unused") static String appendRelativePathToUrl(@Nonnull String url, @Nullable String relativePath); @SuppressWarnings("BooleanMethodIsAlwaysInverted") @Contract("null -> false") static boolean isValidUrl(@Nullable String url); @Contract("null, _ -> false") static boolean isValidUrl(@Nullable String url, String[] allowedSchemes); @SuppressWarnings({"ResultOfMethodCallIgnored", "BooleanMethodIsAlwaysInverted"}) @Contract("null -> false") static boolean isValidUri(@Nullable String uri); @SuppressWarnings("unused") @Contract("null -> null") @Nullable static String extractFileName(@Nullable String url); }
UrlUtil { @SuppressWarnings("OverlyComplexMethod") public static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts) { int partCount = parameterParts.length; if (!isValidUri(url) || partCount == 0) { return url; } if (partCount % 2 != 0) { throw new IllegalArgumentException("Expected even number of parameter parts."); } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); StringBuilder resultUrl; String urlPostfix; if (questionSignPos == -1 && sharpPos == -1) { resultUrl = new StringBuilder(url); urlPostfix = ""; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { resultUrl = new StringBuilder(url.substring(0, sharpPos)); urlPostfix = url.substring(sharpPos); } else { resultUrl = new StringBuilder(url.substring(0, questionSignPos)); urlPostfix = url.length() > questionSignPos + 1 ? '&' + url.substring(questionSignPos + 1) : ""; } boolean firstParameter = true; for (int partIndex = 0; partIndex < partCount; partIndex += 2) { String parameterName = parameterParts[partIndex]; if (StringUtil.isBlank(parameterName)) { continue; } String parameterValue = parameterParts[partIndex + 1]; String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; resultUrl.append(firstParameter ? '?' : '&').append(parameter); firstParameter = false; } return firstParameter ? url : resultUrl.append(urlPostfix).toString(); } private  UrlUtil(); @SuppressWarnings("unused") static Set<String> getParameterNames(@Nonnull String url); @SuppressWarnings("IfStatementWithIdenticalBranches") static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName); static String replaceParameterInUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); static String appendParameterToUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); @SuppressWarnings("OverlyComplexMethod") static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts); @SuppressWarnings("unused") static String appendRelativePathToUrl(@Nonnull String url, @Nullable String relativePath); @SuppressWarnings("BooleanMethodIsAlwaysInverted") @Contract("null -> false") static boolean isValidUrl(@Nullable String url); @Contract("null, _ -> false") static boolean isValidUrl(@Nullable String url, String[] allowedSchemes); @SuppressWarnings({"ResultOfMethodCallIgnored", "BooleanMethodIsAlwaysInverted"}) @Contract("null -> false") static boolean isValidUri(@Nullable String uri); @SuppressWarnings("unused") @Contract("null -> null") @Nullable static String extractFileName(@Nullable String url);  }