|
6 | 6 | import java.util.List; |
7 | 7 | import java.util.Map; |
8 | 8 |
|
9 | | -public class CategoryDescriptionStore { |
10 | | - private final Map<Integer, AlertTranslation> catIdToDescription; |
11 | | - private final Map<Integer, AlertTranslation> matIdToDescription; |
12 | | - |
13 | | - public CategoryDescriptionStore(List<AlertTranslation> alertDescriptionList) { |
14 | | - catIdToDescription = new HashMap<>(); |
15 | | - matIdToDescription = new HashMap<>(); |
16 | | - for (AlertTranslation alertTranslation : alertDescriptionList) { |
17 | | - catIdToDescription.put(alertTranslation.catId(), alertTranslation); |
18 | | - matIdToDescription.put(alertTranslation.matrixCatId(), alertTranslation); |
| 9 | +import static com.github.pyckle.oref.integration.translationstores.CategoryType.CATEGORY; |
| 10 | +import static com.github.pyckle.oref.integration.translationstores.CategoryType.MATRIX; |
| 11 | + |
| 12 | +public class CategoryDescriptionStore |
| 13 | +{ |
| 14 | + private final Map<CategoryKey, AlertCategoryStore> matIdToDescription; |
| 15 | + |
| 16 | + public CategoryDescriptionStore(List<AlertTranslation> alertDescriptionList) |
| 17 | + { |
| 18 | + Map<CategoryKey, AlertCategoryStore.Builder> builders = new HashMap<>(); |
| 19 | + for (AlertTranslation alertTranslation : alertDescriptionList) |
| 20 | + { |
| 21 | + builders.computeIfAbsent(new CategoryKey(CATEGORY, alertTranslation.catId()), |
| 22 | + k -> new AlertCategoryStore.Builder(k.categoryType(), k.id())) |
| 23 | + .addAlertTranslation(alertTranslation); |
| 24 | + builders.computeIfAbsent(new CategoryKey(MATRIX, alertTranslation.matrixCatId()), |
| 25 | + k -> new AlertCategoryStore.Builder(k.categoryType(), k.id())) |
| 26 | + .addAlertTranslation(alertTranslation); |
19 | 27 | } |
| 28 | + matIdToDescription = Map.ofEntries( |
| 29 | + builders.entrySet().stream().map(e -> Map.entry(e.getKey(), e.getValue().build())) |
| 30 | + .toArray(Map.Entry[]::new)); |
20 | 31 | } |
21 | 32 |
|
22 | | - public String getAlertStringFromCatId(String lang, int cat, String defaultVal) { |
23 | | - return labelOrDefault(lang, defaultVal, catIdToDescription.get(cat)); |
| 33 | + public String getAlertStringFromCatId(String lang, int cat, String defaultVal) |
| 34 | + { |
| 35 | + return labelOrDefault(lang, defaultVal, matIdToDescription.get(new CategoryKey(CATEGORY, cat))); |
24 | 36 | } |
25 | 37 |
|
26 | | - public String getAlertStringFromMatId(String lang, String cat, String defaultVal) { |
| 38 | + public String getAlertStringFromMatId(String lang, String cat, String defaultVal) |
| 39 | + { |
27 | 40 | if (isValidInteger(cat)) |
28 | 41 | return defaultVal; |
29 | 42 | return getAlertStringFromMatId(lang, Integer.parseInt(cat), defaultVal); |
30 | 43 | } |
31 | 44 |
|
32 | | - private static boolean isValidInteger(String cat) { |
| 45 | + private static boolean isValidInteger(String cat) |
| 46 | + { |
33 | 47 | if (cat.length() > 8) |
34 | 48 | return true; |
35 | | - for (int i = 0; i < cat.length(); i++) { |
| 49 | + for (int i = 0; i < cat.length(); i++) |
| 50 | + { |
36 | 51 | if (cat.charAt(i) < '0' || cat.charAt(i) > '9') |
37 | 52 | return true; |
38 | 53 | } |
39 | 54 | return false; |
40 | 55 | } |
41 | 56 |
|
42 | | - public String getAlertStringFromMatId(String lang, int cat, String defaultVal) { |
43 | | - return labelOrDefault(lang, defaultVal, matIdToDescription.get(cat)); |
| 57 | + public String getAlertStringFromMatId(String lang, int cat, String defaultVal) |
| 58 | + { |
| 59 | + return labelOrDefault(lang, defaultVal, matIdToDescription.get(new CategoryKey(MATRIX, cat))); |
44 | 60 | } |
45 | 61 |
|
46 | | - private static String labelOrDefault(String lang, String defaultVal, AlertTranslation alertTranslation) { |
| 62 | + private static String labelOrDefault(String lang, String defaultVal, AlertCategoryStore alertCategoryStore) |
| 63 | + { |
| 64 | + var alertTranslation = alertCategoryStore == null ? null : alertCategoryStore.getAlertTranslation(defaultVal); |
47 | 65 | if (alertTranslation == null) |
48 | 66 | return defaultVal; |
49 | 67 | return alertTranslation.title(lang, defaultVal); |
50 | 68 | } |
| 69 | + |
| 70 | + record CategoryKey(CategoryType categoryType, int id) |
| 71 | + { |
| 72 | + } |
51 | 73 | } |
0 commit comments