Class ExecutionContext

java.lang.Object
org.springframework.batch.item.ExecutionContext
All Implemented Interfaces:
Serializable

public class ExecutionContext extends Object implements Serializable
Object representing a context for an ItemStream. It is a thin wrapper for a map that allows optionally for type safety on reads. It also allows for dirty checking by setting a 'dirty' flag whenever any put is called.

Non-transient entries should be serializable, otherwise a custom serializer should be used. Note that putting null value is equivalent to removing the entry for the given key.

Author:
Lucas Ward, Douglas Kaminsky, Mahmoud Ben Hassine
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    Initializes a new execution context with the contents of another map.
    Initializes a new ExecutionContext with the contents of another ExecutionContext.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the dirty flag.
    boolean
    Indicates whether or not a key is represented in this context.
    boolean
    Indicates whether or not a value is represented in this context.
    Returns the entry set containing the contents of this context.
    boolean
     
    get(String key)
    Getter for the value represented by the provided key.
    <V> V
    get(String key, Class<V> type)
    Typesafe getter for the value represented by the provided key, with cast to given class.
    <V> V
    get(String key, Class<V> type, V defaultValue)
    Typesafe getter for the value represented by the provided key, with cast to given class.
    double
    Typesafe Getter for the Double represented by the provided key.
    double
    getDouble(String key, double defaultDouble)
    Typesafe Getter for the Double represented by the provided key with default value to return if key is not represented.
    int
    Typesafe Getter for the Integer represented by the provided key.
    int
    getInt(String key, int defaultInt)
    Typesafe Getter for the Integer represented by the provided key with default value to return if key is not represented.
    long
    Typesafe Getter for the Long represented by the provided key.
    long
    getLong(String key, long defaultLong)
    Typesafe Getter for the Long represented by the provided key with default value to return if key is not represented.
    Typesafe Getter for the String represented by the provided key.
    getString(String key, String defaultString)
    Typesafe Getter for the String represented by the provided key with default value to return if key is not represented.
    int
     
    boolean
    Indicates if context has been changed with a "put" operation since the dirty flag was last cleared.
    boolean
    Indicates whether or not the context is empty.
    void
    put(String key, Object value)
    Add an Object value to the context.
    void
    putDouble(String key, double value)
    Add a Double value to the context.
    void
    putInt(String key, int value)
    Adds an Integer value to the context.
    void
    putLong(String key, long value)
    Adds a Long value to the context.
    void
    putString(String key, String value)
    Adds a String value to the context.
    Removes the mapping for a key from this context if it is present.
    int
    Returns number of entries in the context
    Returns the internal map as read-only.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ExecutionContext

      public ExecutionContext()
      Default constructor. Initializes a new execution context with an empty internal map.
    • ExecutionContext

      public ExecutionContext(Map<String,Object> map)
      Initializes a new execution context with the contents of another map.
      Parameters:
      map - Initial contents of context.
    • ExecutionContext

      public ExecutionContext(ExecutionContext executionContext)
      Initializes a new ExecutionContext with the contents of another ExecutionContext.
      Parameters:
      executionContext - containing the entries to be copied to this current context.
  • Method Details

    • putString

      public void putString(String key, @Nullable String value)
      Adds a String value to the context. Putting null value for a given key removes the key.
      Parameters:
      key - Key to add to context
      value - Value to associate with key
    • putLong

      public void putLong(String key, long value)
      Adds a Long value to the context.
      Parameters:
      key - Key to add to context
      value - Value to associate with key
    • putInt

      public void putInt(String key, int value)
      Adds an Integer value to the context.
      Parameters:
      key - Key to add to context
      value - Value to associate with key
    • putDouble

      public void putDouble(String key, double value)
      Add a Double value to the context.
      Parameters:
      key - Key to add to context
      value - Value to associate with key
    • put

      public void put(String key, @Nullable Object value)
      Add an Object value to the context. Putting null value for a given key removes the key.
      Parameters:
      key - Key to add to context
      value - Value to associate with key
    • isDirty

      public boolean isDirty()
      Indicates if context has been changed with a "put" operation since the dirty flag was last cleared. Note that the last time the flag was cleared might correspond to creation of the context.
      Returns:
      True if "put" operation has occurred since flag was last cleared
    • getString

      public String getString(String key)
      Typesafe Getter for the String represented by the provided key.
      Parameters:
      key - The key to get a value for
      Returns:
      The String value
    • getString

      public String getString(String key, String defaultString)
      Typesafe Getter for the String represented by the provided key with default value to return if key is not represented.
      Parameters:
      key - The key to get a value for
      defaultString - Default to return if key is not represented
      Returns:
      The String value if key is represented, specified default otherwise
    • getLong

      public long getLong(String key)
      Typesafe Getter for the Long represented by the provided key.
      Parameters:
      key - The key to get a value for
      Returns:
      The Long value
    • getLong

      public long getLong(String key, long defaultLong)
      Typesafe Getter for the Long represented by the provided key with default value to return if key is not represented.
      Parameters:
      key - The key to get a value for
      defaultLong - Default to return if key is not represented
      Returns:
      The long value if key is represented, specified default otherwise
    • getInt

      public int getInt(String key)
      Typesafe Getter for the Integer represented by the provided key.
      Parameters:
      key - The key to get a value for
      Returns:
      The Integer value
    • getInt

      public int getInt(String key, int defaultInt)
      Typesafe Getter for the Integer represented by the provided key with default value to return if key is not represented.
      Parameters:
      key - The key to get a value for
      defaultInt - Default to return if key is not represented
      Returns:
      The int value if key is represented, specified default otherwise
    • getDouble

      public double getDouble(String key)
      Typesafe Getter for the Double represented by the provided key.
      Parameters:
      key - The key to get a value for
      Returns:
      The Double value
    • getDouble

      public double getDouble(String key, double defaultDouble)
      Typesafe Getter for the Double represented by the provided key with default value to return if key is not represented.
      Parameters:
      key - The key to get a value for
      defaultDouble - Default to return if key is not represented
      Returns:
      The double value if key is represented, specified default otherwise
    • get

      @Nullable public Object get(String key)
      Getter for the value represented by the provided key.
      Parameters:
      key - The key to get a value for
      Returns:
      The value represented by the given key or null if the key is not present
    • get

      @Nullable public <V> V get(String key, Class<V> type)
      Typesafe getter for the value represented by the provided key, with cast to given class.
      Type Parameters:
      V - Type of returned value
      Parameters:
      key - The key to get a value for
      type - The class of return type
      Returns:
      The value of given type represented by the given key or null if the key is not present
      Since:
      5.1
    • get

      @Nullable public <V> V get(String key, Class<V> type, @Nullable V defaultValue)
      Typesafe getter for the value represented by the provided key, with cast to given class.
      Type Parameters:
      V - Type of returned value
      Parameters:
      key - The key to get a value for
      type - The class of return type
      defaultValue - Default value in case element is not present
      Returns:
      The value of given type represented by the given key or the default value if the key is not present
      Since:
      5.1
    • isEmpty

      public boolean isEmpty()
      Indicates whether or not the context is empty.
      Returns:
      True if the context has no entries, false otherwise.
      See Also:
    • clearDirtyFlag

      public void clearDirtyFlag()
      Clears the dirty flag.
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      Returns the entry set containing the contents of this context.
      Returns:
      An unmodifiable set representing the contents of the context
      See Also:
    • toMap

      public Map<String,Object> toMap()
      Returns the internal map as read-only.
      Returns:
      An unmodifiable map containing all contents.
      Since:
      5.1
      See Also:
    • containsKey

      public boolean containsKey(String key)
      Indicates whether or not a key is represented in this context.
      Parameters:
      key - Key to check existence for
      Returns:
      True if key is represented in context, false otherwise
      See Also:
    • remove

      @Nullable public Object remove(String key)
      Removes the mapping for a key from this context if it is present.
      Parameters:
      key - String that identifies the entry to be removed from the context.
      Returns:
      the value that was removed from the context.
      See Also:
    • containsValue

      public boolean containsValue(Object value)
      Indicates whether or not a value is represented in this context.
      Parameters:
      value - Value to check existence for
      Returns:
      True if value is represented in context, false otherwise
      See Also:
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • size

      public int size()
      Returns number of entries in the context
      Returns:
      Number of entries in the context
      See Also: