1 package org.supercsv.cellprocessor.joda;
2
3 import java.util.Locale;
4
5 import org.joda.time.format.DateTimeFormatter;
6 import org.supercsv.cellprocessor.CellProcessorAdaptor;
7 import org.supercsv.cellprocessor.ift.CellProcessor;
8 import org.supercsv.exception.SuperCsvCellProcessorException;
9 import org.supercsv.util.CsvContext;
10
11
12
13
14
15
16
17
18
19 public abstract class AbstractJodaFormattingProcessor<T> extends
20 CellProcessorAdaptor {
21
22 private final Class<T> jodaClass;
23
24 private final DateTimeFormatter formatter;
25
26 private final String pattern;
27
28 private final Locale locale;
29
30
31
32
33
34
35
36
37
38
39 public AbstractJodaFormattingProcessor(final Class<T> jodaClass) {
40 checkPreconditions(jodaClass);
41 this.jodaClass = jodaClass;
42 this.formatter = null;
43 this.pattern = null;
44 this.locale = null;
45
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public AbstractJodaFormattingProcessor(final Class<T> jodaClass,
61 final CellProcessor next) {
62 super(next);
63 checkPreconditions(jodaClass);
64 this.jodaClass = jodaClass;
65 this.formatter = null;
66 this.pattern = null;
67 this.locale = null;
68
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82 public AbstractJodaFormattingProcessor(final Class<T> jodaClass,
83 final DateTimeFormatter formatter) {
84 checkPreconditions(jodaClass, formatter);
85 this.jodaClass = jodaClass;
86 this.formatter = formatter;
87 this.pattern = null;
88 this.locale = null;
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public AbstractJodaFormattingProcessor(final Class<T> jodaClass,
106 final DateTimeFormatter formatter, final CellProcessor next) {
107 super(next);
108 checkPreconditions(jodaClass, formatter);
109 this.jodaClass = jodaClass;
110 this.formatter = formatter;
111 this.pattern = null;
112 this.locale = null;
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public AbstractJodaFormattingProcessor(final Class<T> jodaClass,
128 final String pattern) {
129 this(jodaClass, pattern, (Locale) null);
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 public AbstractJodaFormattingProcessor(final Class<T> jodaClass,
147 final String pattern, final CellProcessor next) {
148 this(jodaClass, pattern, (Locale) null, next);
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public AbstractJodaFormattingProcessor(final Class<T> jodaClass,
166 final String pattern, final Locale locale) {
167 checkPreconditions(jodaClass, pattern);
168 this.jodaClass = jodaClass;
169 this.pattern = pattern;
170 this.locale = locale;
171 this.formatter = null;
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 public AbstractJodaFormattingProcessor(final Class<T> jodaClass,
191 final String pattern, final Locale locale, final CellProcessor next) {
192 super(next);
193 checkPreconditions(jodaClass, pattern);
194 this.jodaClass = jodaClass;
195 this.pattern = pattern;
196 this.locale = locale;
197 this.formatter = null;
198 }
199
200
201
202
203
204
205
206
207
208
209 private static void checkPreconditions(final Class<?> jodaClass) {
210 if (jodaClass == null) {
211 throw new NullPointerException("jodaClass should not be null");
212 }
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226 private static void checkPreconditions(final Class<?> jodaClass,
227 final DateTimeFormatter formatter) {
228 if (jodaClass == null) {
229 throw new NullPointerException("jodaClass should not be null");
230 } else if (formatter == null) {
231 throw new NullPointerException("formatter should not be null");
232 }
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246 private static void checkPreconditions(final Class<?> jodaClass,
247 final String pattern) {
248 if (jodaClass == null) {
249 throw new NullPointerException("jodaClass should not be null");
250 } else if (pattern == null) {
251 throw new NullPointerException("pattern should not be null");
252 }
253 }
254
255
256
257
258
259
260
261 public Object execute(final Object value, final CsvContext context) {
262 validateInputNotNull(value, context);
263 if (!(value.getClass().equals(jodaClass))) {
264 throw new SuperCsvCellProcessorException(jodaClass, value, context,
265 this);
266 }
267 final T jodaType = jodaClass.cast(value);
268 try {
269 if (formatter != null) {
270 return format(jodaType, formatter);
271 } else if (pattern != null) {
272 return format(jodaType, pattern, locale);
273 } else {
274 return format(jodaType);
275 }
276 } catch (IllegalArgumentException e) {
277 throw new SuperCsvCellProcessorException(
278 String.format("Failed to format value as a %s",
279 jodaClass.getSimpleName()), context, this, e);
280 }
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294 protected abstract String format(final T jodaType,
295 final DateTimeFormatter formatter);
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 protected abstract String format(final T jodaType, final String pattern,
312 final Locale locale);
313
314
315
316
317
318
319
320
321
322
323 protected abstract String format(final T jodaType);
324
325 }