Skip to content

Instantly share code, notes, and snippets.

@antonio
Created December 28, 2011 13:29
Show Gist options
  • Save antonio/1527939 to your computer and use it in GitHub Desktop.
Save antonio/1527939 to your computer and use it in GitHub Desktop.
getList method in ConfigurationUtil
/**
* Returns the value of key as a String list. For example, it would return
* my_property = [ "first_element", "second_element" ]
* as new ArrayList<String>(){{ add("first_element"); add("second_element") }};
* @param key The key to return
* @return The List of values
*/
public static List<String> getList(String key) {
final List<String> result = new ArrayList<String>();
String rawString = getConfig(key);
final List<String> unprocessedArray =
Arrays.asList(StringUtils.split((String) rawString, ","));
for (final String s : unprocessedArray) {
result.add(cleanupArrayString(s));
}
return result;
}
private static String cleanupArrayString(final String s) {
/*
* We need to clean the collection strings because by using the
* toProperties method of the ConfigSlurper, the array elements get
* spaces and the '[' and ']' characters
*/
String result = StringUtils.trim(s);
result = StringUtils.removeStart(result, "[");
result = StringUtils.removeEnd(result, "]");
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment