From 351ccf3755f09529b6ccffe4a188d99a43a0d4e1 Mon Sep 17 00:00:00 2001 From: Laszlo Hornyak Date: Tue, 11 Feb 2014 18:56:37 +0100 Subject: [PATCH] Cleanup in UriUtils.getUpdateUri - String instantiation replaced with StringBuilder and empty string constant Signed-off-by: Laszlo Hornyak --- utils/src/com/cloud/utils/UriUtils.java | 12 +++++++----- utils/test/com/cloud/utils/UriUtilsTest.java | 4 ++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/utils/src/com/cloud/utils/UriUtils.java b/utils/src/com/cloud/utils/UriUtils.java index fab8ffc7002..bee660d7f27 100644 --- a/utils/src/com/cloud/utils/UriUtils.java +++ b/utils/src/com/cloud/utils/UriUtils.java @@ -143,7 +143,7 @@ public class UriUtils { URIBuilder builder = new URIBuilder(url); builder.removeQuery(); - String updatedQuery = new String(); + StringBuilder updatedQuery = new StringBuilder(); List queryParams = getUserDetails(query); ListIterator iterator = queryParams.listIterator(); while (iterator.hasNext()) { @@ -156,14 +156,16 @@ public class UriUtils { value = param.getValue(); } - if (updatedQuery.isEmpty()) { - updatedQuery += (param.getName() + "=" + value); + if (updatedQuery.length() == 0) { + updatedQuery.append(param.getName()).append('=') + .append(value); } else { - updatedQuery += ("&" + param.getName() + "=" + value); + updatedQuery.append('&').append(param.getName()) + .append('=').append(value); } } - String schemeAndHost = new String(); + String schemeAndHost = ""; URI newUri = builder.build(); if (newUri.getScheme() != null) { schemeAndHost = newUri.getScheme() + "://" + newUri.getHost(); diff --git a/utils/test/com/cloud/utils/UriUtilsTest.java b/utils/test/com/cloud/utils/UriUtilsTest.java index 04fa3e5ff15..e15456ac914 100644 --- a/utils/test/com/cloud/utils/UriUtilsTest.java +++ b/utils/test/com/cloud/utils/UriUtilsTest.java @@ -47,6 +47,10 @@ public class UriUtilsTest { "http://localhost/foo/bar?password=1234", true).startsWith( "http://localhost/foo/bar")); + //just to see if it is still ok with multiple parameters + Assert.assertEquals("http://localhost/foo/bar?param1=true¶m2=12345", UriUtils + .getUpdateUri("http://localhost/foo/bar?param1=true¶m2=12345", false)); + //XXX: Interesting cases not covered: // * port is ignored and left out from the return value }