Skip to content

Commit

Permalink
improvement on Parameters and CollectionUtils (apache#2790)
Browse files Browse the repository at this point in the history
Use CollectionUtils.toStringMap to reduce duplicate code in Parameters, and use Java 8 type inference for collections.
  • Loading branch information
kun-song authored and carryxyh committed Nov 16, 2018
1 parent b1a7622 commit 9466425
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
14 changes: 3 additions & 11 deletions dubbo-common/src/main/java/org/apache/dubbo/common/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -42,20 +43,11 @@ public Parameters(String... pairs) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static Map<String, Map<String, String>> splitAll(Map<String, List<String>
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));
}
Expand All @@ -83,7 +83,7 @@ public static Map<String, List<String>> joinAll(Map<String, Map<String, String>>
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));
}
Expand All @@ -94,7 +94,7 @@ public static Map<String, String> split(List<String> list, String separator) {
if (list == null) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
Map<String, String> map = new HashMap<>();
if (list.isEmpty()) {
return map;
}
Expand All @@ -113,7 +113,7 @@ public static List<String> join(Map<String, String> map, String separator) {
if (map == null) {
return null;
}
List<String> list = new ArrayList<String>();
List<String> list = new ArrayList<>();
if (map.size() == 0) {
return list;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ private static boolean objectEquals(Object obj1, Object obj2) {
}

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.");
Expand All @@ -186,7 +186,7 @@ public static Map<String, String> toStringMap(String... pairs) {

@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;
}
Expand Down

0 comments on commit 9466425

Please sign in to comment.