1
2
3
4
5
6
7
8
9
10
11
12
13
14 package olg.csv.bean.impl;
15
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
18 import java.util.List;
19
20 import olg.csv.bean.PropertyException;
21 import olg.csv.bean.filter.AbstractStringFilter;
22 import olg.csv.bean.formatter.Formatter;
23
24
25
26
27
28
29
30
31
32
33 public abstract class PropertyFormatter {
34
35
36
37 protected String name;
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public abstract String toString(Object object);
52
53
54
55
56
57
58 public String getName() {
59 return name;
60 }
61
62
63
64
65
66
67
68
69 public String getFullName() {
70 return name;
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public static PropertyFormatter getConcatePropertyReader(List<PropertyFormatter> formatters,
86 AbstractStringFilter filtre) {
87 if (formatters == null || formatters.isEmpty()) {
88 throw new IllegalArgumentException("PropertyFormatter#getConcatePropertyReader "
89 + "propertiyFormatters argument must be not null and not empty");
90 }
91
92 return new ConcatePropertyReader(formatters, filtre);
93
94 }
95
96 @Override
97 public int hashCode() {
98 final int prime = 31;
99 int result = 1;
100 result = prime * result + (getFullName() == null ? 0 : getFullName().hashCode());
101 return result;
102 }
103 @Override
104 public boolean equals(Object obj) {
105 if (this == obj) {
106 return true;
107 }
108 if (obj == null) {
109 return false;
110 }
111 if (getClass() != obj.getClass()) {
112 return false;
113 }
114 PropertyFormatter other = (PropertyFormatter) obj;
115 if (getFullName() == null) {
116 if (other.getFullName() != null) {
117 return false;
118 }
119 } else if (!getFullName().equals(other.getFullName())) {
120 return false;
121 }
122 return true;
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public static PropertyFormatter getDelegatePropertyReader(Method getter, String name,
141 PropertyFormatter propertyFormatter) {
142
143 if (getter == null) {
144 throw new IllegalArgumentException(
145 "PropertyFormatter#getDelegatePropertyReader getter argument must ne not null");
146 }
147 if (propertyFormatter == null) {
148 throw new IllegalArgumentException(
149 "PropertyFormatter#getDelegatePropertyReader propertyFormatter argument must be not null");
150 }
151
152 return new DelegatePropertyReader(getter, name, propertyFormatter);
153
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public static <T> PropertyFormatter getPropertyReader(Method getter, String name, Formatter<T> formatter,
176 AbstractStringFilter filtre) {
177 if (getter == null) {
178 throw new IllegalArgumentException("PropertyFormatter#getPropertyReader getter argument must be not null");
179 }
180
181 return new PropertyReader(getter, name, (formatter == null ? new Formatter<T>() : formatter), filtre);
182 }
183
184
185
186
187
188
189
190 private static final class ConcatePropertyReader extends PropertyFormatter {
191
192
193
194
195
196
197
198
199
200 ConcatePropertyReader(List<PropertyFormatter> properties, AbstractStringFilter filtre) {
201 super();
202 this.properties = properties;
203 this.filtre = filtre;
204 }
205
206
207
208
209 private final List<PropertyFormatter> properties;
210
211
212
213
214 private final AbstractStringFilter filtre;
215 @Override
216 public String toString(Object object) {
217 String retour = null;
218 if (object != null) {
219
220 StringBuffer buf = new StringBuffer();
221 for (PropertyFormatter property : properties) {
222 String toBuf = property.toString(object);
223 buf.append(toBuf == null ? "" : toBuf);
224 }
225 retour = (filtre == null ? buf.toString() : filtre.filtre(buf.toString()));
226 }
227 return retour;
228 }
229
230
231
232
233 @Override
234 public String getName() {
235 throw new UnsupportedOperationException();
236 }
237
238 @Override
239 public String getFullName() {
240 StringBuilder builder = new StringBuilder();
241
242 for (PropertyFormatter property : properties) {
243 builder.append(property.getFullName()).append(";");
244 }
245 return builder.toString();
246 }
247
248 }
249
250
251
252
253
254
255
256 private static final class DelegatePropertyReader extends PropertyFormatter {
257
258
259
260
261
262
263
264
265
266
267
268 DelegatePropertyReader(Method getter, String name, PropertyFormatter propertyFormatter) {
269 super();
270 this.getter = getter;
271 this.name = name;
272 this.propertyFormatter = propertyFormatter;
273
274 }
275
276 @Override
277 public String getFullName() {
278
279 return name + "." + propertyFormatter.getFullName();
280 }
281
282 @Override
283 public String getName() {
284
285 return propertyFormatter.getName();
286 }
287
288
289
290
291 private final Method getter;
292
293
294
295 private final PropertyFormatter propertyFormatter;
296 @Override
297 public String toString(Object object) {
298 String retour = null;
299 if (object != null) {
300
301 try {
302 Object property = getter.invoke(object, (Object[]) null);
303 retour = propertyFormatter.toString(property);
304 } catch (IllegalArgumentException e) {
305 throw new PropertyException("Error on method invocation " + getter.getName(), e);
306
307
308
309
310
311 } catch (IllegalAccessException e) {
312 throw new PropertyException("Error on method invocation " + getter.getName(), e);
313 } catch (InvocationTargetException e) {
314 throw new PropertyException("Error on method invocation " + getter.getName(),
315
316
317
318
319
320 e.getTargetException());
321 }
322 }
323 return retour;
324 }
325
326 }
327
328
329
330
331
332
333 private static final class PropertyReader extends PropertyFormatter {
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 PropertyReader(Method getter, String name, @SuppressWarnings("rawtypes") Formatter formatter,
349 AbstractStringFilter filtre) {
350 super();
351 this.name = name;
352 this.getter = getter;
353 this.formatter = formatter;
354 this.filtre = filtre;
355
356 }
357
358
359
360
361 private final Method getter;
362
363
364
365
366 @SuppressWarnings("rawtypes")
367 private final Formatter formatter;
368
369
370
371 private final AbstractStringFilter filtre;
372
373 @SuppressWarnings("unchecked")
374 @Override
375 public String toString(Object object) {
376 String retour = null;
377 if (object != null) {
378 try {
379 Object property = getter.invoke(object, (Object[]) null);
380 String rendered = formatter.toString(property);
381 retour = (filtre == null ? rendered : filtre.filtre(rendered));
382
383 } catch (IllegalArgumentException e) {
384 throw new PropertyException("Error on method invocation " + getter.getName(), e);
385 } catch (IllegalAccessException e) {
386 throw new PropertyException("Error on method invocation " + getter.getName(), e);
387 } catch (InvocationTargetException e) {
388 throw new PropertyException("Error on method invocation " + getter.getName(),
389
390
391
392
393
394 e.getTargetException());
395 }
396 }
397 return retour;
398 }
399
400 }
401
402 }