1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.io;
17
18 import java.io.IOException;
19 import java.io.Reader;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.supercsv.cellprocessor.ift.CellProcessor;
26 import org.supercsv.prefs.CsvPreference;
27 import org.supercsv.util.Util;
28
29
30
31
32
33
34
35
36 public class CsvMapReader extends AbstractCsvReader implements ICsvMapReader {
37
38
39
40
41
42
43
44
45
46
47
48
49 public CsvMapReader(final Reader reader, final CsvPreference preferences) {
50 super(reader, preferences);
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64 public CsvMapReader(final ITokenizer tokenizer, final CsvPreference preferences) {
65 super(tokenizer, preferences);
66 }
67
68
69
70
71 public Map<String, String> read(final String... nameMapping) throws IOException {
72
73 if( nameMapping == null ) {
74 throw new NullPointerException("nameMapping should not be null");
75 }
76
77 if( readRow() ) {
78 final Map<String, String> destination = new HashMap<String, String>();
79 Util.filterListToMap(destination, nameMapping, getColumns());
80 return destination;
81 }
82
83 return null;
84 }
85
86
87
88
89 public Map<String, Object> read(final String[] nameMapping, final CellProcessor[] processors) throws IOException {
90
91 if( nameMapping == null ) {
92 throw new NullPointerException("nameMapping should not be null");
93 } else if( processors == null ) {
94 throw new NullPointerException("processors should not be null");
95 }
96
97 if( readRow() ) {
98
99 final List<Object> processedColumns = executeProcessors(new ArrayList<Object>(getColumns().size()),
100 processors);
101
102
103 final Map<String, Object> destination = new HashMap<String, Object>(processedColumns.size());
104 Util.filterListToMap((Map<String, Object>) destination, nameMapping, (List<Object>) processedColumns);
105 return destination;
106 }
107
108 return null;
109 }
110 }