From 60a6ac104771452e9b49199dd19dedbac52cb4e5 Mon Sep 17 00:00:00 2001 From: Mathieu Lirzin Date: Fri, 19 Apr 2019 14:57:54 +0000 Subject: [PATCH] =?UTF-8?q?Improved:=20Add=20=E2=80=98UtilMisc#toMap(Suppl?= =?UTF-8?q?ier>,=20Object...)=E2=80=99=20(OFBIZ-10933)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is convenient to use ‘UtilMisc#toMap’ to create pseudo-literal maps. Previously it was not possible to specify the concrete class of the constructed map. The ‘UtilMisc#toMap(Supplier>, Object...)’ overload now provides a convenient way to instantiate specific map types like ‘LinkedHashMap’ objects. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1857817 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/ofbiz/base/util/UtilMisc.java | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java index 4ba5d32880..7f769444e6 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java @@ -39,6 +39,7 @@ import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; +import java.util.function.Supplier; import org.apache.ofbiz.base.util.collections.MapComparator; import org.apache.ofbiz.entity.Delegator; @@ -108,22 +109,38 @@ public final class UtilMisc { } /** - * Create a map from passed nameX, valueX parameters - * @return The resulting Map + * Creates a pseudo-literal map corresponding to key-values. + * + * @param kvs the key-value pairs + * @return the corresponding map. + * @throws IllegalArgumentException when the key-value list is not even. + */ + public static Map toMap(Object... kvs) { + return toMap(HashMap::new, kvs); + } + + /** + * Creates a pseudo-literal map corresponding to key-values. + * + * @param constructor the constructor used to instantiate the map + * @param kvs the key-value pairs + * @return the corresponding map. + * @throws IllegalArgumentException when the key-value list is not even. */ @SuppressWarnings("unchecked") - public static Map toMap(Object... data) { - if (data.length == 1 && data[0] instanceof Map) { - return UtilGenerics.checkMap(data[0]); + public static Map toMap(Supplier> constructor, Object... kvs) { + if (kvs.length == 1 && kvs[0] instanceof Map) { + return UtilGenerics.checkMap(kvs[0]); } - if (data.length % 2 == 1) { - IllegalArgumentException e = new IllegalArgumentException("You must pass an even sized array to the toMap method (size = " + data.length + ")"); + if (kvs.length % 2 == 1) { + IllegalArgumentException e = new IllegalArgumentException( + "You must pass an even sized array to the toMap method (size = " + kvs.length + ")"); Debug.logInfo(e, module); throw e; } - Map map = new HashMap<>(); - for (int i = 0; i < data.length;) { - map.put((K) data[i++], (V) data[i++]); + Map map = constructor.get(); + for (int i = 0; i < kvs.length;) { + map.put((K) kvs[i++], (V) kvs[i++]); } return map; }