X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FBundleHelper.java;fp=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FBundleHelper.java;h=c6b26c71985048cfb94fa5cdbf1f7f027b9f9bb8;hb=856f5898f4dd716ac0bbf068fd9925b20cb27567;hp=5d2638f865fb965df7b41629c8a03ea34fdec4ff;hpb=efba985031fb5169a3099eeab4b51309ae0b649e;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/resources/BundleHelper.java b/src/be/nikiroo/utils/resources/BundleHelper.java index 5d2638f..c6b26c7 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: + *

* * @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...). *