Skip to content

Commit d82f060

Browse files
committed
feat: adds/Customized/DataTypesHW09
1 parent d308af7 commit d82f060

File tree

4 files changed

+33
-56
lines changed

4 files changed

+33
-56
lines changed
Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,5 @@
11
package com.codedifferently.lesson9.dataprovider;
22

3-
import java.util.List;
4-
import java.util.Map;
5-
import java.util.stream.Collectors;
3+
public class DataProvider {
64

7-
/**
8-
* This class is a base class for data providers. It provides a method for parsing data from a list
9-
* of maps.
10-
*/
11-
public abstract class DataProvider {
12-
/** Returns the name of the provider. */
13-
public abstract String getProviderName();
14-
15-
/** Gets a map of column types keyed by column name. */
16-
public abstract Map<String, Class> getColumnTypeByName();
17-
18-
/**
19-
* Given a list of data objects, returns the list with values converted to the appropriate type.
20-
*
21-
* @param data A list of data objects containing values keyed by column name.
22-
* @return A new list with object values converted to the appropriate type.
23-
*/
24-
public List<Map<String, Object>> parseData(List<Map<String, String>> data) throws Exception {
25-
Map<String, Class> columnTypeByName = getColumnTypeByName();
26-
return data.stream().map(row -> parseRow(columnTypeByName, row)).collect(Collectors.toList());
27-
}
28-
29-
/**
30-
* Parses a single row of data using the provided column types mapping.
31-
*
32-
* @param columnTypeByName A map containing the type of a column keyed by its name.
33-
* @param row A bag of values keyed by column name.
34-
* @return A new list of data values converted to the appropriate type.
35-
*/
36-
private Map<String, Object> parseRow(
37-
Map<String, Class> columnTypeByName, Map<String, String> row) {
38-
var parsedRow = new java.util.HashMap<String, Object>();
39-
row.forEach(
40-
(key, value) -> {
41-
Class type = columnTypeByName.get(key);
42-
try {
43-
parsedRow.put(key, type.getConstructor(String.class).newInstance(value));
44-
} catch (Exception e) {
45-
throw new RuntimeException(
46-
"Error parsing data for " + key + " as type " + type.getName());
47-
}
48-
});
49-
return parsedRow;
50-
}
515
}

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/NileJackProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public String getProviderName() {
1010
return "nilejack";
1111
}
1212

13+
@SuppressWarnings("rawtypes")
1314
public Map<String, Class> getColumnTypeByName() {
1415
return Map.of(
1516
"column1", Float.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.codedifferently.lesson9.dataprovider;
2+
3+
import java.util.Map;
4+
5+
import org.springframework.stereotype.Service;
6+
7+
@Service
8+
public class NileJackProvider extends DataProvider {
9+
public String getProviderName() {
10+
return "nilejack";
11+
}
12+
13+
public Map<String, Class> getColumnTypeByName() {
14+
return Map.of(
15+
"column1", Float.class,
16+
"column2", Integer.class,
17+
"column3", Short.class,
18+
"column4", String.class,
19+
"column5", Boolean.class,
20+
"column6", Double.class,
21+
"column7", Long.class);
22+
}
23+
}

lesson_09/types/types_app/src/main/resources/data/nilejack.json

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
[
2-
package com.code
1+
[
32
{
4-
"column1", Float.class,
5-
"column2", Integer.class,
6-
"column3", Short.class ,
7-
"column4", String.class ,
8-
"column5", Boolean.class,
9-
"column6", Double.class ,
10-
"column7", Long.class ,
3+
"column1": "2.788476E38",
4+
"column2": "23906",
5+
"column3": "1.951334947469109E306",
6+
"column4": "4vynlcbme89",
7+
"column5": "true",
8+
"column6": "1946511983",
9+
"column7": "871716420668780160"
1110
},
1211
{
1312
"column1": "2.9218724E38",

0 commit comments

Comments
 (0)