View Javadoc

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