blob: f98a08eaa7237910983fb40771ae36dc56546b02 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
package design.persistence;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import design.model.Course;
import design.model.Hole;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import design.model.course_search.*;
public class CSVMasterDatabase implements MasterDatabase {
private static CSVMasterDatabase INSTANCE;
public static synchronized CSVMasterDatabase instance() {
if (INSTANCE == null) {
INSTANCE = new CSVMasterDatabase("data/golf_courses_1000.csv");
}
return INSTANCE;
}
private final List<Course> cache;
private final CsvMapper mapper;
private final File file;
private CSVMasterDatabase(String filename) {
this.cache = new ArrayList<>();
this.mapper = new CsvMapper();
this.file = new File(filename);
SimpleModule module = new SimpleModule();
module.addDeserializer(Course.class, new CourseDeserializer());
mapper.registerModule(module);
try {
load();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private void load() throws IOException {
MappingIterator<Course> it = mapper
.readerFor(Course.class)
.with(CsvSchema.emptySchema().withHeader())
.readValues(file);
cache.addAll(it.readAll());
}
@Override
public Course[] getCourses() {
return cache.toArray(Course[]::new);
}
@Override
public Course getCourse(int id) {
return cache.get(id);
}
@Override
public CourseList getCourseList() {
CourseList courses = new CourseList();
for (Course c : cache)
{
courses.add(c);
}
return courses;
}
private static class CourseDeserializer extends JsonDeserializer<Course> {
int curID = 0;
@Override
public Course deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
ObjectNode node = p.getCodec().readTree(p);
int id = curID++;
String name = node.get("Course Name").asText();
double difficulty = node.get("Difficulty Rating").asDouble();
String location = node.get("Location").asText();
int holes = node.get("Number of Holes").asInt();
int totalPar = node.get("Total Par").asInt();
String[] parValues = node.get("Par Per Hole").asText().split(";");
List<Hole> holeList = new ArrayList<>();
for (int i = 0; i < parValues.length; i++) {
holeList.add(new Hole(i + 1, Integer.parseInt(parValues[i].trim())));
}
return new Course(id, name, (int) difficulty, location, holes, totalPar, holeList);
}
}
}
|