Class Appenders
- java.lang.Object
-
- org.sosy_lab.common.Appenders
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Appenders.AbstractAppender
Base implementation ofAppender
that ensures that theAppenders.AbstractAppender.toString()
method returns the same result thatAppender.appendTo(Appendable)
produces in order to ensure that the contract ofAppender
is fulfilled.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
appendTo(Appendable output, @Nullable Object o)
Write the given object into the given output.static StringBuilder
appendTo(StringBuilder sb, Appender a)
Let anAppender
dump itself into aStringBuilder
.static Appender
concat(Iterable<Appender> pAppenders)
Create a newAppender
that consists of the sequential concatenation of multiple appenders.static Appender
concat(Appender... pAppenders)
Create a newAppender
that consists of the sequential concatenation of multiple appenders.static Appender
createAppender(@Nullable Object o)
Return anAppender
for the given object.static Appender
forIterable(Joiner joiner, Iterable<?> it)
static Appender
forMap(Joiner.MapJoiner joiner, Map<?,?> map)
static Appender
fromToStringMethod(@Nullable Object o)
Return anAppender
that writes the result of theObject.toString()
method of an object into the output.static String
toString(Appender a)
Convert anAppender
into a string by calling it'sAppender.appendTo(Appendable)
method.static String
toStringWithTruncation(Appender a, int truncateAt)
Convert anAppender
into a string by calling it'sAppender.appendTo(Appendable)
method.
-
-
-
Method Detail
-
createAppender
public static Appender createAppender(@Nullable Object o)
Return anAppender
for the given object. If the object is anAppender
itself, it is returned. Otherwise an appender that callsObject.toString()
is returned (c.f.fromToStringMethod(Object)
.- Parameters:
o
- The object which will be dumped, may be null.- Returns:
- an
Appender
instance
-
appendTo
public static void appendTo(Appendable output, @Nullable Object o) throws IOException
Write the given object into the given output. If the object is anAppender
, itsAppender.appendTo(Appendable)
method is called, otherwise theObject.toString()
method is called. The object may benull
, in this case"null"
is written.- Parameters:
output
- The appendable to write into.o
- The object which will be dumped, may be null.- Throws:
IOException
- If the appendable throws an IOException
-
appendTo
@CanIgnoreReturnValue public static StringBuilder appendTo(StringBuilder sb, Appender a)
Let anAppender
dump itself into aStringBuilder
. This method is similar to passing theStringBuilder
to theAppender.appendTo(Appendable)
method, just without the checked exception.- Parameters:
sb
- The StringBuilder that will receive the content.a
- The Appender to dump into the StringBuilder.- Returns:
- The passed StringBuilder to allow for method chaining.
-
forIterable
public static Appender forIterable(Joiner joiner, Iterable<?> it)
- Parameters:
joiner
- The joiner that will be used to create a string representation of the iterable.it
- The iterable which will be dumped.- Returns:
- an
Appender
instance
-
forMap
public static Appender forMap(Joiner.MapJoiner joiner, Map<?,?> map)
- Parameters:
joiner
- The joiner that will be used to create a string representation of the map.map
- The map which will be dumped.- Returns:
- an
Appender
instance
-
fromToStringMethod
public static Appender fromToStringMethod(@Nullable Object o)
Return anAppender
that writes the result of theObject.toString()
method of an object into the output.This will not give the performance benefit that is expected from the use of appenders, and should only be used to adapt classes not implementing this interface themselves.
If
null
is passed, the resulting appender will write"null"
. If an object is passed, the appender will call theObject.toString()
method once each time it is used (no caching is done).- Parameters:
o
- The object which will be dumped, may be null.- Returns:
- an
Appender
instance
-
concat
public static Appender concat(Iterable<Appender> pAppenders)
Create a newAppender
that consists of the sequential concatenation of multiple appenders. The given iterable is traversed once each time the resulting appender'sAppender.appendTo(Appendable)
method is called. The iterable may not contain nulls or be null itself..
-
concat
public static Appender concat(Appender... pAppenders)
Create a newAppender
that consists of the sequential concatenation of multiple appenders.- Throws:
NullPointerException
- if any of the provided appendables is null
-
toString
public static String toString(Appender a)
Convert anAppender
into a string by calling it'sAppender.appendTo(Appendable)
method.Note that the contract of
Appender
specifies that you should be able to callObject.toString()
on the object and get the same result, thus it should not be necessary to call this method from client code.However, it may be practical to implement the
Object.toString()
method of anAppender
by delegating to this method.- Parameters:
a
- TheAppender
to convert into a string.- Returns:
- a string representation of the passed object.
-
toStringWithTruncation
public static String toStringWithTruncation(Appender a, int truncateAt)
Convert anAppender
into a string by calling it'sAppender.appendTo(Appendable)
method.This method truncates the returned string at a given length, and tries to be more efficient than generating the full string and truncating it at the end (though no guarantees are made).
- Parameters:
a
- TheAppender
to convert into a string.truncateAt
- The maximum size of the returned string(>= 0)
- Returns:
- a string representation of the passed object, with a maximum size of
truncateAt
-
-