EMMA Coverage Report (generated Tue May 06 07:29:23 PDT 2008)
[all classes][org.springframework.batch.core.converter]

COVERAGE SUMMARY FOR SOURCE FILE [DefaultJobParametersConverter.java]

nameclass, %method, %block, %line, %
DefaultJobParametersConverter.java100% (1/1)100% (5/5)96%  (278/289)96%  (49.9/52)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DefaultJobParametersConverter100% (1/1)100% (5/5)96%  (278/289)96%  (49.9/52)
getJobParameters (Properties): JobParameters 100% (1/1)94%  (168/179)93%  (26.9/29)
DefaultJobParametersConverter (): void 100% (1/1)100% (15/15)100% (3/3)
getProperties (JobParameters): Properties 100% (1/1)100% (87/87)100% (16/16)
setDateFormat (DateFormat): void 100% (1/1)100% (4/4)100% (2/2)
setNumberFormat (NumberFormat): void 100% (1/1)100% (4/4)100% (2/2)

1/*
2 * Copyright 2006-2008 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 */
16package org.springframework.batch.core.converter;
17 
18import java.text.DateFormat;
19import java.text.DecimalFormat;
20import java.text.NumberFormat;
21import java.text.ParseException;
22import java.text.SimpleDateFormat;
23import java.util.Date;
24import java.util.Iterator;
25import java.util.Map;
26import java.util.Properties;
27import java.util.Map.Entry;
28 
29import org.springframework.batch.core.JobParameters;
30import org.springframework.batch.core.JobParametersBuilder;
31import org.springframework.util.StringUtils;
32 
33/**
34 * Converter for {@link JobParameters} instances using a simple naming convention for property keys. Key names ending
35 * with "(<type>)" where type is one of string, date, long are converted to the corresponding type. The default
36 * type is string. E.g.
37 * 
38 * <pre>
39 * schedule.date(date)=2007/12/11
40 * department.id(long)=2345
41 * </pre>
42 * 
43 * The literal values are converted to the correct type using the default Spring strategies, augmented if necessary by
44 * the custom editors provided.
45 * 
46 * @author Dave Syer
47 * 
48 */
49public class DefaultJobParametersConverter implements JobParametersConverter {
50 
51        public static final String DATE_TYPE = "(date)";
52 
53        public static final String STRING_TYPE = "(string)";
54 
55        public static final String LONG_TYPE = "(long)";
56 
57        private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
58 
59        private NumberFormat numberFormat = new DecimalFormat("#");
60 
61        /**
62         * Check for suffix on keys and use those to decide how to convert the value.
63         * 
64         * @throws IllegalArgumentException if a number or date is passed in that cannot be parsed, or cast to the correct
65         *             type.
66         * 
67         * @see org.springframework.batch.core.converter.JobParametersConverter#getJobParameters(java.util.Properties)
68         */
69        public JobParameters getJobParameters(Properties props) {
70 
71                if (props == null || props.isEmpty()) {
72                        return new JobParameters();
73                }
74 
75                JobParametersBuilder propertiesBuilder = new JobParametersBuilder();
76 
77                for (Iterator it = props.entrySet().iterator(); it.hasNext();) {
78                        Entry entry = (Entry) it.next();
79                        String key = (String) entry.getKey();
80                        String value = (String) entry.getValue();
81                        if (key.endsWith(DATE_TYPE)) {
82                                Date date;
83                                try {
84                                        date = dateFormat.parse(value);
85                                } catch (ParseException ex) {
86                                        String suffix = (dateFormat instanceof SimpleDateFormat) ? ", use "
87                                                + ((SimpleDateFormat) dateFormat).toPattern() : "";
88                                        throw new IllegalArgumentException("Date format is invalid: [" + value + "]" + suffix);
89                                }
90                                propertiesBuilder.addDate(StringUtils.replace(key, DATE_TYPE, ""), date);
91                        } else if (key.endsWith(LONG_TYPE)) {
92                                Long result;
93                                try {
94                                        result = (Long) numberFormat.parse(value);
95                                } catch (ParseException ex) {
96                                        String suffix = (numberFormat instanceof DecimalFormat) ? ", use "
97                                                + ((DecimalFormat) numberFormat).toPattern() : "";
98                                        throw new IllegalArgumentException("Number format is invalid: [" + value + "], use " + suffix);
99                                } catch (ClassCastException ex) {
100                                        throw new IllegalArgumentException("Number format is invalid: [" + value
101                                                + "], use a format with no decimal places");
102                                }
103                                propertiesBuilder.addLong(StringUtils.replace(key, LONG_TYPE, ""), result);
104                        } else if (StringUtils.endsWithIgnoreCase(key, STRING_TYPE)) {
105                                propertiesBuilder.addString(StringUtils.replace(key, STRING_TYPE, ""), value);
106                        } else {
107                                propertiesBuilder.addString(key, value.toString());
108                        }
109                }
110 
111                return propertiesBuilder.toJobParameters();
112        }
113 
114        /**
115         * Use the same suffixes to create properties (omitting the string suffix because it is the default).
116         * 
117         * @see org.springframework.batch.core.converter.JobParametersConverter#getProperties(org.springframework.batch.core.JobParameters)
118         */
119        public Properties getProperties(JobParameters params) {
120 
121                if (params == null || params.isEmpty()) {
122                        return new Properties();
123                }
124 
125                Map parameters = params.getParameters();
126                Properties result = new Properties();
127                for (Iterator iterator = parameters.entrySet().iterator(); iterator.hasNext();) {
128                        Entry entry = (Entry) iterator.next();
129                        String key = (String) entry.getKey();
130                        Object value = entry.getValue();
131                        if (value instanceof Date) {
132                                result.setProperty(key + DATE_TYPE, dateFormat.format(value));
133                        } else if (value instanceof Long) {
134                                result.setProperty(key + LONG_TYPE, numberFormat.format(value));
135                        } else {
136                                result.setProperty(key, "" + value);
137                        }
138                }
139                return result;
140        }
141 
142        /**
143         * Public setter for injecting a date format.
144         * 
145         * @param dateFormat a {@link DateFormat}, defaults to "yyyy/MM/dd"
146         */
147        public void setDateFormat(DateFormat dateFormat) {
148                this.dateFormat = dateFormat;
149        }
150 
151        /**
152         * Public setter for the {@link NumberFormat}. Used to parse longs, so must not contain decimal place (e.g. use "#"
153         * or "#,###").
154         * 
155         * @param numberFormat the {@link NumberFormat} to set
156         */
157        public void setNumberFormat(NumberFormat numberFormat) {
158                this.numberFormat = numberFormat;
159        }
160}

[all classes][org.springframework.batch.core.converter]
EMMA 2.0.5312 (C) Vladimir Roubtsov