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.lang.reflect.Field;
20 import java.text.Format;
21 import java.util.ArrayList;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import com.orangesignal.csv.filters.CsvNamedValueFilter;
27
28
29
30
31
32
33
34 public class CsvColumnNameMappingBeanTemplate<T> extends AbstractCsvBeanTemplate<T, CsvColumnNameMappingBeanTemplate<T>> implements CsvColumnNameMappingBeanOperation<CsvColumnNameMappingBeanTemplate<T>> {
35
36
37
38
39 private Map<String, String> columnMapping = new LinkedHashMap<String, String>();
40
41
42
43
44 private CsvNamedValueFilter filter;
45
46
47
48
49
50
51
52
53
54
55
56 public static <T> CsvColumnNameMappingBeanTemplate<T> newInstance(final Class<T> type) {
57 return new CsvColumnNameMappingBeanTemplate<T>(type);
58 }
59
60
61
62
63
64
65
66
67
68
69 public CsvColumnNameMappingBeanTemplate(final Class<T> type) {
70 super(type);
71 }
72
73
74
75
76 @Override
77 public CsvColumnNameMappingBeanTemplate<T> column(final String column, final String field) {
78 return column(column, field, null);
79 }
80
81 @Override
82 public CsvColumnNameMappingBeanTemplate<T> column(final String column, final String field, final Format format) {
83 columnMapping.put(column, field);
84 if (format != null) {
85 setValueParser(field, format);
86 setValueFormatter(column, format);
87 }
88 return this;
89 }
90
91 @Override
92 public void setColumnMapping(final Map<String, String> columnMapping) {
93 if (columnMapping == null) {
94 throw new IllegalArgumentException("Column mapping must not be null");
95 }
96 this.columnMapping = new LinkedHashMap<String, String>(columnMapping);
97 }
98
99 @Override
100 public CsvColumnNameMappingBeanTemplate<T> columnMapping(final Map<String, String> columnMapping) {
101 setColumnMapping(columnMapping);
102 return this;
103 }
104
105 @Override
106 public CsvColumnNameMappingBeanTemplate<T> filter(final CsvNamedValueFilter filter) {
107 this.filter = filter;
108 return this;
109 }
110
111
112
113
114
115
116
117 public void setupColumnMappingIfNeed() {
118 if (columnMapping.size() == 0) {
119 for (final Field f : getType().getDeclaredFields()) {
120 final String name = f.getName();
121 column(name, name);
122 }
123 }
124 }
125
126
127
128
129
130
131
132
133 public boolean isAccept(final List<String> columnNames, final List<String> values) {
134 return filter != null && !filter.accept(columnNames, values);
135 }
136
137
138
139 public Map<String, Object[]> createFieldAndColumnsMap() {
140 return super.createFieldAndColumnsMap(columnMapping);
141 }
142
143
144
145
146
147
148
149
150 public List<String> createColumnNames() {
151 final List<String> columnNames = new ArrayList<String>();
152 for (final Map.Entry<String, String> entry : columnMapping.entrySet()) {
153 columnNames.add(entry.getKey());
154 }
155 return columnNames;
156 }
157
158 public String getFieldName(final String columnName) {
159 return columnMapping.get(columnName);
160 }
161
162 }