1 package com.github.mygreen.supercsv.cellprocessor.conversion;
2
3 import org.supercsv.cellprocessor.CellProcessorAdaptor;
4 import org.supercsv.cellprocessor.ift.CellProcessor;
5 import org.supercsv.cellprocessor.ift.StringCellProcessor;
6 import org.supercsv.util.CsvContext;
7
8
9
10
11
12
13
14
15 public class MultiPad extends CellProcessorAdaptor implements StringCellProcessor {
16
17
18
19
20 private final int size;
21
22
23
24
25 private final char padChar;
26
27
28
29
30 private final boolean rightAlign;
31
32
33
34
35 private final boolean chopped;
36
37
38
39
40 private final PaddingProcessor paddingProcessor;
41
42
43
44
45
46
47
48
49
50
51
52 public MultiPad(final int size, final char padChar, final boolean rightAlign, final boolean chopped,
53 final PaddingProcessor paddingProcessor) {
54 super();
55
56 checkPreconditions(size, paddingProcessor);
57
58 this.size = size;
59 this.padChar = padChar;
60 this.rightAlign = rightAlign;
61 this.chopped = chopped;
62 this.paddingProcessor = paddingProcessor;
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76 public MultiPad(final int size, final char padChar, final boolean rightAlign, final boolean chopped,
77 final PaddingProcessor paddingProcessor, final StringCellProcessor next) {
78 super(next);
79
80 checkPreconditions(size, paddingProcessor);
81
82 this.size = size;
83 this.padChar = padChar;
84 this.rightAlign = rightAlign;
85 this.chopped = chopped;
86 this.paddingProcessor = paddingProcessor;
87 }
88
89 private static void checkPreconditions(final int size, final PaddingProcessor paddingProcessor) {
90
91 if(size <= 0) {
92 throw new IllegalArgumentException(String.format("size should be > 0, but was %d.", size));
93 }
94
95 if(paddingProcessor == null) {
96 throw new IllegalArgumentException("paddingProcessor should not be null.");
97 }
98
99 }
100
101
102
103
104 @Override
105 public <T> T execute(final Object value, final CsvContext context) {
106
107 final String result = paddingProcessor.pad(value == null ? "" : (String) value,
108 size, padChar, rightAlign, chopped);
109 return next.execute(result, context);
110
111 }
112
113
114
115
116
117 public int getSize() {
118 return size;
119 }
120
121
122
123
124
125 public char getPadChar() {
126 return padChar;
127 }
128
129
130
131
132
133 public boolean isRightAlign() {
134 return rightAlign;
135 }
136
137
138
139
140
141 public boolean isChopped() {
142 return chopped;
143 }
144
145
146
147
148
149 public PaddingProcessor getPaddingProcessor() {
150 return paddingProcessor;
151 }
152
153 }