View Javadoc

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.util.Date;
20  import java.util.LinkedHashMap;
21  import java.util.Map;
22  
23  import org.springframework.util.Assert;
24  
25  /**
26   * Helper class for creating {@link JobParameters}. Useful because all
27   * {@link JobParameter} objects are immutable, and must be instantiated separately
28   * to ensure typesafety. Once created, it can be used in the
29   * same was a java.lang.StringBuilder (except, order is irrelevant), by adding
30   * various parameter types and creating a valid {@link JobParameters} once
31   * finished.
32   * 
33   * @author Lucas Ward
34   * @since 1.0
35   * @see JobParameters
36   * @see JobParameter
37   */
38  public class JobParametersBuilder {
39  
40  	private final Map<String, JobParameter> parameterMap;
41  
42  	/**
43  	 * Default constructor. Initializes the builder with empty parameters.
44  	 */
45  	public JobParametersBuilder() {
46  
47  		this.parameterMap = new LinkedHashMap<String, JobParameter>();
48  	}
49  
50  	/**
51  	 * Copy constructor. Initializes the builder with the supplied parameters.
52  	 */
53  	public JobParametersBuilder(JobParameters jobParameters) {
54  		this.parameterMap = new LinkedHashMap<String, JobParameter>(jobParameters.getParameters());
55  	}
56  
57  	/**
58  	 * Add a new String parameter for the given key.
59  	 * 
60  	 * @param key - parameter accessor.
61  	 * @param parameter - runtime parameter
62  	 * @return a refernece to this object.
63  	 */
64  	public JobParametersBuilder addString(String key, String parameter) {
65  		parameterMap.put(key, new JobParameter(parameter));
66  		return this;
67  	}
68  
69  	/**
70  	 * Add a new {@link Date} parameter for the given key.
71  	 * 
72  	 * @param key - parameter accessor.
73  	 * @param parameter - runtime parameter
74  	 * @return a refernece to this object.
75  	 */
76  	public JobParametersBuilder addDate(String key, Date parameter) {
77  		parameterMap.put(key, new JobParameter(parameter));
78  		return this;
79  	}
80  
81  	/**
82  	 * Add a new Long parameter for the given key.
83  	 * 
84  	 * @param key - parameter accessor.
85  	 * @param parameter - runtime parameter
86  	 * @return a reference to this object.
87  	 */
88  	public JobParametersBuilder addLong(String key, Long parameter) {
89  		parameterMap.put(key, new JobParameter(parameter));
90  		return this;
91  	}
92  	
93  	/**
94  	 * Add a new Double parameter for the given key.
95  	 * 
96  	 * @param key - parameter accessor.
97  	 * @param parameter - runtime parameter
98  	 * @return a reference to this object.
99  	 */
100 	public JobParametersBuilder addDouble(String key, Double parameter) {
101 		parameterMap.put(key, new JobParameter(parameter));
102 		return this;
103 	}
104 
105 	/**
106 	 * Conversion method that takes the current state of this builder and
107 	 * returns it as a JobruntimeParameters object.
108 	 * 
109 	 * @return a valid {@link JobParameters} object.
110 	 */
111 	public JobParameters toJobParameters() {
112 		return new JobParameters(parameterMap);
113 	}
114 
115 	/**
116 	 * Add a new {@link JobParameter} for the given key.
117 	 * 
118 	 * @param key - parameter accessor
119 	 * @param jobParameter - runtime parameter
120 	 * @return a reference to this object.
121 	 */
122 	public JobParametersBuilder addParameter(String key, JobParameter jobParameter) {
123 		Assert.notNull(jobParameter, "JobParameter must not be null");
124 		parameterMap.put(key, jobParameter);
125 		return this;
126 	}
127 }