| 1 | /* |
| 2 | * Copyright 2006-2007 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. |
| 25 | * |
| 26 | * @author Lucas Ward |
| 27 | * @since 2.0 |
| 28 | * |
| 29 | */ |
| 30 | public class JobParameter implements Serializable { |
| 31 | |
| 32 | private final Object parameter; |
| 33 | |
| 34 | private final ParameterType parameterType; |
| 35 | |
| 36 | /** |
| 37 | * Construct a new JobParameter as a String. |
| 38 | */ |
| 39 | public JobParameter(String parameter) { |
| 40 | this.parameter = parameter; |
| 41 | parameterType = ParameterType.STRING; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Construct a new JobParameter as a Long. |
| 46 | * |
| 47 | * @param parameter |
| 48 | */ |
| 49 | public JobParameter(Long parameter) { |
| 50 | this.parameter = parameter; |
| 51 | parameterType = ParameterType.LONG; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Construct a new JobParameter as a Date. |
| 56 | * |
| 57 | * @param parameter |
| 58 | */ |
| 59 | public JobParameter(Date parameter) { |
| 60 | this.parameter = new Date(parameter.getTime()); |
| 61 | parameterType = ParameterType.DATE; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Construct a new JobParameter as a Double. |
| 66 | * |
| 67 | * @param parameter |
| 68 | */ |
| 69 | public JobParameter(Double parameter) { |
| 70 | this.parameter = parameter; |
| 71 | parameterType = ParameterType.DOUBLE; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return the value contained within this JobParameter. |
| 76 | */ |
| 77 | public Object getValue() { |
| 78 | |
| 79 | if (parameter.getClass().isInstance(Date.class)) { |
| 80 | return new Date(((Date) parameter).getTime()); |
| 81 | } |
| 82 | else { |
| 83 | return parameter; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return a ParameterType representing the type of this parameter. |
| 89 | */ |
| 90 | public ParameterType getType() { |
| 91 | return parameterType; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public boolean equals(Object obj) { |
| 96 | if (obj instanceof JobParameter == false) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | if (this == obj) { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | JobParameter rhs = (JobParameter) obj; |
| 105 | return this.parameter.equals(rhs.parameter); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public String toString() { |
| 110 | return parameterType == ParameterType.DATE ? "" + ((Date) parameter).getTime() : parameter.toString(); |
| 111 | } |
| 112 | |
| 113 | public int hashCode() { |
| 114 | return 7 + 21 * parameter.hashCode(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Enumeration representing the type of a JobParameter. |
| 119 | */ |
| 120 | public enum ParameterType { |
| 121 | |
| 122 | STRING, DATE, LONG, DOUBLE; |
| 123 | } |
| 124 | } |