@InterfaceAudience.Public @InterfaceStability.Unstable public class BoundedAppender extends Object
CharSequence
appender that considers its limit
as upper
bound.
When limit
would be reached on append, past messages will be
truncated from head, and a header telling the user about truncation will be
prepended, with ellipses in between header and messages.
Note that header and ellipses are not counted against limit
.
An example:
// At the beginning it's an empty string
final Appendable shortAppender = new BoundedAppender(80);
// The whole message fits into limit
shortAppender.append(
"message1 this is a very long message but fitting into limit\n");
// The first message is truncated, the second not
shortAppender.append("message2 this is shorter than the previous one\n");
// The first message is deleted, the second truncated, the third
// preserved
shortAppender.append("message3 this is even shorter message, maybe.\n");
// The first two are deleted, the third one truncated, the last preserved
shortAppender.append("message4 the shortest one, yet the greatest :)");
// Current contents are like this:
// Diagnostic messages truncated, showing last 80 chars out of 199:
// ...s is even shorter message, maybe.
// message4 the shortest one, yet the greatest :)
Note that null values are append
ed
just like in original
implementation
.
Note that this class is not thread safe.
Modifier and Type | Field and Description |
---|---|
static String |
TRUNCATED_MESSAGES_TEMPLATE |
Constructor and Description |
---|
BoundedAppender(int limit) |
Modifier and Type | Method and Description |
---|---|
BoundedAppender |
append(CharSequence csq)
Append a
CharSequence considering limit , truncating
from the head of csq or messages when necessary. |
int |
getLimit() |
int |
length()
Get current length of messages considering truncates
without header and ellipses.
|
String |
toString()
Get a string representation of the actual contents, displaying also a
header and ellipses when there was a truncate.
|
public static final String TRUNCATED_MESSAGES_TEMPLATE
public BoundedAppender append(CharSequence csq)
CharSequence
considering limit
, truncating
from the head of csq
or messages
when necessary.csq
- the CharSequence
to appendpublic int length()
public int getLimit()
Copyright © 2020 Apache Software Foundation. All rights reserved.