001/* 002 * Copyright 2006-2013 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.batch.core; 018 019import java.io.Serializable; 020import java.util.Date; 021 022/** 023 * Domain representation of a parameter to a batch job. Only the following types 024 * can be parameters: String, Long, Date, and Double. The identifying flag is 025 * used to indicate if the parameter is to be used as part of the identification of 026 * a job instance. 027 * 028 * @author Lucas Ward 029 * @author Dave Syer 030 * @author Michael Minella 031 * @since 2.0 032 * 033 */ 034@SuppressWarnings("serial") 035public class JobParameter implements Serializable { 036 037 private final Object parameter; 038 039 private final ParameterType parameterType; 040 041 private final boolean identifying; 042 043 /** 044 * Construct a new JobParameter as a String. 045 * @param parameter {@link String} instance. 046 * @param identifying true if JobParameter should be identifying. 047 */ 048 public JobParameter(String parameter, boolean identifying) { 049 this.parameter = parameter; 050 parameterType = ParameterType.STRING; 051 this.identifying = identifying; 052 } 053 054 /** 055 * Construct a new JobParameter as a Long. 056 * 057 * @param parameter {@link Long} instance. 058 * @param identifying true if JobParameter should be identifying. 059 */ 060 public JobParameter(Long parameter, boolean identifying) { 061 this.parameter = parameter; 062 parameterType = ParameterType.LONG; 063 this.identifying = identifying; 064 } 065 066 /** 067 * Construct a new JobParameter as a Date. 068 * 069 * @param parameter {@link Date} instance. 070 * @param identifying true if JobParameter should be identifying. 071 */ 072 public JobParameter(Date parameter, boolean identifying) { 073 this.parameter = parameter; 074 parameterType = ParameterType.DATE; 075 this.identifying = identifying; 076 } 077 078 /** 079 * Construct a new JobParameter as a Double. 080 * 081 * @param parameter {@link Double} instance. 082 * @param identifying true if JobParameter should be identifying. 083 */ 084 public JobParameter(Double parameter, boolean identifying) { 085 this.parameter = parameter; 086 parameterType = ParameterType.DOUBLE; 087 this.identifying = identifying; 088 } 089 090 091 /** 092 * Construct a new JobParameter as a String. 093 * 094 * @param parameter {@link String} instance. 095 */ 096 public JobParameter(String parameter) { 097 this.parameter = parameter; 098 parameterType = ParameterType.STRING; 099 this.identifying = true; 100 } 101 102 /** 103 * Construct a new JobParameter as a Long. 104 * 105 * @param parameter {@link Long} instance. 106 */ 107 public JobParameter(Long parameter) { 108 this.parameter = parameter; 109 parameterType = ParameterType.LONG; 110 this.identifying = true; 111 } 112 113 /** 114 * Construct a new JobParameter as a Date. 115 * 116 * @param parameter {@link Date} instance. 117 */ 118 public JobParameter(Date parameter) { 119 this.parameter = parameter; 120 parameterType = ParameterType.DATE; 121 this.identifying = true; 122 } 123 124 /** 125 * Construct a new JobParameter as a Double. 126 * 127 * @param parameter {@link Double} instance. 128 */ 129 public JobParameter(Double parameter) { 130 this.parameter = parameter; 131 parameterType = ParameterType.DOUBLE; 132 this.identifying = true; 133 } 134 135 public boolean isIdentifying() { 136 return identifying; 137 } 138 139 /** 140 * @return the value contained within this JobParameter. 141 */ 142 public Object getValue() { 143 144 if (parameter != null && parameter.getClass().isInstance(Date.class)) { 145 return new Date(((Date) parameter).getTime()); 146 } 147 else { 148 return parameter; 149 } 150 } 151 152 /** 153 * @return a ParameterType representing the type of this parameter. 154 */ 155 public ParameterType getType() { 156 return parameterType; 157 } 158 159 @Override 160 public boolean equals(Object obj) { 161 if (obj instanceof JobParameter == false) { 162 return false; 163 } 164 165 if (this == obj) { 166 return true; 167 } 168 169 JobParameter rhs = (JobParameter) obj; 170 return parameter==null ? rhs.parameter==null && parameterType==rhs.parameterType: parameter.equals(rhs.parameter); 171 } 172 173 @Override 174 public String toString() { 175 return parameter == null ? null : (parameterType == ParameterType.DATE ? "" + ((Date) parameter).getTime() 176 : parameter.toString()); 177 } 178 179 @Override 180 public int hashCode() { 181 return 7 + 21 * (parameter == null ? parameterType.hashCode() : parameter.hashCode()); 182 } 183 184 /** 185 * Enumeration representing the type of a JobParameter. 186 */ 187 public enum ParameterType { 188 189 STRING, DATE, LONG, DOUBLE; 190 } 191}