Class MapMessage<M extends MapMessage<M,V>,V>
- java.lang.Object
- 
- org.apache.logging.log4j.message.MapMessage<M,V>
 
- 
- Type Parameters:
- M- Allow subclasses to use fluent APIs and override methods that return instances of subclasses.
- V- The value type
 - All Implemented Interfaces:
- Serializable,- Message,- MultiformatMessage,- MultiFormatStringBuilderFormattable,- StringBuilderFormattable
 - Direct Known Subclasses:
- StringMapMessage,- StructuredDataMessage
 
 @AsynchronouslyFormattable public class MapMessage<M extends MapMessage<M,V>,V> extends Object implements MultiFormatStringBuilderFormattable Represents a Message that consists of a Map.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 StringMapMessageto allow for Objects as values.- See Also:
- Serialized Form
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classMapMessage.MapFormatWhen set as the format specifier causes the Map to be formatted as XML.
 - 
Constructor SummaryConstructors Constructor 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 existingMap.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidappendMap(StringBuilder sb)protected voidasJava(StringBuilder sb)protected voidasJavaUnquoted(StringBuilder sb)protected voidasJson(StringBuilder sb)StringasString()Formats the Structured data as described in RFC 5424.StringasString(String format)Formats the Structured data as described in RFC 5424.voidasXml(StringBuilder sb)Formats this message as an XML fragment String into the given builder.voidclear()Clear the data.booleancontainsKey(String key)Returnstrueif this data structure contains the specified key,falseotherwise.booleanequals(Object o)<CV> voidforEach(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>
 voidforEach(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.voidformatTo(String[] formats, StringBuilder buffer)Writes a text representation of this object into the specifiedStringBuilder, ideally without allocating temporary objects.voidformatTo(StringBuilder buffer)Writes a text representation of this object into the specifiedStringBuilder, ideally without allocating temporary objects.Stringget(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.StringgetFormat()Returns the message.String[]getFormats()Returns the supported formats.StringgetFormattedMessage()Formats the message and return it.StringgetFormattedMessage(String[] formats)Returns the Message formatted as a String.IndexedReadOnlyStringMapgetIndexedReadOnlyStringMap()Returns a read-only view of the message data.Object[]getParameters()Returns the data elements as if they were parameters on the logging event.ThrowablegetThrowable()Always returns null.inthashCode()MnewInstance(Map<String,V> map)Constructs a new instance based on an existing Map.voidput(String candidateKey, String value)Adds an item to the data Map.voidputAll(Map<String,String> map)Adds all the elements from the specified Map.Stringremove(String key)Removes the element with the specified name.protected StringtoKey(String candidateKey)Allows subclasses to change a candidate key to an actual key.StringtoString()protected voidvalidate(String key, boolean value)Default implementation does nothing.protected voidvalidate(String key, byte value)Default implementation does nothing.protected voidvalidate(String key, char value)Default implementation does nothing.protected voidvalidate(String key, double value)Default implementation does nothing.protected voidvalidate(String key, float value)Default implementation does nothing.protected voidvalidate(String key, int value)Default implementation does nothing.protected voidvalidate(String key, long value)Default implementation does nothing.protected voidvalidate(String key, short value)Default implementation does nothing.protected voidvalidate(String key, Object value)Default implementation does nothing.protected voidvalidate(String key, String value)Default implementation does nothing.Mwith(String candidateKey, boolean value)Adds an item to the data Map.Mwith(String candidateKey, byte value)Adds an item to the data Map.Mwith(String candidateKey, char value)Adds an item to the data Map.Mwith(String candidateKey, double value)Adds an item to the data Map.Mwith(String candidateKey, float value)Adds an item to the data Map.Mwith(String candidateKey, int value)Adds an item to the data Map.Mwith(String candidateKey, long value)Adds an item to the data Map.Mwith(String candidateKey, short value)Adds an item to the data Map.Mwith(String candidateKey, Object value)Adds an item to the data Map.Mwith(String candidateKey, String value)Adds an item to the data Map in fluent style.
 
- 
- 
- 
Method Detail- 
getFormatspublic String[] getFormats() Description copied from interface:MultiformatMessageReturns the supported formats.- Specified by:
- getFormatsin interface- MultiformatMessage
- Returns:
- The supported formats.
 
 - 
getParameterspublic Object[] getParameters() Returns the data elements as if they were parameters on the logging event.- Specified by:
- getParametersin interface- Message
- Returns:
- the data elements.
 
 - 
getFormatpublic String getFormat() Returns the message.
 - 
getDatapublic Map<String,V> getData() Returns the message data as an unmodifiable Map.- Returns:
- the message data as an unmodifiable map.
 
 - 
getIndexedReadOnlyStringMappublic IndexedReadOnlyStringMap getIndexedReadOnlyStringMap() Returns a read-only view of the message data.- Returns:
- the read-only message data.
 
 - 
clearpublic void clear() Clear the data.
 - 
containsKeypublic boolean containsKey(String key) Returnstrueif this data structure contains the specified key,falseotherwise.- Parameters:
- key- the key whose presence to check. May be- null.
- Returns:
- trueif this data structure contains the specified key,- falseotherwise
- Since:
- 2.9
 
 - 
putpublic void put(String candidateKey, String value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
 
 - 
putAllpublic void putAll(Map<String,String> map) Adds all the elements from the specified Map.- Parameters:
- map- The Map to add.
 
 - 
getpublic String get(String key) Retrieves the value of the element with the specified key or null if the key is not present.- Parameters:
- key- The name of the element.
- Returns:
- The value of the element or null if the key is not present.
 
 - 
removepublic String remove(String key) Removes the element with the specified name.- Parameters:
- key- The name of the element.
- Returns:
- The previous value of the element.
 
 - 
asStringpublic String asString() Formats the Structured data as described in RFC 5424.- Returns:
- The formatted String.
 
 - 
asStringpublic String asString(String format) Formats the Structured data as described in RFC 5424.- Parameters:
- format- The format identifier.
- Returns:
- The formatted String.
 
 - 
forEachpublic <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.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'sBiConsumer.accept(Object, Object)accept} method may cause aConcurrentModificationExceptionto be thrown.- Type Parameters:
- CV- type of the consumer value
- Parameters:
- action- The action to be performed for each key-value pair in this collection
- Throws:
- ConcurrentModificationException- some implementations may not support structural modifications to this data structure while iterating over the contents with- forEach(BiConsumer)or- forEach(TriConsumer, Object).
- Since:
- 2.9
- See Also:
- ReadOnlyStringMap.forEach(BiConsumer)
 
 - 
forEachpublic <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.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'sacceptmethod may cause aConcurrentModificationExceptionto be thrown.- Type Parameters:
- CV- type of the consumer value
- S- type of the third parameter
- Parameters:
- action- The action to be performed for each key-value pair in this collection
- state- the object to be passed as the third parameter to each invocation on the specified triconsumer
- Throws:
- ConcurrentModificationException- some implementations may not support structural modifications to this data structure while iterating over the contents with- forEach(BiConsumer)or- forEach(TriConsumer, Object).
- Since:
- 2.9
- See Also:
- ReadOnlyStringMap.forEach(TriConsumer, Object)
 
 - 
asXmlpublic void asXml(StringBuilder sb) Formats this message as an XML fragment String into the given builder.- Parameters:
- sb- format into this builder.
 
 - 
getFormattedMessagepublic String getFormattedMessage() Formats the message and return it.- Specified by:
- getFormattedMessagein interface- Message
- Returns:
- the formatted message.
 
 - 
getFormattedMessagepublic String getFormattedMessage(String[] formats) Description copied from interface:MultiformatMessageReturns the Message formatted as a String.- Specified by:
- getFormattedMessagein interface- MultiformatMessage
- Parameters:
- 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.
- Returns:
- The formatted message.
 
 - 
appendMapprotected void appendMap(StringBuilder sb) 
 - 
asJsonprotected void asJson(StringBuilder sb) 
 - 
asJavaUnquotedprotected void asJavaUnquoted(StringBuilder sb) 
 - 
asJavaprotected void asJava(StringBuilder sb) 
 - 
newInstancepublic M newInstance(Map<String,V> map) Constructs a new instance based on an existing Map.- Parameters:
- map- The Map.
- Returns:
- A new MapMessage
 
 - 
formatTopublic void formatTo(StringBuilder buffer) Description copied from interface:StringBuilderFormattableWrites a text representation of this object into the specifiedStringBuilder, ideally without allocating temporary objects.- Specified by:
- formatToin interface- StringBuilderFormattable
- Parameters:
- buffer- the StringBuilder to write into
 
 - 
formatTopublic void formatTo(String[] formats, StringBuilder buffer) Description copied from interface:MultiFormatStringBuilderFormattableWrites a text representation of this object into the specifiedStringBuilder, ideally without allocating temporary objects.- Specified by:
- formatToin interface- MultiFormatStringBuilderFormattable
- Parameters:
- 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 into
 
 - 
getThrowablepublic Throwable getThrowable() Always returns null.- Specified by:
- getThrowablein interface- Message
- Returns:
- null
 
 - 
validateprotected void validate(String key, boolean value) Default implementation does nothing.- Since:
- 2.9
 
 - 
validateprotected void validate(String key, byte value) Default implementation does nothing.- Since:
- 2.9
 
 - 
validateprotected void validate(String key, char value) Default implementation does nothing.- Since:
- 2.9
 
 - 
validateprotected void validate(String key, double value) Default implementation does nothing.- Since:
- 2.9
 
 - 
validateprotected void validate(String key, float value) Default implementation does nothing.- Since:
- 2.9
 
 - 
validateprotected void validate(String key, int value) Default implementation does nothing.- Since:
- 2.9
 
 - 
validateprotected void validate(String key, long value) Default implementation does nothing.- Since:
- 2.9
 
 - 
validateprotected void validate(String key, Object value) Default implementation does nothing.- Since:
- 2.9
 
 - 
validateprotected void validate(String key, short value) Default implementation does nothing.- Since:
- 2.9
 
 - 
validateprotected void validate(String key, String value) Default implementation does nothing.- Since:
- 2.9
 
 - 
toKeyprotected String toKey(String candidateKey) Allows subclasses to change a candidate key to an actual key.- Parameters:
- candidateKey- The candidate key.
- Returns:
- The candidate key.
- Since:
- 2.12
 
 - 
withpublic M with(String candidateKey, boolean value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
- Returns:
- this object
- Since:
- 2.9
 
 - 
withpublic M with(String candidateKey, byte value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
- Returns:
- this object
- Since:
- 2.9
 
 - 
withpublic M with(String candidateKey, char value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
- Returns:
- this object
- Since:
- 2.9
 
 - 
withpublic M with(String candidateKey, double value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
- Returns:
- this object
- Since:
- 2.9
 
 - 
withpublic M with(String candidateKey, float value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
- Returns:
- this object
- Since:
- 2.9
 
 - 
withpublic M with(String candidateKey, int value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
- Returns:
- this object
- Since:
- 2.9
 
 - 
withpublic M with(String candidateKey, long value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
- Returns:
- this object
- Since:
- 2.9
 
 - 
withpublic M with(String candidateKey, Object value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
- Returns:
- this object
- Since:
- 2.9
 
 - 
withpublic M with(String candidateKey, short value) Adds an item to the data Map.- Parameters:
- candidateKey- The name of the data item.
- value- The value of the data item.
- Returns:
- this object
- Since:
- 2.9
 
 
- 
 
-