lllichen closed pull request #2221: optimize code for more understandable
URL: https://github.com/apache/incubator-dubbo/pull/2221
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java 
b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
index a83ca91e22..0a0ceff547 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
@@ -169,17 +169,9 @@ 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;
@@ -187,69 +179,117 @@ public static URL valueOf(String url) {
         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");
+        }
+        return new URLParser(url).build();
+    }
+
+
     public static String encode(String value) {
         if (value == null || value.length() == 0) {
             return "";


 

----------------------------------------------------------------
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