diff --git a/common/source/java/ch/systemsx/cisd/common/properties/PropertyUtils.java b/common/source/java/ch/systemsx/cisd/common/properties/PropertyUtils.java index 0e493407ae606b62ce25d4b15c2f23f6f4ff1f22..c14c5879c45b74b8116b279f44968d3b9ffdbec8 100644 --- a/common/source/java/ch/systemsx/cisd/common/properties/PropertyUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/properties/PropertyUtils.java @@ -16,6 +16,7 @@ package ch.systemsx.cisd.common.properties; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; @@ -214,6 +215,26 @@ public final class PropertyUtils } return Arrays.asList(items); } + + /** + * Returns list of comma separated values at the specific property key. Each element is trimmed. + * + * @return an empty list if the property is undefined or an empty string. + */ + public static List<String> getList(Properties properties, String propertyKey) + { + List<String> result = new ArrayList<String>(); + String property = PropertyUtils.getProperty(properties, propertyKey); + if (StringUtils.isNotBlank(property)) + { + String[] splittedProperty = property.split(","); + for (String term : splittedProperty) + { + result.add(term.trim()); + } + } + return result; + } /** * Looks up given <var>propertyKey</var> in given <var>properties</var>. diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/properties/PropertyUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/properties/PropertyUtilsTest.java index 7d407291ab91c774d188bdce2f67f2a7e3ca8c0a..5ee52804ed9ac0372746e1ecb5e780eca159c4e5 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/properties/PropertyUtilsTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/properties/PropertyUtilsTest.java @@ -234,4 +234,30 @@ public final class PropertyUtilsTest extends AbstractFileSystemTestCase assertEquals(4, PropertyUtils.getPosLong(properties, key, 4)); assertEquals(-7, PropertyUtils.getLong(properties, key, 4)); } + + @Test + public void testGetList() + { + Properties properties = new Properties(); + properties.setProperty("key", " a, gamma, Einstein"); + + assertEquals("[a, gamma, Einstein]", PropertyUtils.getList(properties, "key").toString()); + } + + @Test + public void testGetListForUndefinedProperty() + { + Properties properties = new Properties(); + + assertEquals("[]", PropertyUtils.getList(properties, "key").toString()); + } + + @Test + public void testGetListForEmptyProperty() + { + Properties properties = new Properties(); + properties.setProperty("key", " "); + + assertEquals("[]", PropertyUtils.getList(properties, "key").toString()); + } } \ No newline at end of file