| 1 | /* |
| 2 | * Copyright 2006-2013 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.springframework.batch.core; |
| 18 | |
| 19 | import java.io.Serializable; |
| 20 | import java.util.Date; |
| 21 | |
| 22 | /** |
| 23 | * Domain representation of a parameter to a batch job. Only the following types |
| 24 | * can be parameters: String, Long, Date, and Double. The identifying flag is |
| 25 | * used to indicate if the parameter is to be used as part of the identification of |
| 26 | * a job instance. |
| 27 | * |
| 28 | * @author Lucas Ward |
| 29 | * @author Dave Syer |
| 30 | * @author Michael Minella |
| 31 | * @since 2.0 |
| 32 | * |
| 33 | */ |
| 34 | @SuppressWarnings("serial") |
| 35 | public class JobParameter implements Serializable { |
| 36 | |
| 37 | private final Object parameter; |
| 38 | |
| 39 | private final ParameterType parameterType; |
| 40 | |
| 41 | private final boolean identifying; |
| 42 | |
| 43 | /** |
| 44 | * Construct a new JobParameter as a String. |
| 45 | */ |
| 46 | public JobParameter(String parameter, boolean identifying) { |
| 47 | this.parameter = parameter; |
| 48 | parameterType = ParameterType.STRING; |
| 49 | this.identifying = identifying; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Construct a new JobParameter as a Long. |
| 54 | * |
| 55 | * @param parameter |
| 56 | */ |
| 57 | public JobParameter(Long parameter, boolean identifying) { |
| 58 | this.parameter = parameter; |
| 59 | parameterType = ParameterType.LONG; |
| 60 | this.identifying = identifying; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Construct a new JobParameter as a Date. |
| 65 | * |
| 66 | * @param parameter |
| 67 | */ |
| 68 | public JobParameter(Date parameter, boolean identifying) { |
| 69 | this.parameter = parameter; |
| 70 | parameterType = ParameterType.DATE; |
| 71 | this.identifying = identifying; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Construct a new JobParameter as a Double. |
| 76 | * |
| 77 | * @param parameter |
| 78 | */ |
| 79 | public JobParameter(Double parameter, boolean identifying) { |
| 80 | this.parameter = parameter; |
| 81 | parameterType = ParameterType.DOUBLE; |
| 82 | this.identifying = identifying; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Construct a new JobParameter as a String. |
| 88 | */ |
| 89 | public JobParameter(String parameter) { |
| 90 | this.parameter = parameter; |
| 91 | parameterType = ParameterType.STRING; |
| 92 | this.identifying = true; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Construct a new JobParameter as a Long. |
| 97 | * |
| 98 | * @param parameter |
| 99 | */ |
| 100 | public JobParameter(Long parameter) { |
| 101 | this.parameter = parameter; |
| 102 | parameterType = ParameterType.LONG; |
| 103 | this.identifying = true; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Construct a new JobParameter as a Date. |
| 108 | * |
| 109 | * @param parameter |
| 110 | */ |
| 111 | public JobParameter(Date parameter) { |
| 112 | this.parameter = parameter; |
| 113 | parameterType = ParameterType.DATE; |
| 114 | this.identifying = true; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Construct a new JobParameter as a Double. |
| 119 | * |
| 120 | * @param parameter |
| 121 | */ |
| 122 | public JobParameter(Double parameter) { |
| 123 | this.parameter = parameter; |
| 124 | parameterType = ParameterType.DOUBLE; |
| 125 | this.identifying = true; |
| 126 | } |
| 127 | |
| 128 | public boolean isIdentifying() { |
| 129 | return identifying; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @return the value contained within this JobParameter. |
| 134 | */ |
| 135 | public Object getValue() { |
| 136 | |
| 137 | if (parameter != null && parameter.getClass().isInstance(Date.class)) { |
| 138 | return new Date(((Date) parameter).getTime()); |
| 139 | } |
| 140 | else { |
| 141 | return parameter; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @return a ParameterType representing the type of this parameter. |
| 147 | */ |
| 148 | public ParameterType getType() { |
| 149 | return parameterType; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public boolean equals(Object obj) { |
| 154 | if (obj instanceof JobParameter == false) { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | if (this == obj) { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | JobParameter rhs = (JobParameter) obj; |
| 163 | return parameter==null ? rhs.parameter==null && parameterType==rhs.parameterType: parameter.equals(rhs.parameter); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public String toString() { |
| 168 | return parameter == null ? null : (parameterType == ParameterType.DATE ? "" + ((Date) parameter).getTime() |
| 169 | : parameter.toString()); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public int hashCode() { |
| 174 | return 7 + 21 * (parameter == null ? parameterType.hashCode() : parameter.hashCode()); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Enumeration representing the type of a JobParameter. |
| 179 | */ |
| 180 | public enum ParameterType { |
| 181 | |
| 182 | STRING, DATE, LONG, DOUBLE; |
| 183 | } |
| 184 | } |