Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.batch.item;
 
 import java.util.Map;
 import java.util.Set;
 
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. Note that putting null value is equivalent to removing the entry for the given key.

Author(s):
Lucas Ward
Douglas Kaminsky
 
 public class ExecutionContext implements Serializable {
 
 	private volatile boolean dirty = false;
 
 	private final Map<StringObjectmap;

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

Initializes a new execution context with the contents of another map.

Parameters:
map Initial contents of context.
 
 	public ExecutionContext(Map<StringObjectmap) {
 		this. = new ConcurrentHashMap<StringObject>(map);
 	}

Parameters:
executionContext
 
 	public ExecutionContext(ExecutionContext executionContext) {
 		this();
 		if (executionContext == null) {
 			return;
 		}
 		for (Entry<StringObjectentry : executionContext.entrySet()) {
 			this..put(entry.getKey(), entry.getValue());
 		}
 	}

Adds a String value to the context.

Parameters:
key Key to add to context
value Value to associate with key
 
 
 	public void putString(String keyString value) {
 
 		put(keyvalue);
 	}

Adds a Long value to the context.

Parameters:
key Key to add to context
value Value to associate with key
 
 	public void putLong(String keylong value) {
 
 		put(key, Long.valueOf(value));
 	}

Adds an Integer value to the context.

Parameters:
key Key to add to context
value Value to associate with key
	public void putInt(String keyint value) {
		put(key, Integer.valueOf(value));
	}

Add a Double value to the context.

Parameters:
key Key to add to context
value Value to associate with key
	public void putDouble(String keydouble value) {
		put(key, Double.valueOf(value));
	}

Add an Object value to the context (must be Serializable). Putting null value for a given key removes the key.

Parameters:
key Key to add to context
value Value to associate with key
	public void put(String keyObject value) {
		if (value != null) {
			Assert.isInstanceOf(Serializable.classvalue"Value: [ " + value + "must be serializable.");
			Object result = .put(keyvalue);
			 = result==null || result!=null && !result.equals(value);
		}
		else {
			Object result = .remove(key);
			 = result!=null;
		}
	}

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
	public boolean isDirty() {
		return ;
	}

Typesafe Getter for the String represented by the provided key.

Parameters:
key The key to get a value for
Returns:
The String value
	public String getString(String key) {
		return (StringreadAndValidate(keyString.class);
	}

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 repreesnted, specified default otherwise
	public String getString(String keyString defaultString) {
		if (!.containsKey(key)) {
			return defaultString;
		}
		return (StringreadAndValidate(keyString.class);
	}

Typesafe Getter for the Long represented by the provided key.

Parameters:
key The key to get a value for
Returns:
The Long value
	public long getLong(String key) {
		return ((LongreadAndValidate(keyLong.class)).longValue();
	}

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
	public long getLong(String keylong defaultLong) {
		if (!.containsKey(key)) {
			return defaultLong;
		}
		return ((LongreadAndValidate(keyLong.class)).longValue();
	}

Typesafe Getter for the Integer represented by the provided key.

Parameters:
key The key to get a value for
Returns:
The Integer value
	public int getInt(String key) {
		return ((IntegerreadAndValidate(keyInteger.class)).intValue();
	}

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
	public int getInt(String keyint defaultInt) {
		if (!.containsKey(key)) {
			return defaultInt;
		}
		return ((IntegerreadAndValidate(keyInteger.class)).intValue();
	}

Typesafe Getter for the Double represented by the provided key.

Parameters:
key The key to get a value for
Returns:
The Double value
	public double getDouble(String key) {
		return ((DoublereadAndValidate(keyDouble.class)).doubleValue();
	}

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
	public double getDouble(String keydouble defaultDouble) {
		if (!.containsKey(key)) {
			return defaultDouble;
		}
		return ((DoublereadAndValidate(keyDouble.class)).doubleValue();
	}

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
	public Object get(String key) {
		return .get(key);
	}

Utility method that attempts to take a value represented by a given key and validate it as a member of the specified type.

Parameters:
key The key to validate a value for
type Class against which value should be validated
Returns:
Value typed to the specified Class
	private Object readAndValidate(String keyClass<?> type) {
		Object value = .get(key);
		if (!type.isInstance(value)) {
			throw new ClassCastException("Value for key=[" + key + "] is not of type: [" + type + "], it is ["
					+ (value == null ? null : "(" + value.getClass() + ")" + value) + "]");
		}
		return value;
	}

Indicates whether or not the context is empty.

Returns:
True if the context has no entries, false otherwise.
See also:
java.util.Map.isEmpty()
	public boolean isEmpty() {
		return .isEmpty();
	}

Clears the dirty flag.
	public void clearDirtyFlag() {
		 = false;
	}

Returns the entry set containing the contents of this context.

Returns:
A set representing the contents of the context
See also:
java.util.Map.entrySet()
	public Set<Entry<StringObject>> entrySet() {
		return .entrySet();
	}

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:
java.util.Map.containsKey(java.lang.Object)
	public boolean containsKey(String key) {
		return .containsKey(key);
	}

Removes the mapping for a key from this context if it is present.

	public Object remove(String key) {
		return .remove(key);
	}

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:
java.util.Map.containsValue(java.lang.Object)
	public boolean containsValue(Object value) {
		return .containsValue(value);
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (obj instanceof ExecutionContext == false) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		return this.entrySet().equals(rhs.entrySet());
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return .hashCode();
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return .toString();
	}

Returns number of entries in the context

Returns:
Number of entries in the context
See also:
java.util.Map.size()
	public int size() {
		return .size();
	}
New to GrepCode? Check out our FAQ X