carryxyh commented on a change in pull request #2221: optimize code for more 
understandable
URL: https://github.com/apache/incubator-dubbo/pull/2221#discussion_r209271069
 
 

 ##########
 File path: dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
 ##########
 @@ -169,87 +169,127 @@ public URL(String protocol, String username, String 
password, String host, int p
         this.parameters = Collections.unmodifiableMap(parameters);
     }
 
-    /**
-     * Parse url string
-     *
-     * @param url URL string
-     * @return URL instance
-     * @see URL
-     */
-    public static URL valueOf(String url) {
-        if (url == null || (url = url.trim()).length() == 0) {
-            throw new IllegalArgumentException("url == null");
-        }
+
+    private static class URLParser{
+        String url = null;
         String protocol = null;
         String username = null;
         String password = null;
         String host = null;
         int port = 0;
         String path = null;
         Map<String, String> parameters = null;
-        int i = url.indexOf("?"); // seperator between body and parameters 
-        if (i >= 0) {
-            String[] parts = url.substring(i + 1).split("\\&");
-            parameters = new HashMap<String, String>();
-            for (String part : parts) {
-                part = part.trim();
-                if (part.length() > 0) {
-                    int j = part.indexOf('=');
-                    if (j >= 0) {
-                        parameters.put(part.substring(0, j), part.substring(j 
+ 1));
-                    } else {
-                        parameters.put(part, part);
+
+        public URLParser(String url) {
+            this.url = url;
+            this.parse();
+        }
+
+        private void parseParameters(){
+            int i = url.indexOf("?"); // separator between body and parameters
+            if (i >= 0) {
+                String[] parts = url.substring(i + 1).split("\\&");
+                Map<String, String> parameters = new HashMap<String, String>();
+                for (String part : parts) {
+                    part = part.trim();
+                    if (part.length() > 0) {
+                        int j = part.indexOf('=');
+                        if (j >= 0) {
+                            parameters.put(part.substring(0, j), 
part.substring(j + 1));
+                        } else {
+                            parameters.put(part, part);
+                        }
                     }
                 }
+                url = url.substring(0, i);
+                this.parameters = parameters;
             }
-            url = url.substring(0, i);
         }
-        i = url.indexOf("://");
-        if (i >= 0) {
-            if (i == 0) throw new IllegalStateException("url missing protocol: 
\"" + url + "\"");
-            protocol = url.substring(0, i);
-            url = url.substring(i + 3);
-        } else {
-            // case: file:/path/to/file.txt
-            i = url.indexOf(":/");
+
+        private void parseProtocol(){
+            int i = url.indexOf("://");
             if (i >= 0) {
                 if (i == 0) throw new IllegalStateException("url missing 
protocol: \"" + url + "\"");
                 protocol = url.substring(0, i);
-                url = url.substring(i + 1);
+                url = url.substring(i + 3);
+            } else {
+                // case: file:/path/to/file.txt
+                i = url.indexOf(":/");
+                if (i >= 0) {
+                    if (i == 0) throw new IllegalStateException("url missing 
protocol: \"" + url + "\"");
+                    protocol = url.substring(0, i);
+                    url = url.substring(i + 1);
+                }
             }
         }
 
-        i = url.indexOf("/");
-        if (i >= 0) {
-            path = url.substring(i + 1);
-            url = url.substring(0, i);
-        }
-        i = url.lastIndexOf("@");
-        if (i >= 0) {
-            username = url.substring(0, i);
-            int j = username.indexOf(":");
-            if (j >= 0) {
-                password = username.substring(j + 1);
-                username = username.substring(0, j);
-            }
-            url = url.substring(i + 1);
-        }
-        i = url.lastIndexOf(":");
-        if (i >= 0 && i < url.length() - 1) {
-            if (url.lastIndexOf("%") > i) {
-                // ipv6 address with scope id
-                // e.g. fe80:0:0:0:894:aeec:f37d:23e1%en0
-                // see https://howdoesinternetwork.com/2013/ipv6-zone-id
-                // ignore
-            } else {
-                port = Integer.parseInt(url.substring(i+1));
+        private void parseUsernameAndPassword(){
+            int i = url.indexOf("/");
+            if (i >= 0) {
+                path = url.substring(i + 1);
                 url = url.substring(0, i);
             }
+            i = url.lastIndexOf("@");
+            if (i >= 0) {
+                username = url.substring(0, i);
+                int j = username.indexOf(":");
+                if (j >= 0) {
+                    password = username.substring(j + 1);
+                    username = username.substring(0, j);
+                }
+                url = url.substring(i + 1);
+            }
+        }
+
+        private void parsePort(){
+            int i = url.lastIndexOf(":");
+            if (i >= 0 && i < url.length() - 1) {
+                if (url.lastIndexOf("%") > i) {
+                    // ipv6 address with scope id
+                    // e.g. fe80:0:0:0:894:aeec:f37d:23e1%en0
+                    // see https://howdoesinternetwork.com/2013/ipv6-zone-id
+                    // ignore
+                } else {
+                    port = Integer.parseInt(url.substring(i+1));
+                    url = url.substring(0, i);
+                }
+            }
+        }
+
+        private void parseHost(){
+            if (url.length() > 0) host = url;
+        }
+
+        private void parse(){
+            this.parseParameters();
+            this.parseProtocol();
+            this.parseUsernameAndPassword();
+            this.parsePort();
+            this.parseHost();
+        }
+
+        private URL build(){
+            return new URL(protocol, username, password, host, port, path, 
parameters);
         }
-        if (url.length() > 0) host = url;
-        return new URL(protocol, username, password, host, port, path, 
parameters);
     }
 
+
+
+    /**
+     * Parse url string
+     *
+     * @param url URL string
+     * @return URL instance
+     * @see URL
+     */
+    public static URL valueOf(String url) {
+        if (url == null || (url = url.trim()).length() == 0) {
+            throw new IllegalArgumentException("url == null");
 
 Review comment:
   How about make the msg of exception "url is empty!"

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to