Skip to content

Commit 7f95364

Browse files
author
AmiyahJo
committed
feat: add provider.java file
feat: create a DateProvider implementation
1 parent 2dd1645 commit 7f95364

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.codedifferently.lesson9.dataprovider;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.stream.Collectors;
6+
7+
public abstract class AmiyahJonesProvider {
8+
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());
22+
}
23+
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;
45+
}
46+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[
2+
{
3+
"column1": "5731499272613477376",
4+
"column2": "2002066591",
5+
"column3": "vo591",
6+
"column4": "false",
7+
"column5": "11686",
8+
"column6": "2.90396E38",
9+
"column7": "7.899162733804963E307"
10+
},
11+
{
12+
"column1": "3629992685213369344",
13+
"column2": "394292213",
14+
"column3": "arxbjv",
15+
"column4": "false",
16+
"column5": "25971",
17+
"column6": "3.3176412E38",
18+
"column7": "1.422747221356426E307"
19+
},
20+
{
21+
"column1": "3634306241562193408",
22+
"column2": "1759402995",
23+
"column3": "rp95ft",
24+
"column4": "true",
25+
"column5": "15517",
26+
"column6": "4.4703475E37",
27+
"column7": "1.5779687820629317E308"
28+
},
29+
{
30+
"column1": "2268654800340631040",
31+
"column2": "893983092",
32+
"column3": "lfk8u",
33+
"column4": "true",
34+
"column5": "2526",
35+
"column6": "2.8990186E38",
36+
"column7": "7.688984742776194E307"
37+
},
38+
{
39+
"column1": "3054712055809444864",
40+
"column2": "1143998344",
41+
"column3": "rxbse3ki",
42+
"column4": "false",
43+
"column5": "24432",
44+
"column6": "2.612388E36",
45+
"column7": "5.274382911630398E307"
46+
},
47+
{
48+
"column1": "1352645309887328256",
49+
"column2": "503833821",
50+
"column3": "qbz6jdowfe",
51+
"column4": "false",
52+
"column5": "7617",
53+
"column6": "1.9916393E38",
54+
"column7": "3.3191786026811584E307"
55+
},
56+
{
57+
"column1": "8637983229365371904",
58+
"column2": "1185216615",
59+
"column3": "9sn7fw2o3q",
60+
"column4": "false",
61+
"column5": "23224",
62+
"column6": "1.4122802E38",
63+
"column7": "1.580014576135557E308"
64+
},
65+
{
66+
"column1": "9067948480444420096",
67+
"column2": "1081656270",
68+
"column3": "7f0ne",
69+
"column4": "false",
70+
"column5": "32102",
71+
"column6": "1.3807303E38",
72+
"column7": "1.2057784493311825E308"
73+
},
74+
{
75+
"column1": "6604084776146575360",
76+
"column2": "1701387671",
77+
"column3": "3v2qgcaetf0",
78+
"column4": "true",
79+
"column5": "14055",
80+
"column6": "1.0123542E38",
81+
"column7": "1.7725263417131191E308"
82+
},
83+
{
84+
"column1": "8522793738323822592",
85+
"column2": "583151778",
86+
"column3": "p4zuv5m",
87+
"column4": "false",
88+
"column5": "4456",
89+
"column6": "2.3024422E37",
90+
"column7": "9.653626411185625E307"
91+
}
92+
]

0 commit comments

Comments
 (0)