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