From 3a410067ad1efc482f3a445a7c03b60be077cda0 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Tue, 22 Sep 2020 09:56:48 +0200 Subject: [PATCH] Introduce Dictionary to String Map conversion. --- .../src/org/argeo/util/LangUtils.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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}. */ -- 2.30.2