1 package com.github.mygreen.supercsv.cellprocessor.format;
2
3 import org.supercsv.cellprocessor.ift.BoolCellProcessor;
4 import org.supercsv.cellprocessor.ift.CellProcessor;
5 import org.supercsv.cellprocessor.ift.DateCellProcessor;
6 import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
7 import org.supercsv.cellprocessor.ift.LongCellProcessor;
8 import org.supercsv.cellprocessor.ift.StringCellProcessor;
9 import org.supercsv.util.CsvContext;
10
11 import com.github.mygreen.supercsv.cellprocessor.ValidationCellProcessor;
12
13
14
15
16
17
18
19
20 public class PrintProcessor<T> extends ValidationCellProcessor implements BoolCellProcessor, DateCellProcessor,
21 DoubleCellProcessor, LongCellProcessor, StringCellProcessor {
22
23 private final TextPrinter<T> printer;
24
25
26
27
28
29
30 public PrintProcessor(final TextPrinter<T> printer) {
31 super();
32 checkPreconditions(printer);
33 this.printer = printer;
34 }
35
36
37
38
39
40
41
42 public PrintProcessor(final TextPrinter<T> printer, final StringCellProcessor next) {
43 super(next);
44 checkPreconditions(printer);
45 this.printer = printer;
46 }
47
48
49
50
51
52
53 private static <T> void checkPreconditions(final TextPrinter<T> printer) {
54 if(printer == null) {
55 throw new NullPointerException("printer is null.");
56 }
57 }
58
59 @SuppressWarnings("unchecked")
60 @Override
61 public Object execute(final Object value, final CsvContext context) {
62
63 if(value == null) {
64 return next.execute(value, context);
65 }
66
67 try {
68 final String result = printer.print((T)value);
69 return next.execute(result, context);
70
71 } catch(TextPrintException e) {
72 throw createValidationException(context)
73 .messageFormat("'%s' could not print.", value.toString())
74 .exception(e)
75 .rejectedValue(value)
76 .build();
77 }
78
79 }
80
81 }