Skip to content

Instantly share code, notes, and snippets.

@vmassol
Created March 22, 2012 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmassol/2160618 to your computer and use it in GitHub Desktop.
Save vmassol/2160618 to your computer and use it in GitHub Desktop.
Printer
private String append(Object... objects)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < objects.length; i += 2) {
String key = (String) objects[i];
Object value = objects[i + 1];
if (value instanceof List) {
appendList(buffer, key, (List) value);
} else {
appendLiteral(buffer, key, value);
}
if (i != objects.length - 2) {
append(' ');
}
}
return buffer.toString();
}
private void appendLiteral(StringBuffer buffer, String key, Object value)
{
buffer.append(String.format("%s = [%s]", key, value));
}
private void appendList(StringBuffer buffer, String key, List values)
{
buffer.append(key);
buffer.append(' ');
buffer.append('=');
buffer.append(' ');
buffer.append('[');
Iterator<String> it = values.listIterator();
while (it.hasNext()) {
Object value = it.next();
buffer.append('[');
buffer.append(value);
buffer.append(']');
if (it.hasNext()) {
buffer.append(',');
buffer.append(' ');
}
}
buffer.append(']');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment