예제 #1
0
  /**
   * Value is split by comma and trimmed. Never returns null.
   *
   * <p>Examples :
   *
   * <ul>
   *   <li>"one,two,three " -> ["one", "two", "three"]
   *   <li>" one, two, three " -> ["one", "two", "three"]
   *   <li>"one, , three" -> ["one", "", "three"]
   * </ul>
   */
  public String[] getStringArray(String key) {
    PropertyDefinition property = getDefinitions().get(key);
    if ((null != property) && (property.multiValues())) {
      String value = getString(key);
      if (value == null) {
        return ArrayUtils.EMPTY_STRING_ARRAY;
      }

      List<String> values = new ArrayList<>();
      for (String v : Splitter.on(",").trimResults().split(value)) {
        values.add(v.replace("%2C", ","));
      }
      return values.toArray(new String[values.size()]);
    }

    return getStringArrayBySeparator(key, ",");
  }
예제 #2
0
  public Settings setProperty(String key, @Nullable String[] values) {
    PropertyDefinition property = getDefinitions().get(key);
    if ((null == property) || (!property.multiValues())) {
      throw new IllegalStateException(
          "Fail to set multiple values on a single value property " + key);
    }

    String text = null;
    if (values != null) {
      List<String> escaped = new ArrayList<>();
      for (String value : values) {
        if (null != value) {
          escaped.add(value.replace(",", "%2C"));
        } else {
          escaped.add("");
        }
      }

      String escapedValue = Joiner.on(',').join(escaped);
      text = StringUtils.trim(escapedValue);
    }
    return setProperty(key, text);
  }