1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.util;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.supercsv.cellprocessor.ift.CellProcessor;
23 import org.supercsv.exception.SuperCsvConstraintViolationException;
24 import org.supercsv.exception.SuperCsvException;
25
26
27
28
29
30
31
32 public final class Util {
33
34
35 private Util() {
36 }
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public static void executeCellProcessors(final List<Object> destination, final List<?> source,
63 final CellProcessor[] processors, final int lineNo, final int rowNo) {
64
65 if( destination == null ) {
66 throw new NullPointerException("destination should not be null");
67 } else if( source == null ) {
68 throw new NullPointerException("source should not be null");
69 } else if( processors == null ) {
70 throw new NullPointerException("processors should not be null");
71 }
72
73
74 final CsvContext context = new CsvContext(lineNo, rowNo, 1);
75 context.setRowSource(new ArrayList<Object>(source));
76
77 if( source.size() != processors.length ) {
78 throw new SuperCsvException(String.format(
79 "The number of columns to be processed (%d) must match the number of CellProcessors (%d): check that the number"
80 + " of CellProcessors you have defined matches the expected number of columns being read/written",
81 source.size(), processors.length), context);
82 }
83
84 destination.clear();
85
86 for( int i = 0; i < source.size(); i++ ) {
87
88 context.setColumnNumber(i + 1);
89
90 if( processors[i] == null ) {
91 destination.add(source.get(i));
92 } else {
93 destination.add(processors[i].execute(source.get(i), context));
94 }
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public static <T> void filterListToMap(final Map<String, T> destinationMap, final String[] nameMapping,
115 final List<? extends T> sourceList) {
116 if( destinationMap == null ) {
117 throw new NullPointerException("destinationMap should not be null");
118 } else if( nameMapping == null ) {
119 throw new NullPointerException("nameMapping should not be null");
120 } else if( sourceList == null ) {
121 throw new NullPointerException("sourceList should not be null");
122 } else if( nameMapping.length != sourceList.size() ) {
123 throw new SuperCsvException(
124 String
125 .format(
126 "the nameMapping array and the sourceList should be the same size (nameMapping length = %d, sourceList size = %d)",
127 nameMapping.length, sourceList.size()));
128 }
129
130 destinationMap.clear();
131
132 for( int i = 0; i < nameMapping.length; i++ ) {
133 final String key = nameMapping[i];
134
135 if( key == null ) {
136 continue;
137 }
138
139
140 if( destinationMap.containsKey(key) ) {
141 throw new SuperCsvException(String.format("duplicate nameMapping '%s' at index %d", key, i));
142 }
143
144 destinationMap.put(key, sourceList.get(i));
145 }
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159 public static List<Object> filterMapToList(final Map<String, ?> map, final String[] nameMapping) {
160 if( map == null ) {
161 throw new NullPointerException("map should not be null");
162 } else if( nameMapping == null ) {
163 throw new NullPointerException("nameMapping should not be null");
164 }
165
166 final List<Object> result = new ArrayList<Object>(nameMapping.length);
167 for( final String key : nameMapping ) {
168 result.add(map.get(key));
169 }
170 return result;
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184 public static Object[] filterMapToObjectArray(final Map<String, ?> values, final String[] nameMapping) {
185
186 if( values == null ) {
187 throw new NullPointerException("values should not be null");
188 } else if( nameMapping == null ) {
189 throw new NullPointerException("nameMapping should not be null");
190 }
191
192 final Object[] targetArray = new Object[nameMapping.length];
193 int i = 0;
194 for( final String name : nameMapping ) {
195 targetArray[i++] = values.get(name);
196 }
197 return targetArray;
198 }
199
200
201
202
203
204
205
206
207 public static String[] objectArrayToStringArray(final Object[] objectArray) {
208 if( objectArray == null ) {
209 return null;
210 }
211
212 final String[] stringArray = new String[objectArray.length];
213 for( int i = 0; i < objectArray.length; i++ ) {
214 stringArray[i] = objectArray[i] != null ? objectArray[i].toString() : null;
215 }
216
217 return stringArray;
218 }
219
220
221
222
223
224
225
226
227 public static String[] objectListToStringArray(final List<?> objectList) {
228 if( objectList == null ) {
229 return null;
230 }
231
232 final String[] stringArray = new String[objectList.size()];
233 for( int i = 0; i < objectList.size(); i++ ) {
234 stringArray[i] = objectList.get(i) != null ? objectList.get(i).toString() : null;
235 }
236
237 return stringArray;
238 }
239
240 }