View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
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   * The Class CsvClientImpl.
37   *
38   * @param <T>
39   *            the generic type
40   */
41  public class CsvClientImpl<T> implements CsvClient<T> {
42  
43      /** The bean reader. */
44      private BeanReader<T> beanReader;
45  
46      /** The bean writer. */
47      private BeanWriter<T> beanWriter;
48  
49      /** The row reader. */
50      private RowReader rowReader;
51  
52      /** The row writer. */
53      private RowWriter rowWriter;
54  
55      /** The row instructions. */
56      private final RowInstructions rowInstructions;
57  
58      /** The bean instructions. */
59      private BeanInstructions beanInstructions;
60  
61      /**
62       * Instantiates a new csv client impl.
63       *
64       * @param writer
65       *            the writer
66       */
67      public CsvClientImpl(Writer writer) {
68          this.rowWriter = new RowWriterImpl(writer);
69          this.rowInstructions = getRowWriter().getRowInstructions();
70      }
71  
72      /**
73       * Instantiates a new csv client impl.
74       *
75       * @param writer
76       *            the writer
77       * @param beanClass
78       *            the bean class
79       */
80      public CsvClientImpl(Writer writer, Class<T> beanClass) {
81          this(writer, new BeanParser().getBeanInstructions(beanClass));
82      }
83  
84      /**
85       * Instantiates a new csv client impl.
86       *
87       * @param writer
88       *            the writer
89       * @param beanInstructions
90       *            the bean instructions
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      * Instantiates a new csv client impl.
101      *
102      * @param reader
103      *            the reader
104      */
105     public CsvClientImpl(Reader reader) {
106         this.rowReader = new RowReaderImpl(reader);
107         this.rowInstructions = getRowReader().getRowInstructions();
108     }
109 
110     /**
111      * Instantiates a new csv client impl.
112      *
113      * @param reader
114      *            the reader
115      * @param beanClass
116      *            the bean class
117      */
118     public CsvClientImpl(Reader reader, Class<T> beanClass) {
119         this(reader, new BeanParser().getBeanInstructions(beanClass));
120     }
121 
122     /**
123      * Instantiates a new csv client impl.
124      *
125      * @param reader
126      *            the reader
127      * @param beanInstructions
128      *            the bean instructions
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      * Gets the bean instructions.
339      *
340      * @return the bean instructions
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      * Gets the bean reader.
353      *
354      * @return the bean reader
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      * Gets the bean writer.
366      *
367      * @return the bean writer
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      * Gets the row reader.
379      *
380      * @return the row reader
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      * Gets the row writer.
392      *
393      * @return the row writer
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 }