You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by ca...@apache.org on 2018/11/16 02:20:45 UTC

[incubator-dubbo] branch master updated: improvement on Parameters and CollectionUtils (#2790)

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

carryxyh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 9466425  improvement on Parameters and CollectionUtils (#2790)
9466425 is described below

commit 9466425f51d182a003c609c4f12bf64ff8f95a1a
Author: Song Kun <so...@gmail.com>
AuthorDate: Fri Nov 16 10:20:33 2018 +0800

    improvement on Parameters and CollectionUtils (#2790)
    
    Use CollectionUtils.toStringMap to reduce duplicate code in Parameters, and use Java 8 type inference for collections.
---
 .../src/main/java/org/apache/dubbo/common/Parameters.java  | 14 +++-----------
 .../org/apache/dubbo/common/utils/CollectionUtils.java     | 12 ++++++------
 2 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java b/dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java
index 3999927..529d3fe 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java
@@ -19,6 +19,7 @@ package org.apache.dubbo.common;
 import org.apache.dubbo.common.extension.ExtensionLoader;
 import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.common.utils.StringUtils;
 
 import java.io.UnsupportedEncodingException;
@@ -42,20 +43,11 @@ public class Parameters {
     }
 
     public Parameters(Map<String, String> parameters) {
-        this.parameters = Collections.unmodifiableMap(parameters != null ? new HashMap<String, String>(parameters) : new HashMap<String, String>(0));
+        this.parameters = Collections.unmodifiableMap(parameters != null ? new HashMap<>(parameters) : new HashMap<>(0));
     }
 
     private static Map<String, String> toMap(String... pairs) {
-        Map<String, String> parameters = new HashMap<String, String>();
-        if (pairs.length > 0) {
-            if (pairs.length % 2 != 0) {
-                throw new IllegalArgumentException("pairs must be even.");
-            }
-            for (int i = 0; i < pairs.length; i = i + 2) {
-                parameters.put(pairs[i], pairs[i + 1]);
-            }
-        }
-        return parameters;
+        return CollectionUtils.toStringMap(pairs);
     }
 
     public static Parameters parseParameters(String query) {
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java
index cd5414b..3727108 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java
@@ -72,7 +72,7 @@ public class CollectionUtils {
         if (list == null) {
             return null;
         }
-        Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
+        Map<String, Map<String, String>> result = new HashMap<>();
         for (Map.Entry<String, List<String>> entry : list.entrySet()) {
             result.put(entry.getKey(), split(entry.getValue(), separator));
         }
@@ -83,7 +83,7 @@ public class CollectionUtils {
         if (map == null) {
             return null;
         }
-        Map<String, List<String>> result = new HashMap<String, List<String>>();
+        Map<String, List<String>> result = new HashMap<>();
         for (Map.Entry<String, Map<String, String>> entry : map.entrySet()) {
             result.put(entry.getKey(), join(entry.getValue(), separator));
         }
@@ -94,7 +94,7 @@ public class CollectionUtils {
         if (list == null) {
             return null;
         }
-        Map<String, String> map = new HashMap<String, String>();
+        Map<String, String> map = new HashMap<>();
         if (list.isEmpty()) {
             return map;
         }
@@ -113,7 +113,7 @@ public class CollectionUtils {
         if (map == null) {
             return null;
         }
-        List<String> list = new ArrayList<String>();
+        List<String> list = new ArrayList<>();
         if (map.size() == 0) {
             return list;
         }
@@ -172,7 +172,7 @@ public class CollectionUtils {
     }
 
     public static Map<String, String> toStringMap(String... pairs) {
-        Map<String, String> parameters = new HashMap<String, String>();
+        Map<String, String> parameters = new HashMap<>();
         if (pairs.length > 0) {
             if (pairs.length % 2 != 0) {
                 throw new IllegalArgumentException("pairs must be even.");
@@ -186,7 +186,7 @@ public class CollectionUtils {
 
     @SuppressWarnings("unchecked")
     public static <K, V> Map<K, V> toMap(Object... pairs) {
-        Map<K, V> ret = new HashMap<K, V>();
+        Map<K, V> ret = new HashMap<>();
         if (pairs == null || pairs.length == 0) {
             return ret;
         }