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