跳至主要內容

Job Parameters


Job Parameters

JobParameters支持的参数类型

Job Parameters共支持四种类型的参数,数据类型分别是String、Date、Long、Double。

JobParameters的源代码

JobParameters的属性为Map<String, JobParameter>。

package org.springframework.batch.core;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

public class JobParameters implements Serializable {
    private final Map<String, JobParameter> parameters;

    public JobParameters() {
        this.parameters = new LinkedHashMap();
    }

    public JobParameters(Map<String, JobParameter> parameters) {
        this.parameters = new LinkedHashMap(parameters);
    }

    public Long getLong(String key) {
        if (!this.parameters.containsKey(key)) {
            return 0L;
        } else {
            Object value = ((JobParameter)this.parameters.get(key)).getValue();
            return value == null ? 0L : (Long)value;
        }
    }

    public Long getLong(String key, long defaultValue) {
        return this.parameters.containsKey(key) ? this.getLong(key) : defaultValue;
    }

    public String getString(String key) {
        JobParameter value = (JobParameter)this.parameters.get(key);
        return value == null ? null : value.toString();
    }

    public String getString(String key, String defaultValue) {
        return this.parameters.containsKey(key) ? this.getString(key) : defaultValue;
    }

    public Double getDouble(String key) {
        if (!this.parameters.containsKey(key)) {
            return 0.0;
        } else {
            Double value = (Double)((JobParameter)this.parameters.get(key)).getValue();
            return value == null ? 0.0 : value;
        }
    }

    public Double getDouble(String key, double defaultValue) {
        return this.parameters.containsKey(key) ? this.getDouble(key) : defaultValue;
    }

    public Date getDate(String key) {
        return this.getDate(key, (Date)null);
    }

    public Date getDate(String key, Date defaultValue) {
        return this.parameters.containsKey(key) ? (Date)((JobParameter)this.parameters.get(key)).getValue() : defaultValue;
    }

    public Map<String, JobParameter> getParameters() {
        return new LinkedHashMap(this.parameters);
    }

    public boolean isEmpty() {
        return this.parameters.isEmpty();
    }

    public boolean equals(Object obj) {
        if (!(obj instanceof JobParameters)) {
            return false;
        } else if (obj == this) {
            return true;
        } else {
            JobParameters rhs = (JobParameters)obj;
            return this.parameters.equals(rhs.parameters);
        }
    }

    public int hashCode() {
        return 17 + 23 * this.parameters.hashCode();
    }

    public String toString() {
        return this.parameters.toString();
    }
}

JobParameter源代码详解

JobParameter中有ParameterType,ParameterType是一个enum,有STRING, DATE, LONG, DOUBLE;

package org.springframework.batch.core;

import java.io.Serializable;
import java.util.Date;

public class JobParameter implements Serializable {
    private final Object parameter;
    private final ParameterType parameterType;
    private final boolean identifying;

    public JobParameter(String parameter, boolean identifying) {
        this.parameter = parameter;
        this.parameterType = JobParameter.ParameterType.STRING;
        this.identifying = identifying;
    }

    public JobParameter(Long parameter, boolean identifying) {
        this.parameter = parameter;
        this.parameterType = JobParameter.ParameterType.LONG;
        this.identifying = identifying;
    }

    public JobParameter(Date parameter, boolean identifying) {
        this.parameter = parameter;
        this.parameterType = JobParameter.ParameterType.DATE;
        this.identifying = identifying;
    }

    public JobParameter(Double parameter, boolean identifying) {
        this.parameter = parameter;
        this.parameterType = JobParameter.ParameterType.DOUBLE;
        this.identifying = identifying;
    }

    public JobParameter(String parameter) {
        this.parameter = parameter;
        this.parameterType = JobParameter.ParameterType.STRING;
        this.identifying = true;
    }

    public JobParameter(Long parameter) {
        this.parameter = parameter;
        this.parameterType = JobParameter.ParameterType.LONG;
        this.identifying = true;
    }

    public JobParameter(Date parameter) {
        this.parameter = parameter;
        this.parameterType = JobParameter.ParameterType.DATE;
        this.identifying = true;
    }

    public JobParameter(Double parameter) {
        this.parameter = parameter;
        this.parameterType = JobParameter.ParameterType.DOUBLE;
        this.identifying = true;
    }

    public boolean isIdentifying() {
        return this.identifying;
    }

    public Object getValue() {
        return this.parameter != null && this.parameter.getClass().isInstance(Date.class) ? new Date(((Date)this.parameter).getTime()) : this.parameter;
    }

    public ParameterType getType() {
        return this.parameterType;
    }

    public boolean equals(Object obj) {
        if (!(obj instanceof JobParameter)) {
            return false;
        } else if (this == obj) {
            return true;
        } else {
            JobParameter rhs = (JobParameter)obj;
            return this.parameter == null ? rhs.parameter == null && this.parameterType == rhs.parameterType : this.parameter.equals(rhs.parameter);
        }
    }

    public String toString() {
        return this.parameter == null ? null : (this.parameterType == JobParameter.ParameterType.DATE ? "" + ((Date)this.parameter).getTime() : this.parameter.toString());
    }

    public int hashCode() {
        return 7 + 21 * (this.parameter == null ? this.parameterType.hashCode() : this.parameter.hashCode());
    }

    public static enum ParameterType {
        STRING,
        DATE,
        LONG,
        DOUBLE;

        private ParameterType() {
        }
    }
}
上次编辑于:
贡献者: Neil