1 package com.github.mygreen.supercsv.builder;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Optional;
6
7 import org.supercsv.cellprocessor.ift.CellProcessor;
8
9 import com.github.mygreen.supercsv.validation.CsvValidator;
10
11
12
13
14
15
16
17
18
19 public class BeanMapping<T> {
20
21 private final Class<T> type;
22
23 private boolean header;
24
25 private boolean validateHeader;
26
27 private List<CsvValidator<T>> validators = new ArrayList<>();
28
29 private List<Object> listeners = new ArrayList<>();
30
31 private List<ColumnMapping> columns = new ArrayList<>();
32
33 private List<CallbackMethod> preReadMethods = new ArrayList<>();
34
35 private List<CallbackMethod> postReadMethods = new ArrayList<>();
36
37 private List<CallbackMethod> preWriteMethods = new ArrayList<>();
38
39 private List<CallbackMethod> postWriteMethods = new ArrayList<>();
40
41 private boolean skipValidationOnWrite;
42
43 private Class<?>[] groups;
44
45 private HeaderMapper headerMapper;
46
47 private Configuration configuration;
48
49 public BeanMapping(final Class<T> type) {
50 this.type = type;
51 }
52
53
54
55
56
57
58 public Optional<ColumnMapping> getColumnMapping(final int columnNumber) {
59 return columns.stream()
60 .filter(c -> c.getNumber() == columnNumber)
61 .findFirst();
62 }
63
64
65
66
67
68
69 public Optional<ColumnMapping> getColumnMapping(final String columnName) {
70 return columns.stream()
71 .filter(c -> c.getName() != null && c.getName().equals(columnName))
72 .findFirst();
73 }
74
75
76
77
78
79 public String[] getHeader() {
80
81 return columns.stream()
82 .map(c -> headerMapper.toMap(c, configuration, groups))
83 .toArray(n -> new String[n]);
84
85 }
86
87
88
89
90
91
92 public String[] getNameMapping() {
93
94 return columns.stream()
95 .map(c -> c.getName())
96 .toArray(n -> new String[n]);
97 }
98
99
100
101
102
103 public CellProcessor[] getCellProcessorsForReading() {
104
105 return columns.stream()
106 .map(c -> c.getCellProcessorForReading())
107 .toArray(n -> new CellProcessor[n]);
108 }
109
110
111
112
113
114 public CellProcessor[] getCellProcessorsForWriting() {
115
116 return columns.stream()
117 .map(c -> c.getCellProcessorForWriting())
118 .toArray(n -> new CellProcessor[n]);
119 }
120
121
122
123
124
125 public Class<T> getType() {
126 return type;
127 }
128
129
130
131
132
133 public boolean isHeader() {
134 return header;
135 }
136
137
138
139
140
141 public void setHeader(boolean header) {
142 this.header = header;
143 }
144
145 public boolean isValidateHeader() {
146 return validateHeader;
147 }
148
149 public void setValidateHeader(boolean validateHeader) {
150 this.validateHeader = validateHeader;
151 }
152
153 public List<CsvValidator<T>> getValidators() {
154 return validators;
155 }
156
157 public void addAllValidators(List<CsvValidator<T>> validators) {
158 this.validators.addAll(validators);
159 }
160
161 public List<Object> getListeners() {
162 return listeners;
163 }
164
165 public void addAllListeners(List<Object> listeners) {
166 this.listeners.addAll(listeners);
167 }
168
169 public List<ColumnMapping> getColumns() {
170 return columns;
171 }
172
173 public void setColumns(List<ColumnMapping> columns) {
174 this.columns = columns;;
175 }
176
177 public void addAllColumns(List<ColumnMapping> columns) {
178 this.columns.addAll(columns);
179 }
180
181 public List<CallbackMethod> getPreReadMethods() {
182 return preReadMethods;
183 }
184
185 public void addPreReadMethod(final CallbackMethod method) {
186 this.preReadMethods.add(method);
187 }
188
189 public List<CallbackMethod> getPostReadMethods() {
190 return postReadMethods;
191 }
192
193 public void addPostReadMethod(final CallbackMethod method) {
194 this.postReadMethods.add(method);
195 }
196
197 public List<CallbackMethod> getPreWriteMethods() {
198 return preWriteMethods;
199 }
200
201 public void addPreWriteMethod(final CallbackMethod method) {
202 this.preWriteMethods.add(method);
203 }
204
205 public List<CallbackMethod> getPostWriteMethods() {
206 return postWriteMethods;
207 }
208
209 public void addPostWriteMethod(final CallbackMethod method) {
210 this.postWriteMethods.add(method);
211 }
212
213 public boolean isSkipValidationOnWrite() {
214 return skipValidationOnWrite;
215 }
216
217 public void setSkipValidationOnWrite(boolean skipValidationOnWrite) {
218 this.skipValidationOnWrite = skipValidationOnWrite;
219 }
220
221 public Class<?>[] getGroups() {
222 return groups;
223 }
224
225 public void setGroups(Class<?>[] groups) {
226 this.groups = groups;
227 }
228
229
230
231
232
233
234 public HeaderMapper getHeaderMapper() {
235 return headerMapper;
236 }
237
238
239
240
241
242
243 public void setHeaderMapper(HeaderMapper headerMapper) {
244 this.headerMapper = headerMapper;
245 }
246
247
248
249
250
251 public Configuration getConfiguration() {
252 return configuration;
253 }
254
255
256
257
258
259 public void setConfiguration(Configuration configuraton) {
260 this.configuration = configuraton;
261 }
262
263 }