Improved: Generalize toMap to abitrary key types
(OFBIZ-10354) The `toMap` static method doesn't use its 'K' generic type in its implementation Thanks: Mathieu Lirzin git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1830588 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -112,18 +112,18 @@ public final class UtilMisc {
|
||||
* @return The resulting Map
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <K, V> Map<String, V> toMap(Object... data) {
|
||||
public static <K, V> Map<K, V> toMap(Object... data) {
|
||||
if (data.length == 1 && data[0] instanceof Map) {
|
||||
return UtilGenerics.<String, V>checkMap(data[0]);
|
||||
return UtilGenerics.<K, V>checkMap(data[0]);
|
||||
}
|
||||
if (data.length % 2 == 1) {
|
||||
IllegalArgumentException e = new IllegalArgumentException("You must pass an even sized array to the toMap method (size = " + data.length + ")");
|
||||
Debug.logInfo(e, module);
|
||||
throw e;
|
||||
}
|
||||
Map<String, V> map = new HashMap<>();
|
||||
Map<K, V> map = new HashMap<>();
|
||||
for (int i = 0; i < data.length;) {
|
||||
map.put((String) data[i++], (V) data[i++]);
|
||||
map.put((K) data[i++], (V) data[i++]);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user