Class MapMessage<M extends MapMessage<M,​V>,​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:
    java.io.Serializable, Message, MultiformatMessage, org.apache.logging.log4j.util.MultiFormatStringBuilderFormattable, org.apache.logging.log4j.util.StringBuilderFormattable
    Direct Known Subclasses:
    StringMapMessage, StructuredDataMessage

    @AsynchronouslyFormattable
    public class MapMessage<M extends MapMessage<M,​V>,​V>
    extends java.lang.Object
    implements org.apache.logging.log4j.util.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 StringMapMessage to allow for Objects as values.

    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  MapMessage.MapFormat
      When set as the format specifier causes the Map to be formatted as XML.
    • Constructor Summary

      Constructors 
      Constructor Description
      MapMessage()
      Constructs a new instance.
      MapMessage​(int initialCapacity)
      Constructs a new instance.
      MapMessage​(java.util.Map<java.lang.String,​V> map)
      Constructs a new instance based on an existing Map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String asString()
      Formats the Structured data as described in RFC 5424.
      java.lang.String asString​(java.lang.String format)
      Formats the Structured data as described in RFC 5424.
      void asXml​(java.lang.StringBuilder sb)
      Formats this message as an XML fragment String into the given builder.
      void clear()
      Clear the data.
      boolean containsKey​(java.lang.String key)
      Returns true if this data structure contains the specified key, false otherwise.
      boolean equals​(java.lang.Object o)  
      <CV> void forEach​(org.apache.logging.log4j.util.BiConsumer<java.lang.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​(org.apache.logging.log4j.util.TriConsumer<java.lang.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​(java.lang.String[] formats, java.lang.StringBuilder buffer)  
      void formatTo​(java.lang.StringBuilder buffer)  
      java.lang.String get​(java.lang.String key)
      Retrieves the value of the element with the specified key or null if the key is not present.
      java.util.Map<java.lang.String,​V> getData()
      Returns the message data as an unmodifiable Map.
      java.lang.String getFormat()
      Returns the message.
      java.lang.String[] getFormats()
      Returns the supported formats.
      java.lang.String getFormattedMessage()
      Formats the message and return it.
      java.lang.String getFormattedMessage​(java.lang.String[] formats)
      Returns the Message formatted as a String.
      org.apache.logging.log4j.util.IndexedReadOnlyStringMap getIndexedReadOnlyStringMap()
      Returns a read-only view of the message data.
      java.lang.Object[] getParameters()
      Returns the data elements as if they were parameters on the logging event.
      java.lang.Throwable getThrowable()
      Always returns null.
      int hashCode()  
      M newInstance​(java.util.Map<java.lang.String,​V> map)
      Constructs a new instance based on an existing Map.
      void put​(java.lang.String candidateKey, java.lang.String value)
      Adds an item to the data Map.
      void putAll​(java.util.Map<java.lang.String,​java.lang.String> map)
      Adds all the elements from the specified Map.
      java.lang.String remove​(java.lang.String key)
      Removes the element with the specified name.
      java.lang.String toString()  
      M with​(java.lang.String candidateKey, boolean value)
      Adds an item to the data Map.
      M with​(java.lang.String candidateKey, byte value)
      Adds an item to the data Map.
      M with​(java.lang.String candidateKey, char value)
      Adds an item to the data Map.
      M with​(java.lang.String candidateKey, double value)
      Adds an item to the data Map.
      M with​(java.lang.String candidateKey, float value)
      Adds an item to the data Map.
      M with​(java.lang.String candidateKey, int value)
      Adds an item to the data Map.
      M with​(java.lang.String candidateKey, long value)
      Adds an item to the data Map.
      M with​(java.lang.String candidateKey, short value)
      Adds an item to the data Map.
      M with​(java.lang.String candidateKey, java.lang.Object value)
      Adds an item to the data Map.
      M with​(java.lang.String candidateKey, java.lang.String value)
      Adds an item to the data Map in fluent style.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • MapMessage

        public MapMessage()
        Constructs a new instance.
      • MapMessage

        public MapMessage​(int initialCapacity)
        Constructs a new instance.
        Parameters:
        initialCapacity - the initial capacity.
      • MapMessage

        public MapMessage​(java.util.Map<java.lang.String,​V> map)
        Constructs a new instance based on an existing Map.
        Parameters:
        map - The Map.
    • Method Detail

      • getFormats

        public java.lang.String[] getFormats()
        Description copied from interface: MultiformatMessage
        Returns the supported formats.
        Specified by:
        getFormats in interface MultiformatMessage
        Returns:
        The supported formats.
      • getParameters

        public java.lang.Object[] getParameters()
        Returns the data elements as if they were parameters on the logging event.
        Specified by:
        getParameters in interface Message
        Returns:
        the data elements.
      • getFormat

        public java.lang.String getFormat()
        Returns the message.
        Specified by:
        getFormat in interface Message
        Returns:
        the message.
      • getData

        public java.util.Map<java.lang.String,​V> getData()
        Returns the message data as an unmodifiable Map.
        Returns:
        the message data as an unmodifiable map.
      • getIndexedReadOnlyStringMap

        public org.apache.logging.log4j.util.IndexedReadOnlyStringMap getIndexedReadOnlyStringMap()
        Returns a read-only view of the message data.
        Returns:
        the read-only message data.
      • clear

        public void clear()
        Clear the data.
      • containsKey

        public boolean containsKey​(java.lang.String key)
        Returns true if this data structure contains the specified key, false otherwise.
        Parameters:
        key - the key whose presence to check. May be null.
        Returns:
        true if this data structure contains the specified key, false otherwise
        Since:
        2.9
      • put

        public void put​(java.lang.String candidateKey,
                        java.lang.String value)
        Adds an item to the data Map.
        Parameters:
        candidateKey - The name of the data item.
        value - The value of the data item.
      • putAll

        public void putAll​(java.util.Map<java.lang.String,​java.lang.String> map)
        Adds all the elements from the specified Map.
        Parameters:
        map - The Map to add.
      • get

        public java.lang.String get​(java.lang.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.
      • remove

        public java.lang.String remove​(java.lang.String key)
        Removes the element with the specified name.
        Parameters:
        key - The name of the element.
        Returns:
        The previous value of the element.
      • asString

        public java.lang.String asString()
        Formats the Structured data as described in RFC 5424.
        Returns:
        The formatted String.
      • asString

        public java.lang.String asString​(java.lang.String format)
        Formats the Structured data as described in RFC 5424.
        Parameters:
        format - The format identifier.
        Returns:
        The formatted String.
      • forEach

        public <CV> void forEach​(org.apache.logging.log4j.util.BiConsumer<java.lang.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's BiConsumer.accept(Object, Object) accept} method may cause a ConcurrentModificationException to 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:
        java.util.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)
      • forEach

        public <CV,​S> void forEach​(org.apache.logging.log4j.util.TriConsumer<java.lang.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's accept method may cause a ConcurrentModificationException to 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:
        java.util.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)
      • asXml

        public void asXml​(java.lang.StringBuilder sb)
        Formats this message as an XML fragment String into the given builder.
        Parameters:
        sb - format into this builder.
      • getFormattedMessage

        public java.lang.String getFormattedMessage()
        Formats the message and return it.
        Specified by:
        getFormattedMessage in interface Message
        Returns:
        the formatted message.
      • getFormattedMessage

        public java.lang.String getFormattedMessage​(java.lang.String[] formats)
        Description copied from interface: MultiformatMessage
        Returns the Message formatted as a String.
        Specified by:
        getFormattedMessage in 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.
      • newInstance

        public M newInstance​(java.util.Map<java.lang.String,​V> map)
        Constructs a new instance based on an existing Map.
        Parameters:
        map - The Map.
        Returns:
        A new MapMessage
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • formatTo

        public void formatTo​(java.lang.StringBuilder buffer)
        Specified by:
        formatTo in interface org.apache.logging.log4j.util.StringBuilderFormattable
      • formatTo

        public void formatTo​(java.lang.String[] formats,
                             java.lang.StringBuilder buffer)
        Specified by:
        formatTo in interface org.apache.logging.log4j.util.MultiFormatStringBuilderFormattable
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • getThrowable

        public java.lang.Throwable getThrowable()
        Always returns null.
        Specified by:
        getThrowable in interface Message
        Returns:
        null
      • with

        public M with​(java.lang.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
      • with

        public M with​(java.lang.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
      • with

        public M with​(java.lang.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
      • with

        public M with​(java.lang.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
      • with

        public M with​(java.lang.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
      • with

        public M with​(java.lang.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
      • with

        public M with​(java.lang.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
      • with

        public M with​(java.lang.String candidateKey,
                      java.lang.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
      • with

        public M with​(java.lang.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
      • with

        public M with​(java.lang.String candidateKey,
                      java.lang.String value)
        Adds an item to the data Map in fluent style.
        Parameters:
        candidateKey - The name of the data item.
        value - The value of the data item.
        Returns:
        this