1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv.handlers;
18
19 import java.io.IOException;
20 import java.text.Format;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24
25 import com.orangesignal.csv.CsvReader;
26 import com.orangesignal.csv.CsvWriter;
27 import com.orangesignal.csv.bean.CsvColumnPositionMappingBeanTemplate;
28 import com.orangesignal.csv.filters.CsvValueFilter;
29 import com.orangesignal.csv.io.CsvColumnPositionMappingBeanReader;
30 import com.orangesignal.csv.io.CsvColumnPositionMappingBeanWriter;
31
32
33
34
35
36
37
38 public class ColumnPositionMappingBeanListHandler<T> extends AbstractBeanListHandler<T, CsvColumnPositionMappingBeanTemplate<T>, ColumnPositionMappingBeanListHandler<T>> {
39
40
41
42
43
44
45 private boolean header = true;
46
47
48
49
50
51
52
53
54
55 public ColumnPositionMappingBeanListHandler(final Class<T> type) {
56 super(CsvColumnPositionMappingBeanTemplate.newInstance(type));
57 }
58
59
60
61
62
63
64
65
66
67 public ColumnPositionMappingBeanListHandler<T> addColumn(final String field) {
68 return addColumn(field, null);
69 }
70
71
72
73
74
75
76
77
78
79 public ColumnPositionMappingBeanListHandler<T> addColumn(final String field, final Format format) {
80 template.column(field, format);
81 return this;
82 }
83
84
85
86
87
88
89
90
91 public ColumnPositionMappingBeanListHandler<T> addColumn(final int position, final String field) {
92 return addColumn(position, field, null);
93 }
94
95
96
97
98
99
100
101
102
103
104 public ColumnPositionMappingBeanListHandler<T> addColumn(final int position, final String field, final Format format) {
105 template.column(position, field, format);
106 return this;
107 }
108
109
110
111
112
113
114
115
116 public void setColumnMapping(final Map<Integer, String> columnMapping) {
117 template.setColumnMapping(columnMapping);
118 }
119
120
121
122
123
124
125
126
127 public ColumnPositionMappingBeanListHandler<T> columnMapping(final Map<Integer, String> columnMapping) {
128 setColumnMapping(columnMapping);
129 return this;
130 }
131
132
133
134
135
136
137
138
139 public ColumnPositionMappingBeanListHandler<T> filter(final CsvValueFilter filter) {
140 template.filter(filter);
141 return this;
142 }
143
144
145
146
147
148
149
150
151 public ColumnPositionMappingBeanListHandler<T> header(final boolean header) {
152 this.header = header;
153 return this;
154 }
155
156
157
158 @Override
159 public List<T> load(final CsvReader reader, final boolean ignoreScalar) throws IOException {
160 @SuppressWarnings("resource")
161 final CsvColumnPositionMappingBeanReader<T> r = new CsvColumnPositionMappingBeanReader<T>(reader, template);
162
163
164 final List<T> results = new ArrayList<T>();
165 final boolean order = ignoreScalar || orders != null && !orders.isEmpty();
166 int offset = 0;
167
168 List<String> values;
169 while ((values = r.readValues()) != null && (order || limit <= 0 || results.size() < limit)) {
170 if (beanFilter == null && !order && offset < this.offset) {
171 offset++;
172 continue;
173 }
174 final T bean = r.toBean(values);
175 if (beanFilter != null) {
176 if (!beanFilter.accept(bean)) {
177 continue;
178 }
179 if (!order && offset < this.offset) {
180 offset++;
181 continue;
182 }
183 }
184 results.add(bean);
185 }
186
187 if (ignoreScalar || !order) {
188 return results;
189 }
190 return processScalar(results);
191 }
192
193 @Override
194 public void save(final List<T> list, final CsvWriter writer) throws IOException {
195 @SuppressWarnings("resource")
196 final CsvColumnPositionMappingBeanWriter<T> w = new CsvColumnPositionMappingBeanWriter<T>(writer, template, header);
197
198
199 for (final T bean : list) {
200
201 if (bean == null) {
202 w.write(null);
203 continue;
204 } else if (beanFilter != null && !beanFilter.accept(bean)) {
205 continue;
206 }
207
208 w.write(bean);
209 }
210 }
211
212 }