M
- Allow subclasses to use fluent APIs and override methods that return instances of subclasses.V
- The value type@AsynchronouslyFormattable public class MapMessage<M extends MapMessage<M,V>,V> extends Object implements MultiFormatStringBuilderFormattable
Thread-safety note: the contents of this message can be modified after construction. When using asynchronous loggers and appenders it is not recommended to modify this message after the message is logged, because it is undefined whether the logged message string will contain the old values or the modified values.
This class was pulled up from StringMapMessage
to allow for Objects as values.
Modifier and Type | Class and Description |
---|---|
static class |
MapMessage.MapFormat
When set as the format specifier causes the Map to be formatted as XML.
|
Constructor and Description |
---|
MapMessage()
Constructs a new instance.
|
MapMessage(int initialCapacity)
Constructs a new instance.
|
MapMessage(Map<String,V> map)
Constructs a new instance based on an existing
Map . |
Modifier and Type | Method and Description |
---|---|
protected void |
appendMap(StringBuilder sb) |
protected void |
asJava(StringBuilder sb) |
protected void |
asJavaUnquoted(StringBuilder sb) |
protected void |
asJson(StringBuilder sb) |
String |
asString()
Formats the Structured data as described in RFC 5424.
|
String |
asString(String format)
Formats the Structured data as described in RFC 5424.
|
void |
asXml(StringBuilder sb)
Formats this message as an XML fragment String into the given builder.
|
void |
clear()
Clear the data.
|
boolean |
containsKey(String key)
Returns
true if this data structure contains the specified key, false otherwise. |
boolean |
equals(Object o) |
<CV> void |
forEach(BiConsumer<String,? super CV> action)
Performs the given action for each key-value pair in this data structure
until all entries have been processed or the action throws an exception.
|
<CV,S> void |
forEach(TriConsumer<String,? super CV,S> action,
S state)
Performs the given action for each key-value pair in this data structure
until all entries have been processed or the action throws an exception.
|
void |
formatTo(String[] formats,
StringBuilder buffer)
Writes a text representation of this object into the specified
StringBuilder , ideally without allocating
temporary objects. |
void |
formatTo(StringBuilder buffer)
Writes a text representation of this object into the specified
StringBuilder , ideally without allocating
temporary objects. |
String |
get(String key)
Retrieves the value of the element with the specified key or null if the key is not present.
|
Map<String,V> |
getData()
Returns the message data as an unmodifiable Map.
|
String |
getFormat()
Returns the message.
|
String[] |
getFormats()
Returns the supported formats.
|
String |
getFormattedMessage()
Formats the message and return it.
|
String |
getFormattedMessage(String[] formats)
Returns the Message formatted as a String.
|
IndexedReadOnlyStringMap |
getIndexedReadOnlyStringMap()
Returns a read-only view of the message data.
|
Object[] |
getParameters()
Returns the data elements as if they were parameters on the logging event.
|
Throwable |
getThrowable()
Always returns null.
|
int |
hashCode() |
M |
newInstance(Map<String,V> map)
Constructs a new instance based on an existing Map.
|
void |
put(String candidateKey,
String value)
Adds an item to the data Map.
|
void |
putAll(Map<String,String> map)
Adds all the elements from the specified Map.
|
String |
remove(String key)
Removes the element with the specified name.
|
protected String |
toKey(String candidateKey)
Allows subclasses to change a candidate key to an actual key.
|
String |
toString() |
protected void |
validate(String key,
boolean value)
Default implementation does nothing.
|
protected void |
validate(String key,
byte value)
Default implementation does nothing.
|
protected void |
validate(String key,
char value)
Default implementation does nothing.
|
protected void |
validate(String key,
double value)
Default implementation does nothing.
|
protected void |
validate(String key,
float value)
Default implementation does nothing.
|
protected void |
validate(String key,
int value)
Default implementation does nothing.
|
protected void |
validate(String key,
long value)
Default implementation does nothing.
|
protected void |
validate(String key,
Object value)
Default implementation does nothing.
|
protected void |
validate(String key,
short value)
Default implementation does nothing.
|
protected void |
validate(String key,
String value)
Default implementation does nothing.
|
M |
with(String candidateKey,
boolean value)
Adds an item to the data Map.
|
M |
with(String candidateKey,
byte value)
Adds an item to the data Map.
|
M |
with(String candidateKey,
char value)
Adds an item to the data Map.
|
M |
with(String candidateKey,
double value)
Adds an item to the data Map.
|
M |
with(String candidateKey,
float value)
Adds an item to the data Map.
|
M |
with(String candidateKey,
int value)
Adds an item to the data Map.
|
M |
with(String candidateKey,
long value)
Adds an item to the data Map.
|
M |
with(String candidateKey,
Object value)
Adds an item to the data Map.
|
M |
with(String candidateKey,
short value)
Adds an item to the data Map.
|
M |
with(String candidateKey,
String value)
Adds an item to the data Map in fluent style.
|
public MapMessage()
public MapMessage(int initialCapacity)
initialCapacity
- the initial capacity.public MapMessage(Map<String,V> map)
Map
.map
- The Map.public String[] getFormats()
MultiformatMessage
getFormats
in interface MultiformatMessage
public Object[] getParameters()
getParameters
in interface Message
public Map<String,V> getData()
public IndexedReadOnlyStringMap getIndexedReadOnlyStringMap()
public void clear()
public boolean containsKey(String key)
true
if this data structure contains the specified key, false
otherwise.key
- the key whose presence to check. May be null
.true
if this data structure contains the specified key, false
otherwisepublic void put(String candidateKey, String value)
candidateKey
- The name of the data item.value
- The value of the data item.public void putAll(Map<String,String> map)
map
- The Map to add.public String get(String key)
key
- The name of the element.public String remove(String key)
key
- The name of the element.public String asString()
public String asString(String format)
format
- The format identifier.public <CV> void forEach(BiConsumer<String,? super CV> action)
Some implementations may not support structural modifications (adding new elements or removing elements) while
iterating over the contents. In such implementations, attempts to add or remove elements from the
BiConsumer
's BiConsumer.accept(Object, Object)
accept} method may cause a
ConcurrentModificationException
to be thrown.
CV
- type of the consumer valueaction
- The action to be performed for each key-value pair in this collectionConcurrentModificationException
- some implementations may not support structural modifications
to this data structure while iterating over the contents with forEach(BiConsumer)
or
forEach(TriConsumer, Object)
.ReadOnlyStringMap.forEach(BiConsumer)
public <CV,S> void forEach(TriConsumer<String,? super CV,S> action, S state)
The third parameter lets callers pass in a stateful object to be modified with the key-value pairs, so the TriConsumer implementation itself can be stateless and potentially reusable.
Some implementations may not support structural modifications (adding new elements or removing elements) while
iterating over the contents. In such implementations, attempts to add or remove elements from the
TriConsumer
's accept
method may cause a
ConcurrentModificationException
to be thrown.
CV
- type of the consumer valueS
- type of the third parameteraction
- The action to be performed for each key-value pair in this collectionstate
- the object to be passed as the third parameter to each invocation on the specified
triconsumerConcurrentModificationException
- some implementations may not support structural modifications
to this data structure while iterating over the contents with forEach(BiConsumer)
or
forEach(TriConsumer, Object)
.ReadOnlyStringMap.forEach(TriConsumer, Object)
public void asXml(StringBuilder sb)
sb
- format into this builder.public String getFormattedMessage()
getFormattedMessage
in interface Message
public String getFormattedMessage(String[] formats)
MultiformatMessage
getFormattedMessage
in interface MultiformatMessage
formats
- An array of Strings that provide extra information about how to format the message. MapMessage uses
the first format specifier it recognizes. The supported formats are XML, JSON, and JAVA. The default
format is key1="value1" key2="value2" as required by RFC
5424 messages.protected void appendMap(StringBuilder sb)
protected void asJson(StringBuilder sb)
protected void asJavaUnquoted(StringBuilder sb)
protected void asJava(StringBuilder sb)
public M newInstance(Map<String,V> map)
map
- The Map.public void formatTo(StringBuilder buffer)
StringBuilderFormattable
StringBuilder
, ideally without allocating
temporary objects.formatTo
in interface StringBuilderFormattable
buffer
- the StringBuilder to write intopublic void formatTo(String[] formats, StringBuilder buffer)
MultiFormatStringBuilderFormattable
StringBuilder
, ideally without allocating
temporary objects.formatTo
in interface MultiFormatStringBuilderFormattable
formats
- An array of Strings that provide extra information about how to format the message.
Each MultiFormatStringBuilderFormattable implementation is free to use the provided formats however they choose.buffer
- the StringBuilder to write intopublic Throwable getThrowable()
getThrowable
in interface Message
protected void validate(String key, boolean value)
protected void validate(String key, byte value)
protected void validate(String key, char value)
protected void validate(String key, double value)
protected void validate(String key, float value)
protected void validate(String key, int value)
protected void validate(String key, long value)
protected void validate(String key, Object value)
protected void validate(String key, short value)
protected void validate(String key, String value)
protected String toKey(String candidateKey)
candidateKey
- The candidate key.public M with(String candidateKey, boolean value)
candidateKey
- The name of the data item.value
- The value of the data item.public M with(String candidateKey, byte value)
candidateKey
- The name of the data item.value
- The value of the data item.public M with(String candidateKey, char value)
candidateKey
- The name of the data item.value
- The value of the data item.public M with(String candidateKey, double value)
candidateKey
- The name of the data item.value
- The value of the data item.public M with(String candidateKey, float value)
candidateKey
- The name of the data item.value
- The value of the data item.public M with(String candidateKey, int value)
candidateKey
- The name of the data item.value
- The value of the data item.public M with(String candidateKey, long value)
candidateKey
- The name of the data item.value
- The value of the data item.public M with(String candidateKey, Object value)
candidateKey
- The name of the data item.value
- The value of the data item.public M with(String candidateKey, short value)
candidateKey
- The name of the data item.value
- The value of the data item.Copyright © 1999-1969 The Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.