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.CsvColumnNameMappingBeanOperation;
28 import com.orangesignal.csv.bean.CsvColumnNameMappingBeanTemplate;
29 import com.orangesignal.csv.filters.CsvNamedValueFilter;
30 import com.orangesignal.csv.io.CsvColumnNameMappingBeanReader;
31 import com.orangesignal.csv.io.CsvColumnNameMappingBeanWriter;
32
33
34
35
36
37
38
39 public class ColumnNameMappingBeanListHandler<T> extends AbstractBeanListHandler<T, CsvColumnNameMappingBeanTemplate<T>, ColumnNameMappingBeanListHandler<T>> implements CsvColumnNameMappingBeanOperation<ColumnNameMappingBeanListHandler<T>> {
40
41
42
43
44
45
46 private boolean header = true;
47
48
49
50
51
52
53
54
55
56 public ColumnNameMappingBeanListHandler(final Class<T> type) {
57 super(CsvColumnNameMappingBeanTemplate.newInstance(type));
58 }
59
60
61
62 @Override
63 public ColumnNameMappingBeanListHandler<T> column(final String column, final String field) {
64 return column(column, field, null);
65 }
66
67 @Override
68 public ColumnNameMappingBeanListHandler<T> column(final String column, final String field, final Format format) {
69 template.column(column, field, format);
70 return this;
71 }
72
73 @Override
74 public void setColumnMapping(final Map<String, String> columnMapping) {
75 template.setColumnMapping(columnMapping);
76 }
77
78 @Override
79 public ColumnNameMappingBeanListHandler<T> columnMapping(final Map<String, String> columnMapping) {
80 setColumnMapping(columnMapping);
81 return this;
82 }
83
84 @Override
85 public ColumnNameMappingBeanListHandler<T> filter(final CsvNamedValueFilter filter) {
86 template.filter(filter);
87 return this;
88 }
89
90
91
92
93
94
95
96
97 public ColumnNameMappingBeanListHandler<T> header(final boolean header) {
98 this.header = header;
99 return this;
100 }
101
102
103
104 @Override
105 public List<T> load(final CsvReader reader, final boolean ignoreScalar) throws IOException {
106 @SuppressWarnings("resource")
107 final CsvColumnNameMappingBeanReader<T> r = new CsvColumnNameMappingBeanReader<T>(reader, template);
108
109
110 final List<T> results = new ArrayList<T>();
111 final boolean order = ignoreScalar || orders != null && !orders.isEmpty();
112 int offset = 0;
113
114 List<String> values;
115 while ((values = r.readValues()) != null && (order || limit <= 0 || results.size() < limit)) {
116 if (beanFilter == null && !order && offset < this.offset) {
117 offset++;
118 continue;
119 }
120 final T bean = r.toBean(values);
121 if (beanFilter != null) {
122 if (!beanFilter.accept(bean)) {
123 continue;
124 }
125 if (!order && offset < this.offset) {
126 offset++;
127 continue;
128 }
129 }
130 results.add(bean);
131 }
132
133 if (ignoreScalar || !order) {
134 return results;
135 }
136 return processScalar(results);
137 }
138
139 @Override
140 public void save(final List<T> list, final CsvWriter writer) throws IOException {
141 @SuppressWarnings("resource")
142 final CsvColumnNameMappingBeanWriter<T> w = new CsvColumnNameMappingBeanWriter<T>(writer, template, header);
143
144
145 for (final T bean : list) {
146
147 if (bean == null) {
148 w.write(null);
149 continue;
150 } else if (beanFilter != null && !beanFilter.accept(bean)) {
151 continue;
152 }
153
154 w.write(bean);
155 }
156 }
157
158 }