logahawk.formatters
Class HierarchicalCollectionArgFormatter
java.lang.Object
logahawk.formatters.AbstractArgumentFormatter
logahawk.formatters.CollectionArgFormatter
logahawk.formatters.HierarchicalCollectionArgFormatter
- All Implemented Interfaces:
- ArgumentFormatter
@Immutable
public class HierarchicalCollectionArgFormatter
- extends CollectionArgFormatter
Writes each object in the Collection
s in a way that shows a hierarchy between the objects. This formatter
will create a single line result if no objects in the collection are multi-line (contain Logger.LINE_SEPARATOR
, otherwise this formatter will create a multi-line result. The first object in the list is
considered the root, the last object is considered the child.
For single-line results, a ConcatCollectionArgFormatter
will be used. This allows the separator to be
different depending on single-line and multi-line results.
This class assumes that the arguments are a hierarchy, but does not do any validation of that assumption. Therefore
this class is not intended to be used in a generic ArgumentFormatterContainer
, it is designed to be used in
situations similar to the needs of ParentObjectArgFormatter
.
For a list containing the single-line strings "Alpha", "Beta", and "Gamma" (where "Gamma" is the child), and the
inner ConcatCollectionArgFormatter
's separator is " -> " the output will look like:
Alpha -> Beta -> Gama
For a list containing the three multi-line strings:
Alpha
First Line
Second Line
Beta
First Line
Second Line
Gama
First Line
Second Line
(where "Gamma" is the child), with the multiLineSeparator
is "|-- ", the output will look like:
|-- Alpha
First Line
Second Line
|-- Beta
First Line
Second Line
|-- Gamma
First Line
Second Line
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
concat
protected final ConcatCollectionArgFormatter concat
- Used to merge the formatted lines into one big String with the appropriate new lines.
singleLineFormatter
protected final ConcatCollectionArgFormatter singleLineFormatter
- Used to format the message when none of the items in the hierarchy are multi-line.
multiLineSeparator
protected final String multiLineSeparator
- Separator between items in the hierarchy.
HierarchicalCollectionArgFormatter
public HierarchicalCollectionArgFormatter()
HierarchicalCollectionArgFormatter
public HierarchicalCollectionArgFormatter(ConcatCollectionArgFormatter singleLineFormatter,
String multiLineSeparator)
- Parameters:
multiLineSeparator
- singleLineFormatter
-
format
public String format(Object obj,
Collection<ArgumentFormatter> formatters,
int indentLevel)
- Formats the provided object into a list of objects, displaying the index before each item. The provided formatters
will be used to format each item, using
AbstractArgumentFormatter.getChildObjectFormat(java.lang.Object, java.util.Collection, int)
. If there is only
one item, this will simply format that one item and return it, without all the index and other formatting.
format
protected String format(List<Object> list,
Collection<ArgumentFormatter> formatters,
int indentLevel)