|
1 | 1 | package com.codedifferently.lesson9.dataprovider;
|
2 | 2 |
|
3 |
| -import java.util.List; |
4 | 3 | import java.util.Map;
|
5 |
| -import java.util.stream.Collectors; |
6 | 4 |
|
7 |
| -public abstract class AmiyahJonesProvider { |
| 5 | +import org.springframework.stereotype.Service; |
8 | 6 |
|
9 |
| - public abstract String getProviderName(); |
10 |
| - |
11 |
| - public abstract Map<String, Class> getColumnTypeByName(); |
12 |
| - |
13 |
| - /** |
14 |
| - * Given a list of data objects, returns the list with values converted to the appropriate type. |
15 |
| - * |
16 |
| - * @param data A list of data objects containing values keyed by column name. |
17 |
| - * @return A new list with object values converted to the appropriate type. |
18 |
| - */ |
19 |
| - public List<Map<String, Object>> parseData(List<Map<String, String>> data) throws Exception { |
20 |
| - Map<String, Class> columnTypeByName = getColumnTypeByName(); |
21 |
| - return data.stream().map(row -> parseRow(columnTypeByName, row)).collect(Collectors.toList()); |
| 7 | +@Service |
| 8 | +public class AmiyahJonesProvider extends DataProvider { |
| 9 | + public String getProviderName() { |
| 10 | + return "amiyahjones"; |
22 | 11 | }
|
23 | 12 |
|
24 |
| - /** |
25 |
| - * Parses a single row of data using the provided column types mapping. |
26 |
| - * |
27 |
| - * @param columnTypeByName A map containing the type of a column keyed by its name. |
28 |
| - * @param row A bag of values keyed by column name. |
29 |
| - * @return A new list of data values converted to the appropriate type. |
30 |
| - */ |
31 |
| - private Map<String, Object> parseRow( |
32 |
| - Map<String, Class> columnTypeByName, Map<String, String> row) { |
33 |
| - var parsedRow = new java.util.HashMap<String, Object>(); |
34 |
| - row.forEach( |
35 |
| - (key, value) -> { |
36 |
| - Class type = columnTypeByName.get(key); |
37 |
| - try { |
38 |
| - parsedRow.put(key, type.getConstructor(String.class).newInstance(value)); |
39 |
| - } catch (Exception e) { |
40 |
| - throw new RuntimeException( |
41 |
| - "Error parsing data for " + key + " as type " + type.getName()); |
42 |
| - } |
43 |
| - }); |
44 |
| - return parsedRow; |
| 13 | + public Map<String, Class> getColumnTypeByName() { |
| 14 | + return Map.of( |
| 15 | + "column1", Integer.class, |
| 16 | + "column2", String.class, |
| 17 | + "column3", Boolean.class, |
| 18 | + "column4", Float.class, |
| 19 | + "column5", Double.class, |
| 20 | + "column6", Long.class, |
| 21 | + "column7", Short.class); |
45 | 22 | }
|
46 | 23 | }
|
0 commit comments