-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalDatabaseModel.java
More file actions
32 lines (28 loc) · 1.08 KB
/
LocalDatabaseModel.java
File metadata and controls
32 lines (28 loc) · 1.08 KB
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
import java.io.*;
import java.util.*;
import java.util.stream.*;
import com.google.gson.*;
public final class LocalDatabaseModel implements Model {
private final JsonObject root;
public LocalDatabaseModel() {
try(var reader = new InputStreamReader(Model.class.getResourceAsStream("/dsonames.json"))) {
root = JsonParser.parseReader(reader).getAsJsonObject();
} catch (IOException e) {
throw new IOError(e);
}
}
public List<String> names() {
var result = new ArrayList<String>();
for (var entry : root.entrySet()) {
var planet = entry.getValue().getAsJsonObject();
result.add(planet.get("name").getAsString());
}
return result;
}
public Map<String, String> translatedNames(String language) {
return root.entrySet().stream()
.map(e -> e.getValue().getAsJsonObject())
.filter(p -> p.has(language))
.collect(Collectors.toMap(p -> p.get("name").getAsString(), p -> p.get(language).getAsString(), (a, b) -> a));
}
}