From: Niki Roo Date: Mon, 20 May 2019 20:08:29 +0000 (+0200) Subject: Bundle: use default Meta value if set X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=13bfeea6ce8894711317f8259b143984210be0c4;p=fanfix-jexer.git Bundle: use default Meta value if set --- diff --git a/changelog.md b/changelog.md index 4d99aaa..3530fff 100644 --- a/changelog.md +++ b/changelog.md @@ -7,6 +7,7 @@ - new: stream classes - new: Bundles can now also set Boolean, Integer... and not just get them - new: Bundles get/setList() +- new: Bundles can now use the default values provided by the Meta - fix: IOUtils.readSmallStream and \n at the end - fix: Base64 implementation changed, no strange errors anymore - change: StringUtils.unzip64(String) now returns a byte[] (StringUtils.unzip64s(String) can be used instead) diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index 85abfe7..09481c4 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -72,6 +72,27 @@ public class Bundle> { setBundle(name, Locale.getDefault(), false); } + /** + * Check if the setting is set into this {@link Bundle}. + * + * @param id + * the id of the setting to check + * @param includeDefaultValue + * TRUE to only return false when the setting is not set AND + * there is no default value + * + * @return TRUE if the setting is set + */ + public boolean iSet(E id, boolean includeDefaultValue) { + if (getString(id.toString(), null) == null) { + if (!includeDefaultValue || getString(id) == null) { + return false; + } + } + + return true; + } + /** * Return the value associated to the given id as a {@link String}. * @@ -82,7 +103,34 @@ public class Bundle> { * resource file) */ public String getString(E id) { - return getString(id.name()); + return getString(id, null); + } + + /** + * Return the value associated to the given id as a {@link String}. + *

+ * If no value is associated, take the default one if any. + * + * @param id + * the id of the value to get + * @param def + * the default value when it is not present in the config file + * + * @return the associated value, or NULL if not found (not present in the + * resource file) + */ + public String getString(E id, String def) { + String rep = getString(id.name(), null); + if (rep == null) { + MetaInfo info = new MetaInfo(type, this, id); + rep = info.getDefaultString(); + } + + if (rep == null) { + rep = def; + } + + return rep; } /** @@ -103,6 +151,8 @@ public class Bundle> { * with the runtime value "_suffix" (that is, "_" and suffix). *

* Will only accept suffixes that form an existing id. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -153,6 +203,8 @@ public class Bundle> { /** * Return the value associated to the given id as a {@link Boolean}. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -166,6 +218,8 @@ public class Bundle> { /** * Return the value associated to the given id as a {@link Boolean}. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -198,6 +252,8 @@ public class Bundle> { /** * Return the value associated to the given id as an {@link Integer}. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -210,6 +266,8 @@ public class Bundle> { /** * Return the value associated to the given id as an int. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -242,6 +300,8 @@ public class Bundle> { /** * Return the value associated to the given id as a {@link Character}. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -254,6 +314,8 @@ public class Bundle> { /** * Return the value associated to the given id as a {@link Character}. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -276,6 +338,8 @@ public class Bundle> { * and can be parsed. *

* The returned value is an ARGB value. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -303,6 +367,8 @@ public class Bundle> { /** * Return the value associated to the given id as a list of values if it is * found and can be parsed. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -455,15 +521,17 @@ public class Bundle> { } /** - * Get the value for the given key if it exists in the internal map, or NULL - * if not. + * Get the value for the given key if it exists in the internal map, or + * def if not. * * @param key * the key to check for + * @param def + * the default value when it is not present in the internal map * - * @return the value, or NULL + * @return the value, or def if not found */ - protected String getString(String key) { + protected String getString(String key, String def) { if (changeMap.containsKey(key)) { return changeMap.get(key); } @@ -472,7 +540,7 @@ public class Bundle> { return map.get(key); } - return null; + return def; } /** diff --git a/src/be/nikiroo/utils/resources/TransBundle.java b/src/be/nikiroo/utils/resources/TransBundle.java index fb9f290..192945f 100644 --- a/src/be/nikiroo/utils/resources/TransBundle.java +++ b/src/be/nikiroo/utils/resources/TransBundle.java @@ -137,7 +137,7 @@ public class TransBundle> extends Bundle { } else if ("DUMMY".equals(id.name().toUpperCase())) { result = "[" + key.toLowerCase() + "]"; } else if (containsKey(key)) { - result = getString(key); + result = getString(key, null); } else { result = null; } @@ -326,7 +326,7 @@ public class TransBundle> extends Bundle { String name = id.name() + "_NOUTF"; if (containsKey(name)) { - String value = getString(name); + String value = getString(name, null); writeValue(writer, name, value); } }