1 package org.supercsv.cellprocessor.joda;
2
3 import org.joda.time.format.DateTimeFormatter;
4 import org.supercsv.cellprocessor.CellProcessorAdaptor;
5 import org.supercsv.cellprocessor.ift.CellProcessor;
6 import org.supercsv.cellprocessor.ift.StringCellProcessor;
7 import org.supercsv.exception.SuperCsvCellProcessorException;
8 import org.supercsv.util.CsvContext;
9
10
11
12
13
14
15
16
17
18 public abstract class AbstractJodaParsingProcessor<T> extends
19 CellProcessorAdaptor implements StringCellProcessor {
20
21 private final DateTimeFormatter formatter;
22
23
24
25
26
27 public AbstractJodaParsingProcessor() {
28 this.formatter = null;
29 }
30
31
32
33
34
35
36
37
38
39
40
41 public AbstractJodaParsingProcessor(final CellProcessor next) {
42 super(next);
43 this.formatter = null;
44 }
45
46
47
48
49
50
51
52
53
54
55 public AbstractJodaParsingProcessor(final DateTimeFormatter formatter) {
56 checkPreconditions(formatter);
57 this.formatter = formatter;
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public AbstractJodaParsingProcessor(final DateTimeFormatter formatter,
73 final CellProcessor next) {
74 super(next);
75 checkPreconditions(formatter);
76 this.formatter = formatter;
77 }
78
79
80
81
82
83
84
85
86
87
88 private static void checkPreconditions(final DateTimeFormatter formatter) {
89 if (formatter == null) {
90 throw new NullPointerException("formatter should not be null");
91 }
92 }
93
94
95
96
97
98
99
100 public Object execute(final Object value, final CsvContext context) {
101 validateInputNotNull(value, context);
102 if (!(value instanceof String)) {
103 throw new SuperCsvCellProcessorException(String.class, value,
104 context, this);
105 }
106
107 final String string = (String) value;
108 final T result;
109 try {
110 if (formatter != null) {
111 result = parse(string, formatter);
112 } else {
113 result = parse(string);
114 }
115 } catch (IllegalArgumentException e) {
116 throw new SuperCsvCellProcessorException("Failed to parse value",
117 context, this, e);
118 }
119
120 return next.execute(result, context);
121 }
122
123
124
125
126
127
128
129
130
131
132 protected abstract T parse(final String string);
133
134
135
136
137
138
139
140
141
142
143
144
145
146 protected abstract T parse(final String string,
147 final DateTimeFormatter formatter);
148
149 }