DefaultExceptionContext (Apache Commons Lang 3.20.0 API)
Description: declaration: package: org.apache.commons.lang3.exception, class: DefaultExceptionContext

# Class DefaultExceptionContext

java.lang.Object
org.apache.commons.lang3.exception.DefaultExceptionContext

All Implemented Interfaces:
  `Serializable, ExceptionContext`

public class DefaultExceptionContext extends Object implements ExceptionContext, Serializable
Default implementation of the context storing the label-value pairs for contexted exceptions.

This implementation is serializable, however this is dependent on the values that are added also being serializable.

Since:
  3.0
See Also:
  - `ContextedException`
  - `ContextedRuntimeException`
  - Serialized Form

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DefaultExceptionContext()`
  Constructs a new instance.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `DefaultExceptionContext`
  `addContextValue(String label, Object value)`
  Adds a contextual label-value pair into this context.
  `List<Pair<String,Object>>`
  `getContextEntries()`
  Gets the full list of label-value pairs defined in the contextual data.
  `Set<String>`
  `getContextLabels()`
  Gets the full set of labels defined in the contextual data.
  `List<Object>`
  `getContextValues(String label)`
  Gets all the contextual data values associated with the label.
  `Object`
  `getFirstContextValue(String label)`
  Gets the first available contextual data value associated with the label.
  `String`
  `getFormattedExceptionMessage(String baseMessage)`
  Builds the message containing the contextual information.
  `DefaultExceptionContext`
  `setContextValue(String label, Object value)`
  Sets a contextual label-value pair into this context.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### DefaultExceptionContext

    public DefaultExceptionContext()
    Constructs a new instance.

- ## Method Details

  - ### addContextValue

    public DefaultExceptionContext addContextValue(String label, Object value)
    Adds a contextual label-value pair into this context.

    The pair will be added to the context, independently of an already existing pair with the same label.

    Specified by:
      `addContextValue` in interface `ExceptionContext`
    Parameters:
      `label` - the label of the item to add, `null` not recommended
      `value` - the value of item to add, may be `null`
    Returns:
      `this`, for method chaining, not `null`

  - ### getContextEntries

    public List<Pair<String,Object>> getContextEntries()
    Gets the full list of label-value pairs defined in the contextual data.
    Specified by:
      `getContextEntries` in interface `ExceptionContext`
    Returns:
      the list of pairs, not `null`

  - ### getContextLabels

    public Set<String> getContextLabels()
    Gets the full set of labels defined in the contextual data.
    Specified by:
      `getContextLabels` in interface `ExceptionContext`
    Returns:
      the set of labels, not `null`

  - ### getContextValues

    public List<Object> getContextValues(String label)
    Gets all the contextual data values associated with the label.
    Specified by:
      `getContextValues` in interface `ExceptionContext`
    Parameters:
      `label` - the label to get the contextual values for, may be `null`
    Returns:
      the contextual values associated with the label, never `null`

  - ### getFirstContextValue

    public Object getFirstContextValue(String label)
    Gets the first available contextual data value associated with the label.
    Specified by:
      `getFirstContextValue` in interface `ExceptionContext`
    Parameters:
      `label` - the label to get the contextual value for, may be `null`
    Returns:
      the first contextual value associated with the label, may be `null`

  - ### getFormattedExceptionMessage

    public String getFormattedExceptionMessage(String baseMessage)
    Builds the message containing the contextual information.
    Specified by:
      `getFormattedExceptionMessage` in interface `ExceptionContext`
    Parameters:
      `baseMessage` - the base exception message without context information appended
    Returns:
      the exception message with context information appended, never null

  - ### setContextValue

    public DefaultExceptionContext setContextValue(String label, Object value)
    Sets a contextual label-value pair into this context.

    The pair will be added normally, but any existing label-value pair with the same label is removed from the context.

    Specified by:
      `setContextValue` in interface `ExceptionContext`
    Parameters:
      `label` - the label of the item to add, `null` not recommended
      `value` - the value of item to add, may be `null`
    Returns:
      `this`, for method chaining, not `null`