1
2
3
4
5
6
7
8
9
10
11 package org.csveed.api;
12
13 import java.io.Reader;
14 import java.io.Writer;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Locale;
18
19 import org.csveed.bean.AbstractMapper;
20 import org.csveed.bean.BeanInstructions;
21 import org.csveed.bean.BeanParser;
22 import org.csveed.bean.BeanReader;
23 import org.csveed.bean.BeanReaderImpl;
24 import org.csveed.bean.BeanWriter;
25 import org.csveed.bean.BeanWriterImpl;
26 import org.csveed.bean.conversion.Converter;
27 import org.csveed.report.CsvException;
28 import org.csveed.report.GeneralError;
29 import org.csveed.row.RowInstructions;
30 import org.csveed.row.RowReader;
31 import org.csveed.row.RowReaderImpl;
32 import org.csveed.row.RowWriter;
33 import org.csveed.row.RowWriterImpl;
34
35
36
37
38
39
40
41 public class CsvClientImpl<T> implements CsvClient<T> {
42
43
44 private BeanReader<T> beanReader;
45
46
47 private BeanWriter<T> beanWriter;
48
49
50 private RowReader rowReader;
51
52
53 private RowWriter rowWriter;
54
55
56 private final RowInstructions rowInstructions;
57
58
59 private BeanInstructions beanInstructions;
60
61
62
63
64
65
66
67 public CsvClientImpl(Writer writer) {
68 this.rowWriter = new RowWriterImpl(writer);
69 this.rowInstructions = getRowWriter().getRowInstructions();
70 }
71
72
73
74
75
76
77
78
79
80 public CsvClientImpl(Writer writer, Class<T> beanClass) {
81 this(writer, new BeanParser().getBeanInstructions(beanClass));
82 }
83
84
85
86
87
88
89
90
91
92 public CsvClientImpl(Writer writer, BeanInstructions beanInstructions) {
93 this.beanWriter = new BeanWriterImpl<>(writer, beanInstructions);
94 this.rowWriter = getBeanWriter().getRowWriter();
95 this.rowInstructions = getRowWriter().getRowInstructions();
96 this.beanInstructions = beanInstructions;
97 }
98
99
100
101
102
103
104
105 public CsvClientImpl(Reader reader) {
106 this.rowReader = new RowReaderImpl(reader);
107 this.rowInstructions = getRowReader().getRowInstructions();
108 }
109
110
111
112
113
114
115
116
117
118 public CsvClientImpl(Reader reader, Class<T> beanClass) {
119 this(reader, new BeanParser().getBeanInstructions(beanClass));
120 }
121
122
123
124
125
126
127
128
129
130 public CsvClientImpl(Reader reader, BeanInstructions beanInstructions) {
131 this.beanReader = new BeanReaderImpl<>(reader, beanInstructions);
132 this.rowReader = getBeanReader().getRowReader();
133 this.rowInstructions = getRowReader().getRowInstructions();
134 this.beanInstructions = beanInstructions;
135 }
136
137 @Override
138 public void writeBeans(Collection<T> beans) {
139 getBeanWriter().writeBeans(beans);
140 }
141
142 @Override
143 public void writeBean(T bean) {
144 getBeanWriter().writeBean(bean);
145 }
146
147 @Override
148 public void writeRow(Row row) {
149 getRowWriter().writeRow(row);
150 }
151
152 @Override
153 public Row writeRow(String[] row) {
154 return getRowWriter().writeRow(row);
155 }
156
157 @Override
158 public void writeRows(Collection<Row> rows) {
159 getRowWriter().writeRows(rows);
160 }
161
162 @Override
163 public void writeRows(String[][] rows) {
164 getRowWriter().writeRows(rows);
165 }
166
167 @Override
168 public Header writeHeader(String[] header) {
169 return getRowWriter().writeHeader(header);
170 }
171
172 @Override
173 public void writeHeader(Header header) {
174 getRowWriter().writeHeader(header);
175 }
176
177 @Override
178 public void writeHeader() {
179 getBeanWriter().writeHeader();
180 }
181
182 @Override
183 public List<T> readBeans() {
184 return getBeanReader().readBeans();
185 }
186
187 @Override
188 public T readBean() {
189 return getBeanReader().readBean();
190 }
191
192 @Override
193 public List<Row> readRows() {
194 return getRowReader().readRows();
195 }
196
197 @Override
198 public Row readRow() {
199 return getRowReader().readRow();
200 }
201
202 @Override
203 public Header readHeader() {
204 return getRowReader().readHeader();
205 }
206
207 @Override
208 public int getCurrentLine() {
209 return getRowReader().getCurrentLine();
210 }
211
212 @Override
213 public boolean isFinished() {
214 return getRowReader().isFinished();
215 }
216
217 @Override
218 public CsvClient<T> setUseHeader(boolean useHeader) {
219 rowInstructions.setUseHeader(useHeader);
220 return this;
221 }
222
223 @Override
224 public CsvClient<T> setStartRow(int startRow) {
225 rowInstructions.setStartRow(startRow);
226 return this;
227 }
228
229 @Override
230 public CsvClient<T> setEscape(char symbol) {
231 rowInstructions.setEscape(symbol);
232 return this;
233 }
234
235 @Override
236 public CsvClient<T> setQuote(char symbol) {
237 rowInstructions.setQuote(symbol);
238 return this;
239 }
240
241 @Override
242 public CsvClient<T> setSeparator(char symbol) {
243 rowInstructions.setSeparator(symbol);
244 return this;
245 }
246
247 @Override
248 public CsvClient<T> setComment(char symbol) {
249 rowInstructions.setComment(symbol);
250 return this;
251 }
252
253 @Override
254 public CsvClient<T> setEndOfLine(char[] symbols) {
255 rowInstructions.setEndOfLine(symbols);
256 return this;
257 }
258
259 @Override
260 public CsvClient<T> skipEmptyLines(boolean skip) {
261 rowInstructions.skipEmptyLines(skip);
262 return this;
263 }
264
265 @Override
266 public CsvClient<T> skipCommentLines(boolean skip) {
267 rowInstructions.skipCommentLines(skip);
268 return this;
269 }
270
271 @Override
272 public CsvClient<T> setMapper(Class<? extends AbstractMapper> mapper) {
273 getBeanInstructions().setMapper(mapper);
274 return this;
275 }
276
277 @Override
278 public CsvClient<T> setDate(String propertyName, String dateFormat) {
279 getBeanInstructions().setDate(propertyName, dateFormat);
280 return this;
281 }
282
283 @Override
284 public CsvClient<T> setLocalizedNumber(String propertyName, Locale locale) {
285 getBeanInstructions().setLocalizedNumber(propertyName, locale);
286 return this;
287 }
288
289 @Override
290 public CsvClient<T> setRequired(String propertyName, boolean required) {
291 getBeanInstructions().setRequired(propertyName, required);
292 return this;
293 }
294
295 @Override
296 public CsvClient<T> setConverter(String propertyName, Converter converter) {
297 getBeanInstructions().setConverter(propertyName, converter);
298 return this;
299 }
300
301 @Override
302 public CsvClient<T> ignoreProperty(String propertyName) {
303 getBeanInstructions().ignoreProperty(propertyName);
304 return this;
305 }
306
307 @Override
308 public CsvClient<T> mapColumnIndexToProperty(int columnIndex, String propertyName) {
309 getBeanInstructions().mapColumnIndexToProperty(columnIndex, propertyName);
310 return this;
311 }
312
313 @Override
314 public CsvClient<T> mapColumnNameToProperty(String columnName, String propertyName) {
315 getBeanInstructions().mapColumnNameToProperty(columnName, propertyName);
316 return this;
317 }
318
319 @Override
320 public CsvClient<T> setStartIndexDynamicColumns(int startIndex) {
321 getBeanInstructions().setStartIndexDynamicColumns(startIndex);
322 return this;
323 }
324
325 @Override
326 public CsvClient<T> setHeaderNameToProperty(String propertyName) {
327 getBeanInstructions().setHeaderNameToProperty(propertyName);
328 return this;
329 }
330
331 @Override
332 public CsvClient<T> setHeaderValueToProperty(String propertyName) {
333 getBeanInstructions().setHeaderValueToProperty(propertyName);
334 return this;
335 }
336
337
338
339
340
341
342 private BeanInstructions getBeanInstructions() {
343 if (this.beanInstructions == null) {
344 throw new CsvException(new GeneralError(
345 "BeanInstructions have not been initialized. Make sure to pass BeanInstructions or the bean class"
346 + " to CsvClient."));
347 }
348 return this.beanInstructions;
349 }
350
351
352
353
354
355
356 private BeanReader<T> getBeanReader() {
357 if (this.beanReader == null) {
358 throw new CsvException(new GeneralError(
359 "BeanReader has not been initialized. Make sure to pass BeanInstructions or the bean class to CsvClient."));
360 }
361 return this.beanReader;
362 }
363
364
365
366
367
368
369 private BeanWriter<T> getBeanWriter() {
370 if (this.beanWriter == null) {
371 throw new CsvException(new GeneralError(
372 "BeanWriter has not been initialized. Make sure to pass BeanInstructions or the bean class to CsvClient."));
373 }
374 return this.beanWriter;
375 }
376
377
378
379
380
381
382 private RowReader getRowReader() {
383 if (this.rowReader == null) {
384 throw new CsvException(new GeneralError(
385 "RowReader has not been initialized. Make sure to pass a Reader to the constructor."));
386 }
387 return this.rowReader;
388 }
389
390
391
392
393
394
395 public RowWriter getRowWriter() {
396 if (this.rowWriter == null) {
397 throw new CsvException(new GeneralError(
398 "RowWriter has not been initialized. Make sure to pass a Writer to the constructor."));
399 }
400 return rowWriter;
401 }
402
403 }