ExecutionContext详解
ExecutionContext详解
ExecutionContext源代码详解
ExecutionContext中主要有一个Map<String, Object>,用于存储Spring Batch执行过程中的参数等。
package org.springframework.batch.item;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.util.Assert;
public class ExecutionContext implements Serializable {
private volatile boolean dirty;
private final Map<String, Object> map;
public ExecutionContext() {
this.dirty = false;
this.map = new ConcurrentHashMap();
}
public ExecutionContext(Map<String, Object> map) {
this.dirty = false;
this.map = new ConcurrentHashMap(map);
}
public ExecutionContext(ExecutionContext executionContext) {
this();
if (executionContext != null) {
Iterator i$ = executionContext.entrySet().iterator();
while(i$.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry)i$.next();
this.map.put(entry.getKey(), entry.getValue());
}
}
}
public void putString(String key, String value) {
this.put(key, value);
}
public void putLong(String key, long value) {
this.put(key, value);
}
public void putInt(String key, int value) {
this.put(key, value);
}
public void putDouble(String key, double value) {
this.put(key, value);
}
public void put(String key, Object value) {
Object result;
if (value != null) {
Assert.isInstanceOf(Serializable.class, value, "Value: [ " + value + "must be serializable.");
result = this.map.put(key, value);
this.dirty = result == null || result != null && !result.equals(value);
} else {
result = this.map.remove(key);
this.dirty = result != null;
}
}
public boolean isDirty() {
return this.dirty;
}
public String getString(String key) {
return (String)this.readAndValidate(key, String.class);
}
public String getString(String key, String defaultString) {
return !this.map.containsKey(key) ? defaultString : (String)this.readAndValidate(key, String.class);
}
public long getLong(String key) {
return (Long)this.readAndValidate(key, Long.class);
}
public long getLong(String key, long defaultLong) {
return !this.map.containsKey(key) ? defaultLong : (Long)this.readAndValidate(key, Long.class);
}
public int getInt(String key) {
return (Integer)this.readAndValidate(key, Integer.class);
}
public int getInt(String key, int defaultInt) {
return !this.map.containsKey(key) ? defaultInt : (Integer)this.readAndValidate(key, Integer.class);
}
public double getDouble(String key) {
return (Double)this.readAndValidate(key, Double.class);
}
public double getDouble(String key, double defaultDouble) {
return !this.map.containsKey(key) ? defaultDouble : (Double)this.readAndValidate(key, Double.class);
}
public Object get(String key) {
return this.map.get(key);
}
private Object readAndValidate(String key, Class<?> type) {
Object value = this.map.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) + "]");
} else {
return value;
}
}
public boolean isEmpty() {
return this.map.isEmpty();
}
public void clearDirtyFlag() {
this.dirty = false;
}
public Set<Map.Entry<String, Object>> entrySet() {
return this.map.entrySet();
}
public boolean containsKey(String key) {
return this.map.containsKey(key);
}
public Object remove(String key) {
return this.map.remove(key);
}
public boolean containsValue(Object value) {
return this.map.containsValue(value);
}
public boolean equals(Object obj) {
if (!(obj instanceof ExecutionContext)) {
return false;
} else if (this == obj) {
return true;
} else {
ExecutionContext rhs = (ExecutionContext)obj;
return this.entrySet().equals(rhs.entrySet());
}
}
public int hashCode() {
return this.map.hashCode();
}
public String toString() {
return this.map.toString();
}
public int size() {
return this.map.size();
}
}