From: Mathieu Baudier Date: Tue, 22 Sep 2020 07:56:48 +0000 (+0200) Subject: Introduce Dictionary to String Map conversion. X-Git-Tag: argeo-commons-2.1.89~94 X-Git-Url: http://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=3a410067ad1efc482f3a445a7c03b60be077cda0 Introduce Dictionary to String Map conversion. --- diff --git a/org.argeo.util/src/org/argeo/util/LangUtils.java b/org.argeo.util/src/org/argeo/util/LangUtils.java index b791f490d..968f8ffe1 100644 --- a/org.argeo.util/src/org/argeo/util/LangUtils.java +++ b/org.argeo.util/src/org/argeo/util/LangUtils.java @@ -12,7 +12,9 @@ import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.util.Dictionary; import java.util.Enumeration; +import java.util.HashMap; import java.util.Hashtable; +import java.util.Map; import java.util.Properties; import javax.naming.InvalidNameException; @@ -41,7 +43,7 @@ public class LangUtils { * Creates a new {@link Dictionary} with one key-value pair (neither key not * value should be null) */ - public static Dictionary dico(String key, Object value) { + public static Dictionary dict(String key, Object value) { assert key != null; assert value != null; Hashtable props = new Hashtable<>(); @@ -49,6 +51,26 @@ public class LangUtils { return props; } + /**@deprecated Use {@link #dict(String, Object)} instead.*/ + @Deprecated + public static Dictionary dico(String key, Object value) { + return dict(key, value); + } + + /** Converts a {@link Dictionary} to a {@link Map} of strings. */ + public static Map dictToStringMap(Dictionary properties) { + if (properties == null) { + return null; + } + Map res = new HashMap<>(properties.size()); + Enumeration keys = properties.keys(); + while (keys.hasMoreElements()) { + String key = keys.nextElement(); + res.put(key, properties.get(key).toString()); + } + return res; + } + /** * Wraps the keys of the provided {@link Dictionary} as an {@link Iterable}. */