1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv.io;
18
19 import java.io.Closeable;
20 import java.io.Flushable;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27
28 import com.orangesignal.csv.CsvWriter;
29 import com.orangesignal.csv.filters.CsvNamedValueFilter;
30
31
32
33
34
35
36
37 public class CsvColumnNameMapWriter implements Closeable, Flushable {
38
39
40
41
42 private CsvWriter writer;
43
44
45
46
47 private List<String> columnNames;
48
49
50
51
52
53
54 private final boolean header;
55
56
57
58
59 private int columnCount = -1;
60
61
62
63
64 private CsvNamedValueFilter filter;
65
66
67
68
69
70
71
72
73
74
75 public CsvColumnNameMapWriter(final CsvWriter writer) {
76 this(writer, null, true);
77 }
78
79
80
81
82
83
84
85
86
87 public CsvColumnNameMapWriter(final CsvWriter writer, final boolean header) {
88 this(writer, null, header);
89 }
90
91
92
93
94
95
96
97
98 public CsvColumnNameMapWriter(final CsvWriter writer, final List<String> columnNames) {
99 this(writer, columnNames, true);
100 }
101
102
103
104
105
106
107
108
109
110
111 public CsvColumnNameMapWriter(final CsvWriter writer, final List<String> columnNames, final boolean header) {
112 if (writer == null) {
113 throw new IllegalArgumentException("CsvWriter must not be null");
114 }
115 this.writer = writer;
116
117 if (columnNames != null) {
118 this.columnNames = Collections.unmodifiableList(columnNames);
119 }
120 this.header = header;
121 }
122
123
124
125
126
127
128
129 private void ensureOpen() throws IOException {
130 if (writer == null) {
131 throw new IOException("CsvWriter closed");
132 }
133 }
134
135 private void ensureHeader(final Map<String, String> map) throws IOException {
136 if (columnNames == null && map != null) {
137 columnNames = new ArrayList<String>(map.keySet());
138 }
139 if (columnNames == null) {
140
141 throw new IOException("No header is available");
142 }
143 if (columnCount == -1) {
144 if (header) {
145
146 writer.writeValues(columnNames);
147 }
148 columnCount = columnNames.size();
149 }
150 }
151
152
153
154
155 @Override
156 public void flush() throws IOException {
157 synchronized (this) {
158 ensureOpen();
159 writer.flush();
160 }
161 }
162
163 @Override
164 public void close() throws IOException {
165 synchronized (this) {
166 ensureOpen();
167 writer.close();
168 writer = null;
169 columnNames = null;
170 columnCount = -1;
171 }
172 }
173
174
175
176
177
178
179
180
181
182
183 public void writeHeader(final Map<String, String> map) throws IOException {
184 synchronized (this) {
185 ensureOpen();
186 ensureHeader(map);
187 }
188 }
189
190
191
192
193
194
195
196
197 public boolean write(final Map<String, String> map) throws IOException {
198 synchronized (this) {
199 ensureOpen();
200 ensureHeader(map);
201
202
203 if (map == null) {
204 writer.writeValues(null);
205 return true;
206 }
207
208 final List<String> values = toValues(map);
209 if (filter != null && !filter.accept(columnNames, values)) {
210 return false;
211 }
212 writer.writeValues(values);
213 return true;
214 }
215 }
216
217 private List<String> toValues(final Map<String, String> map) {
218 final String[] values = new String[columnCount];
219 for (int i = 0; i < columnCount; i++) {
220 values[i] = map.get(columnNames.get(i));
221 }
222 return Arrays.asList(values);
223 }
224
225
226
227
228
229
230
231
232
233 public CsvNamedValueFilter getFilter() {
234 return filter;
235 }
236
237
238
239
240
241
242 public void setFilter(final CsvNamedValueFilter filter) {
243 synchronized (this) {
244 this.filter = filter;
245 }
246 }
247
248 }