Repository navigation
Expand file tree
/
Copy pathAlgorithmRepository.java
More file actions
55 lines (46 loc) · 2.16 KB
/
Copy pathAlgorithmRepository.java
File metadata and controls
55 lines (46 loc) · 2.16 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// src/main/java/com/simulator/analysis/AlgorithmRepository.java
package com.simulator;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
public class AlgorithmRepository {
private static final String DATA_PATH = "/data/algorithms.json";
private static AlgorithmRepository INSTANCE;
private final Map<String, List<AlgorithmRecord>> byCategory;
private AlgorithmRepository(Map<String, List<AlgorithmRecord>> byCategory) {
this.byCategory = byCategory;
}
public static synchronized AlgorithmRepository getInstance() {
if (INSTANCE == null) {
INSTANCE = load();
}
return INSTANCE;
}
private static AlgorithmRepository load() {
try (InputStream in = AlgorithmRepository.class.getResourceAsStream(DATA_PATH)) {
if (in == null) throw new IllegalStateException("Missing data file: " + DATA_PATH);
InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
Type type = new TypeToken<Map<String, List<AlgorithmRecord>>>() {}.getType();
Map<String, List<AlgorithmRecord>> map = new Gson().fromJson(reader, type);
// Normalize null lists to empty
map.replaceAll((k, v) -> v == null ? new ArrayList<>() : v);
return new AlgorithmRepository(map);
} catch (Exception e) {
throw new RuntimeException("Failed to load algorithms.json", e);
}
}
public List<String> getCategories() {
return new ArrayList<>(byCategory.keySet()).stream().sorted().collect(Collectors.toList());
}
public List<AlgorithmRecord> getAlgorithms(String category) {
return new ArrayList<>(byCategory.getOrDefault(category, Collections.emptyList()));
}
public Optional<AlgorithmRecord> find(String category, String name) {
return getAlgorithms(category).stream().filter(a -> a.getName().equals(name)).findFirst();
}
}