1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv.bean;
18
19 import java.text.Format;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.SortedMap;
25 import java.util.TreeMap;
26
27 import com.orangesignal.csv.filters.CsvValueFilter;
28
29
30
31
32
33
34
35 public class CsvColumnPositionMappingBeanTemplate<T> extends AbstractCsvBeanTemplate<T, CsvColumnPositionMappingBeanTemplate<T>> implements CsvColumnPositionMappingBeanOperation<CsvColumnPositionMappingBeanTemplate<T>> {
36
37
38
39
40 private SortedMap<Integer, String> columnMapping = new TreeMap<Integer, String>();
41
42
43
44
45 private CsvValueFilter filter;
46
47
48
49
50
51
52
53
54
55
56
57 public static <T> CsvColumnPositionMappingBeanTemplate<T> newInstance(final Class<T> type) {
58 return new CsvColumnPositionMappingBeanTemplate<T>(type);
59 }
60
61
62
63
64
65
66
67
68
69
70 public CsvColumnPositionMappingBeanTemplate(final Class<T> type) {
71 super(type);
72 }
73
74
75
76
77 @Override
78 public CsvColumnPositionMappingBeanTemplate<T> column(final String field) {
79 return column(field, null);
80 }
81
82 @Override
83 public CsvColumnPositionMappingBeanTemplate<T> column(final String field, final Format format) {
84 return column(getMaxColumnPosition() + 1, field, format);
85 }
86
87 @Override
88 public CsvColumnPositionMappingBeanTemplate<T> column(final int position, final String field) {
89 return column(position, field, null);
90 }
91
92 @Override
93 public CsvColumnPositionMappingBeanTemplate<T> column(final int position, final String field, final Format format) {
94 columnMapping.put(position, field);
95 if (format != null) {
96 setValueParser(field, format);
97 setValueFormatter(Integer.valueOf(position), format);
98 }
99 return this;
100 }
101
102 @Override
103 public void setColumnMapping(final Map<Integer, String> columnMapping) {
104 if (columnMapping == null) {
105 throw new IllegalArgumentException("Column mapping must not be null");
106 }
107 this.columnMapping = new TreeMap<Integer, String>(columnMapping);
108 }
109
110 @Override
111 public CsvColumnPositionMappingBeanTemplate<T> columnMapping(final Map<Integer, String> columnMapping) {
112 setColumnMapping(columnMapping);
113 return this;
114 }
115
116 @Override
117 public CsvColumnPositionMappingBeanTemplate<T> filter(final CsvValueFilter filter) {
118 this.filter = filter;
119 return this;
120 }
121
122
123
124
125
126
127
128
129
130 public int getMaxColumnPosition() {
131 if (columnMapping.size() > 0) {
132 return columnMapping.lastKey();
133 }
134 return -1;
135 }
136
137
138
139
140
141
142 public List<String> createColumnNames() {
143 final List<String> columnNames = new ArrayList<String>(columnMapping.size());
144 for (final Map.Entry<Integer, String> e : columnMapping.entrySet()) {
145 columnNames.add(e.getValue());
146 }
147 return columnNames;
148 }
149
150
151
152
153
154
155
156 public boolean isAccept(final List<String> values) {
157 return filter != null && !filter.accept(values);
158 }
159
160
161
162 public Map<String, Object[]> createFieldAndColumnsMap() {
163 return super.createFieldAndColumnsMap(columnMapping);
164 }
165
166
167
168 public Set<Map.Entry<Integer, String>> columnMappingEntrySet() {
169 return columnMapping.entrySet();
170 }
171
172 }