From 856f5898f4dd716ac0bbf068fd9925b20cb27567 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 1 Jun 2019 10:56:55 +0200 Subject: [PATCH] fix items in Bundle --- src/be/nikiroo/utils/resources/Bundle.java | 32 ++++-- .../nikiroo/utils/resources/BundleHelper.java | 106 +++++++++++++++++- src/be/nikiroo/utils/resources/MetaInfo.java | 13 +-- 3 files changed, 129 insertions(+), 22 deletions(-) diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index 5139287f..0c57bf99 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -92,7 +92,7 @@ public class Bundle> { /** * Check if the setting is set into this {@link Bundle}. * - * @param id + * @param name * the id of the setting to check * @param includeDefaultValue * TRUE to only return false when the setting is not set AND @@ -215,11 +215,7 @@ public class Bundle> { setString(id.name(), value); } else { List values = getList(id); - for (int i = values.size(); i < item; i++) { - values.add(null); - } - values.set(item, value); - setString(id.name(), BundleHelper.fromList(values)); + setString(id.name(), BundleHelper.fromList(values, value, item)); } } @@ -277,9 +273,6 @@ public class Bundle> { * the id of the value to get * @param suffix * the runtime suffix - * @param item - * the item number to get for an array of values, or -1 for - * non-arrays * @param def * the default value when it is not present in the config file * @param item @@ -577,6 +570,9 @@ public class Bundle> { * @param def * the default value when it is not present in the config file or * if it is not a char value + * @param item + * the item number to get for an array of values, or -1 for + * non-arrays * * @return the associated value */ @@ -645,6 +641,9 @@ public class Bundle> { * * @param id * the id of the value to get + * @param def + * the default value when it is not present in the config file or + * if it is not a char value * * @return the associated value */ @@ -667,6 +666,12 @@ public class Bundle> { * * @param id * the id of the value to get + * @param def + * the default value when it is not present in the config file or + * if it is not a char value + * @param item + * the item number to get for an array of values, or -1 for + * non-arrays * * @return the associated value */ @@ -733,6 +738,9 @@ public class Bundle> { * * @param id * the id of the value to get + * @param def + * the default value when it is not present in the config file or + * if it is not a char value * * @return the associated list, empty if the value is empty, NULL if it is * not found or cannot be parsed as a list @@ -754,6 +762,12 @@ public class Bundle> { * * @param id * the id of the value to get + * @param def + * the default value when it is not present in the config file or + * if it is not a char value + * @param item + * the item number to get for an array of values, or -1 for + * non-arrays * * @return the associated list, empty if the value is empty, NULL if it is * not found or cannot be parsed as a list diff --git a/src/be/nikiroo/utils/resources/BundleHelper.java b/src/be/nikiroo/utils/resources/BundleHelper.java index 5d2638f8..c6b26c71 100644 --- a/src/be/nikiroo/utils/resources/BundleHelper.java +++ b/src/be/nikiroo/utils/resources/BundleHelper.java @@ -262,14 +262,19 @@ class BundleHelper { } /** - * The size of this raw list. + * The size of this raw list (note than a NULL list is of size 0). * * @param raw * the raw list * - * @return its size if it is a list, -1 if not + * @return its size if it is a list (NULL is an empty list), -1 if it is not + * a list */ static public int getListSize(String raw) { + if (raw == null) { + return 0; + } + List list = parseList(raw, -1); if (list == null) { return -1; @@ -371,7 +376,17 @@ class BundleHelper { /** * Return a {@link String} representation of the given list of values. *

- * NULL will be assimilated to an empty {@link String}. + * NULL will be assimilated to an empty {@link String} if later non-null + * values exist, or just ignored if not. + *

+ * Example: + *

    + *
  • 1,NULL, 3 will become 1, + * "", 3
  • + *
  • 1,NULL, NULL will become 1
  • + *
  • NULL, NULL, NULL will become an empty list + *
  • + *
* * @param list * the input value @@ -379,17 +394,98 @@ class BundleHelper { * @return the raw {@link String} value that correspond to it */ static public String fromList(List list) { + if (list == null) { + list = new ArrayList(); + } + + int last = list.size() - 1; + for (int i = 0; i < list.size(); i++) { + if (list.get(i) != null) { + last = i; + } + } + StringBuilder builder = new StringBuilder(); - for (String item : list) { + for (int i = 0; i <= last; i++) { + String item = list.get(i); + if (item == null) { + item = ""; + } + if (builder.length() > 0) { builder.append(", "); } - builder.append(escape(item == null ? "" : item)); + builder.append(escape(item)); } return builder.toString(); } + /** + * Return a {@link String} representation of the given list of values. + *

+ * NULL will be assimilated to an empty {@link String} if later non-null + * values exist, or just ignored if not. + *

+ * Example: + *

    + *
  • 1,NULL, 3 will become 1, + * "", 3
  • + *
  • 1,NULL, NULL will become 1
  • + *
  • NULL, NULL, NULL will become an empty list + *
  • + *
+ * + * @param list + * the input value + * @param value + * the value to insert + * @param item + * the position to insert it at + * + * @return the raw {@link String} value that correspond to it + */ + static public String fromList(List list, String value, int item) { + if (list == null) { + list = new ArrayList(); + } + + while (item >= list.size()) { + list.add(null); + } + list.set(item, value); + + return fromList(list); + } + + /** + * Return a {@link String} representation of the given list of values. + *

+ * NULL will be assimilated to an empty {@link String} if later non-null + * values exist, or just ignored if not. + *

+ * Example: + *

    + *
  • 1,NULL, 3 will become 1, + * "", 3
  • + *
  • 1,NULL, NULL will become 1
  • + *
  • NULL, NULL, NULL will become an empty list + *
  • + *
+ * + * @param list + * the input value + * @param value + * the value to insert + * @param item + * the position to insert it at + * + * @return the raw {@link String} value that correspond to it + */ + static public String fromList(String list, String value, int item) { + return fromList(parseList(list, -1), value, item); + } + /** * Escape the given value for list formating (no carets, no NEWLINES...). *

diff --git a/src/be/nikiroo/utils/resources/MetaInfo.java b/src/be/nikiroo/utils/resources/MetaInfo.java index 8ec98f99..f7598f19 100644 --- a/src/be/nikiroo/utils/resources/MetaInfo.java +++ b/src/be/nikiroo/utils/resources/MetaInfo.java @@ -477,12 +477,7 @@ public class MetaInfo> implements Iterable> { */ public void setString(String value, int item) { if (isArray() && item >= 0) { - List values = BundleHelper.parseList(this.value, -1); - for (int i = values.size(); i <= item; i++) { - values.add(null); - } - values.set(item, value); - this.value = BundleHelper.fromList(values); + this.value = BundleHelper.fromList(this.value, value, item); } else { this.value = value; } @@ -575,7 +570,8 @@ public class MetaInfo> implements Iterable> { value = null; } - for (Runnable listener : reloadedListeners) { + // Copy the list so we can create new listener in a listener + for (Runnable listener : new ArrayList(reloadedListeners)) { try { listener.run(); } catch (Exception e) { @@ -607,7 +603,8 @@ public class MetaInfo> implements Iterable> { * dirty flag) */ public void save(boolean onlyIfDirty) { - for (Runnable listener : saveListeners) { + // Copy the list so we can create new listener in a listener + for (Runnable listener : new ArrayList(saveListeners)) { try { listener.run(); } catch (Exception e) { -- 2.27.0