Skip to content

Instantly share code, notes, and snippets.

@vmassol
Created March 22, 2012 21:00
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/2164507 to your computer and use it in GitHub Desktop.
Save vmassol/2164507 to your computer and use it in GitHub Desktop.
Printer with ToStringBuilder
private class XWikiStyle extends StandardToStringStyle
{
public XWikiStyle()
{
super();
setUseClassName(false);
setUseIdentityHashCode(false);
setContentStart("");
setContentEnd("]");
setFieldNameValueSeparator(" = [");
setFieldSeparator("], ");
}
protected void appendDetail(StringBuffer buffer, String fieldName, Collection<?> coll)
{
Iterator<?> it = coll.iterator();
while (it.hasNext()) {
Object value = it.next();
buffer.append('[');
buffer.append(value);
buffer.append(']');
if (it.hasNext()) {
buffer.append(',');
buffer.append(' ');
}
}
}
protected void appendDetail(StringBuffer buffer, String fieldName, Map map)
{
Iterator<Map.Entry> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
buffer.append('[');
buffer.append(entry.getKey());
buffer.append(']');
buffer.append(' ');
buffer.append('=');
buffer.append(' ');
buffer.append('[');
buffer.append(entry.getValue());
buffer.append(']');
if (it.hasNext()) {
buffer.append(',');
buffer.append(' ');
}
}
}
}
/**
* {@inheritDoc}
* <p>
* The output is syntax independent since this class is used for all syntaxes. Specific syntaxes should extend this
* class and override this method to perform syntax-dependent formatting.
*
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
ToStringBuilder builder = new ToStringBuilder(this, new XWikiStyle());
builder = builder.append("Typed", isTyped())
.append("Type", getType().getScheme());
if (getReference() != null) {
builder = builder.append("Reference", getReference());
}
if (!getBaseReferences().isEmpty()) {
builder = builder.append("Base References", getBaseReferences());
}
Map<String, String> params = getParameters();
if (!params.isEmpty()) {
builder = builder.append("Parameters", params);
}
return builder.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment