Class Appenders
Appenders for various cases.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBase implementation ofAppenderthat ensures that theAppenders.AbstractAppender.toString()method returns the same result thatAppender.appendTo(Appendable)produces in order to ensure that the contract ofAppenderis fulfilled. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendTo(Appendable output, @Nullable Object o) Write the given object into the given output.static StringBuilderappendTo(StringBuilder sb, Appender a) Let anAppenderdump itself into aStringBuilder.static AppenderCreate a newAppenderthat consists of the sequential concatenation of multiple appenders.static AppenderCreate a newAppenderthat consists of the sequential concatenation of multiple appenders.static AppendercreateAppender(@Nullable Object o) Return anAppenderfor the given object.static AppenderforIterable(Joiner joiner, Iterable<?> it) static AppenderforMap(Joiner.MapJoiner joiner, Map<?, ?> map) static AppenderfromToStringMethod(@Nullable Object o) Return anAppenderthat writes the result of theObject.toString()method of an object into the output.static StringConvert anAppenderinto a string by calling it'sAppender.appendTo(Appendable)method.static StringtoStringWithTruncation(Appender a, int truncateAt) Convert anAppenderinto a string by calling it'sAppender.appendTo(Appendable)method.
-
Method Details
-
createAppender
Return anAppenderfor the given object. If the object is anAppenderitself, 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
Appenderinstance
-
appendTo
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
Let anAppenderdump itself into aStringBuilder. This method is similar to passing theStringBuilderto 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
- 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
Appenderinstance
-
forMap
- 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
Appenderinstance
-
fromToStringMethod
Return anAppenderthat 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
nullis 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
Appenderinstance
-
concat
Create a newAppenderthat 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
Create a newAppenderthat consists of the sequential concatenation of multiple appenders.- Throws:
NullPointerException- if any of the provided appendables is null
-
toString
Convert anAppenderinto a string by calling it'sAppender.appendTo(Appendable)method.Note that the contract of
Appenderspecifies 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 anAppenderby delegating to this method.- Parameters:
a- TheAppenderto convert into a string.- Returns:
- a string representation of the passed object.
-
toStringWithTruncation
Convert anAppenderinto 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- TheAppenderto 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
-