Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement on Parameters and CollectionUtils #2790

Merged
merged 1 commit into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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