1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv.bean;
18
19 import java.io.IOException;
20 import java.lang.reflect.Field;
21 import java.text.Format;
22 import java.text.ParseException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28
29
30
31
32
33
34
35
36 public abstract class AbstractCsvBeanTemplate<T, O extends AbstractCsvBeanTemplate<T, O>> {
37
38
39
40
41 private Class<T> type;
42
43
44
45
46 private Map<String, Format> valueParserMapping = new HashMap<String, Format>();
47
48
49
50
51 private Map<Object, Format> valueFormatterMapping = new HashMap<Object, Format>();
52
53
54
55
56 private CsvValueConverter valueConverter = new SimpleCsvValueConverter();
57
58
59
60
61
62
63
64
65
66
67 protected AbstractCsvBeanTemplate(final Class<T> type) {
68 if (type == null) {
69 throw new IllegalArgumentException("Class must not be null");
70 }
71 this.type = type;
72 }
73
74
75
76
77
78
79
80
81
82 public Class<T> getType() {
83 return type;
84 }
85
86
87
88
89
90
91
92 public void setValueParserMapping(final Map<String, Format> valueParserMapping) {
93 if (valueParserMapping == null) {
94 throw new IllegalArgumentException("CSV value parser mapping must not be null");
95 }
96 this.valueParserMapping = valueParserMapping;
97 }
98
99
100
101
102
103
104
105
106 @SuppressWarnings("unchecked")
107 public O valueParserMapping(final Map<String, Format> valueParserMapping) {
108 setValueParserMapping(valueParserMapping);
109 return (O) this;
110 }
111
112
113
114
115
116
117
118 public void setValueFormatterMapping(final Map<Object, Format> valueFormatterMapping) {
119 if (valueFormatterMapping == null) {
120 throw new IllegalArgumentException("CSV value formatter mapping must not be null");
121 }
122 this.valueFormatterMapping = valueFormatterMapping;
123 }
124
125
126
127
128
129
130
131
132 @SuppressWarnings("unchecked")
133 public O valueFormatterMapping(final Map<Object, Format> valueFormatterMapping) {
134 setValueFormatterMapping(valueFormatterMapping);
135 return (O) this;
136 }
137
138
139
140
141
142
143
144 public void setValueConverter(final CsvValueConverter valueConverter) {
145 if (valueConverter == null) {
146 throw new IllegalArgumentException("CsvValueConverter must not be null");
147 }
148 this.valueConverter = valueConverter;
149 }
150
151
152
153
154
155
156
157
158 @SuppressWarnings("unchecked")
159 public O valueConverter(final CsvValueConverter valueConverter) {
160 setValueConverter(valueConverter);
161 return (O) this;
162 }
163
164
165
166
167
168
169
170
171
172
173 @SuppressWarnings("unchecked")
174 public O format(final String name, final Format format) {
175 setValueParser(name, format);
176 setValueFormatter(name, format);
177 return (O) this;
178 }
179
180
181
182
183
184
185
186
187
188
189
190 public void setValueParser(final String field, final Format parser) {
191 final Format src = valueParserMapping.get(field);
192 if (src != null) {
193 valueParserMapping.put(field, FormatUtils.mergeFormatPattern(src, parser));
194 } else {
195 valueParserMapping.put(field, parser);
196 }
197 }
198
199
200
201
202
203
204
205 public void setValueFormatter(final Object column, final Format formatter) {
206 valueFormatterMapping.put(column, formatter);
207 }
208
209
210
211
212
213
214
215 public T createBean() throws IOException {
216 try {
217 return type.newInstance();
218 } catch (final IllegalAccessException e) {
219 throw new IOException("Cannot create " + type.getName() + ": " + e.getMessage(), e);
220 } catch (final InstantiationException e) {
221 throw new IOException("Cannot create " + type.getName() + ": " + e.getMessage(), e);
222 }
223 }
224
225
226
227
228
229
230
231
232 public Map<String, Object[]> createFieldAndColumnsMap(final Map<?, String> map) {
233 final Map<String, Object[]> results = new HashMap<String, Object[]>();
234 for (final Field f : type.getDeclaredFields()) {
235 final String fieldName = f.getName();
236 final List<Object> list = new ArrayList<Object>();
237 for (final Map.Entry<?, String> e : map.entrySet()) {
238 if (fieldName.equals(e.getValue())) {
239 list.add(e.getKey());
240 }
241 }
242 if (list.size() > 0) {
243 results.put(fieldName, list.toArray());
244 }
245 }
246 return results;
247 }
248
249
250
251
252
253
254
255
256
257
258 public Object stringToObject(final Field field, final String value) {
259 final Format format = valueParserMapping.get(field.getName());
260 if (format != null) {
261 if (value == null || value.isEmpty()) {
262 return null;
263 }
264 try {
265 return format.parseObject(value);
266 } catch (final ParseException e) {
267 throw new IllegalArgumentException(String.format("Unable to parse the %s: %s", field.getName(), value), e);
268 }
269 }
270 final Class<?> type = field.getType();
271 return valueConverter.convert(value, type.isArray() ? type.getComponentType() : type);
272 }
273
274
275
276
277
278
279
280
281
282
283 public String objectToString(final Object column, final Object obj) {
284 final Format format = valueFormatterMapping.get(column);
285 if (format != null) {
286 if (obj == null) {
287 return null;
288 }
289 return format.format(obj);
290 }
291 return valueConverter.convert(obj);
292 }
293
294 }