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.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23
24 import com.orangesignal.csv.CsvReader;
25 import com.orangesignal.csv.CsvWriter;
26 import com.orangesignal.csv.filters.CsvValueFilter;
27 import com.orangesignal.csv.io.CsvColumnPositionMapReader;
28 import com.orangesignal.csv.io.CsvColumnPositionMapWriter;
29
30
31
32
33
34
35
36 public class ColumnPositionMapListHandler extends AbstractCsvListHandler<Map<Integer, String>, ColumnPositionMapListHandler> {
37
38
39
40
41 private CsvValueFilter valueFilter;
42
43
44
45
46 public ColumnPositionMapListHandler() {}
47
48
49
50
51
52
53
54
55 public ColumnPositionMapListHandler filter(final CsvValueFilter filter) {
56 this.valueFilter = filter;
57 return this;
58 }
59
60 @Override
61 public List<Map<Integer, String>> load(final CsvReader reader, final boolean ignoreScalar) throws IOException {
62 @SuppressWarnings("resource")
63 final CsvColumnPositionMapReader r = new CsvColumnPositionMapReader(reader);
64 r.setFilter(valueFilter);
65
66
67 final List<Map<Integer, String>> results = new ArrayList<Map<Integer, String>>();
68 int offset = 0;
69
70 List<String> values;
71 while ((values = r.readValues()) != null && (ignoreScalar || limit <= 0 || results.size() < limit)) {
72 if (!ignoreScalar && offset < this.offset) {
73 offset++;
74 continue;
75 }
76 results.add(r.toMap(values));
77 }
78 return results;
79 }
80
81 @Override
82 public void save(final List<Map<Integer, String>> list, final CsvWriter writer) throws IOException {
83 @SuppressWarnings("resource")
84 final CsvColumnPositionMapWriter w = new CsvColumnPositionMapWriter(writer);
85 w.setFilter(valueFilter);
86
87
88 for (final Map<Integer, String> map : list) {
89 w.write(map);
90 }
91 }
92
93 }