You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/05/24 15:09:06 UTC

[camel] 01/10: (chores) camel-util: return an static empty String if there is no query option

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a1f78f12afeb5badeb27b4f7b5947a5b53238e4e
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Tue May 23 15:04:19 2023 +0200

    (chores) camel-util: return an static empty String if there is no query option
---
 .../java/org/apache/camel/util/URISupport.java     | 64 +++++++++++-----------
 1 file changed, 33 insertions(+), 31 deletions(-)

diff --git a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
index e3d281b4449..4285609af52 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
@@ -58,6 +58,8 @@ public final class URISupport {
 
     private static final String CHARSET = "UTF-8";
 
+    private static final String EMPTY_QUERY_STRING = "";
+
     private URISupport() {
         // Helper class
     }
@@ -450,43 +452,43 @@ public final class URISupport {
 
     public static String createQueryString(Collection<String> sortedKeys, Map<String, Object> options, boolean encode)
             throws URISyntaxException {
+        if (options.isEmpty()) {
+            return EMPTY_QUERY_STRING;
+        }
+
         try {
-            if (options.size() > 0) {
-                StringBuilder rc = new StringBuilder();
-                boolean first = true;
-                for (Object o : sortedKeys) {
-                    if (first) {
-                        first = false;
-                    } else {
-                        rc.append("&");
-                    }
+            StringBuilder rc = new StringBuilder();
+            boolean first = true;
+            for (Object o : sortedKeys) {
+                if (first) {
+                    first = false;
+                } else {
+                    rc.append("&");
+                }
 
-                    String key = (String) o;
-                    Object value = options.get(key);
-
-                    // the value may be a list since the same key has multiple
-                    // values
-                    if (value instanceof List) {
-                        List<String> list = (List<String>) value;
-                        for (Iterator<String> it = list.iterator(); it.hasNext();) {
-                            String s = it.next();
-                            appendQueryStringParameter(key, s, rc, encode);
-                            // append & separator if there is more in the list
-                            // to append
-                            if (it.hasNext()) {
-                                rc.append("&");
-                            }
-                        }
-                    } else {
-                        // use the value as a String
-                        String s = value != null ? value.toString() : null;
+                String key = (String) o;
+                Object value = options.get(key);
+
+                // the value may be a list since the same key has multiple
+                // values
+                if (value instanceof List) {
+                    List<String> list = (List<String>) value;
+                    for (Iterator<String> it = list.iterator(); it.hasNext();) {
+                        String s = it.next();
                         appendQueryStringParameter(key, s, rc, encode);
+                        // append & separator if there is more in the list
+                        // to append
+                        if (it.hasNext()) {
+                            rc.append("&");
+                        }
                     }
+                } else {
+                    // use the value as a String
+                    String s = value != null ? value.toString() : null;
+                    appendQueryStringParameter(key, s, rc, encode);
                 }
-                return rc.toString();
-            } else {
-                return "";
             }
+            return rc.toString();
         } catch (UnsupportedEncodingException e) {
             URISyntaxException se = new URISyntaxException(e.toString(), "Invalid encoding");
             se.initCause(e);