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 | Assert.notNull(parameter, "Parameter must not be null."); |
66 | parameterMap.put(key, new JobParameter(parameter)); |
67 | return this; |
68 | } |
69 | |
70 | /** |
71 | * Add a new {@link Date} parameter for the given key. |
72 | * |
73 | * @param key - parameter accessor. |
74 | * @param parameter - runtime parameter |
75 | * @return a refernece to this object. |
76 | */ |
77 | public JobParametersBuilder addDate(String key, Date parameter) { |
78 | Assert.notNull(parameter, "Parameter must not be null."); |
79 | parameterMap.put(key, new JobParameter(parameter)); |
80 | return this; |
81 | } |
82 | |
83 | /** |
84 | * Add a new Long parameter for the given key. |
85 | * |
86 | * @param key - parameter accessor. |
87 | * @param parameter - runtime parameter |
88 | * @return a reference to this object. |
89 | */ |
90 | public JobParametersBuilder addLong(String key, Long parameter) { |
91 | Assert.notNull(parameter, "Parameter must not be null."); |
92 | parameterMap.put(key, new JobParameter(parameter)); |
93 | return this; |
94 | } |
95 | |
96 | /** |
97 | * Add a new Double parameter for the given key. |
98 | * |
99 | * @param key - parameter accessor. |
100 | * @param parameter - runtime parameter |
101 | * @return a reference to this object. |
102 | */ |
103 | public JobParametersBuilder addDouble(String key, Double parameter) { |
104 | Assert.notNull(parameter, "Parameter must not be null."); |
105 | parameterMap.put(key, new JobParameter(parameter)); |
106 | return this; |
107 | } |
108 | |
109 | /** |
110 | * Conversion method that takes the current state of this builder and |
111 | * returns it as a JobruntimeParameters object. |
112 | * |
113 | * @return a valid JobParameters object. |
114 | */ |
115 | public JobParameters toJobParameters() { |
116 | return new JobParameters(parameterMap); |
117 | } |
118 | } |