diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/CommaSeparatedListBuilder.java b/common/source/java/ch/systemsx/cisd/common/utilities/CommaSeparatedListBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..32f460af5fe8f2c1fb86b17cfc2b060bb72b2e57 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/utilities/CommaSeparatedListBuilder.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.utilities; + +import java.util.List; + +/** + * Builder of a comma-separated list of stringified objects. + * + * @author Franz-Josef Elmer + */ +public class CommaSeparatedListBuilder +{ + /** + * Creates comma-separated list from specified objects. + */ + public static String toString(Object... objects) + { + CommaSeparatedListBuilder listBuilder = new CommaSeparatedListBuilder(); + for (Object object : objects) + { + listBuilder.append(object); + } + return listBuilder.toString(); + } + + /** + * Creates comma-separated list from specified objects. + */ + public static String toString(List<?> objects) + { + CommaSeparatedListBuilder listBuilder = new CommaSeparatedListBuilder(); + for (Object object : objects) + { + listBuilder.append(object); + } + return listBuilder.toString(); + } + + private final StringBuilder builder = new StringBuilder(); + + /** + * Appends specified object. + */ + public void append(Object object) + { + if (builder.length() > 0) + { + builder.append(", "); + } + builder.append(object); + } + + /** + * Returns comma-separated list of appended objects. + */ + @Override + public String toString() + { + return builder.toString(); + } +} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/CommaSeparatedListBuilderTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/CommaSeparatedListBuilderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8d5963d00eedac0eeaffb450d7ae068e5abb7eb7 --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/CommaSeparatedListBuilderTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.utilities; + +import java.util.Arrays; + +import org.testng.AssertJUnit; +import org.testng.annotations.Test; + +/** + * + * + * @author Franz-Josef Elmer + */ +public class CommaSeparatedListBuilderTest extends AssertJUnit +{ + + @Test + public void testNoObjectsAppended() + { + CommaSeparatedListBuilder builder = new CommaSeparatedListBuilder(); + + assertEquals("", builder.toString()); + } + + @Test + public void testOneObjectAppended() + { + CommaSeparatedListBuilder builder = new CommaSeparatedListBuilder(); + builder.append("hello"); + + assertEquals("hello", builder.toString()); + } + + @Test + public void testTwoObjectsAppended() + { + CommaSeparatedListBuilder builder = new CommaSeparatedListBuilder(); + builder.append("hello"); + builder.append(null); + + assertEquals("hello, null", builder.toString()); + } + + @Test + public void testToStringArray() + { + assertEquals("hello, 42", CommaSeparatedListBuilder.toString("hello", 42)); + } + + @Test + public void testToStringList() + { + assertEquals("hello, 42", CommaSeparatedListBuilder.toString(Arrays.<Object>asList("hello", 42))); + } + +}