Skip to content
Snippets Groups Projects
Commit 20765f6f authored by felmer's avatar felmer
Browse files

LMS-2612 Introducing CommaSeparatedListBuilder

SVN: 24581
parent c8a9e675
No related branches found
No related tags found
No related merge requests found
/*
* 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();
}
}
/*
* 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)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment