1 package com.github.mygreen.cellformatter.lang;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Locale;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.concurrent.ConcurrentHashMap;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13
14
15
16
17
18
19
20
21
22
23 public class EraResolver {
24
25
26
27
28 private static final MessageResolvergeResolver.html#MessageResolver">MessageResolver messageResolver = new MessageResolver("com.github.mygreen.cellformatter.era", true, true);
29
30
31
32
33 private Map<Locale, Era> eras;
34
35 public EraResolver() {
36 this.eras = new ConcurrentHashMap<>();
37 }
38
39
40
41
42
43
44 public Era getEra(final MSLocale locale) {
45 return locale == null ? Era.UNKNOWN_ERA : getEra(locale.getLocale());
46 }
47
48
49
50
51
52
53 public Era getEra(final Locale locale) {
54
55 if(locale == null) {
56 return Era.UNKNOWN_ERA;
57 }
58
59 if(eras.containsKey(locale)) {
60 return eras.get(locale);
61 }
62
63 MessageResource resource = messageResolver.loadResource(locale);
64
65
66 final Era era;
67 if(resource.isNullObject()) {
68 era = Era.UNKNOWN_ERA;
69 } else {
70 era = createEra(resource);
71 }
72
73 eras.put(locale, era);
74
75 return era;
76
77 }
78
79 private Era createEra(final MessageResource resource) {
80
81
82 Pattern keyPattern = Pattern.compile("^era\\.(.+)\\.name$");
83 Set<String> keyNameSet = new HashSet<>();
84 for(String key : resource.getKeys()) {
85
86 Matcher matcher = keyPattern.matcher(key);
87 if(!matcher.matches()) {
88 continue;
89 }
90
91 String keyName = matcher.group(1);
92 keyNameSet.add(keyName);
93
94 }
95
96
97 List<EraPeriod> periodList = new ArrayList<>();
98 for(String keyName : keyNameSet) {
99
100 EraPeriodtter/lang/EraPeriod.html#EraPeriod">EraPeriod period = new EraPeriod();
101 String name = resource.getMessage(String.format("era.%s.name", keyName));
102 period.setName(name);
103
104 String abbrevName = resource.getMessage(String.format("era.%s.abbrevName", keyName));
105 period.setAbbrevName(abbrevName);
106
107 String abbrevRomanName = resource.getMessage(String.format("era.%s.abbrevRomanName", keyName));
108 period.setAbbrevRomanName(abbrevRomanName);
109
110 String start = resource.getMessage(String.format("era.%s.start", keyName));
111 if(Utils.isNotEmpty(start)) {
112 period.setStartDate(ExcelDateUtils.parseDate(start));
113 }
114
115 String end = resource.getMessage(String.format("era.%s.end", keyName));
116 if(Utils.isNotEmpty(end)) {
117 period.setEndDate(ExcelDateUtils.parseDate(end));
118 }
119
120 periodList.add(period);
121 }
122
123 if(periodList.isEmpty()) {
124 return Era.UNKNOWN_ERA;
125 }
126
127 final Eracellformatter/lang/Era.html#Era">Era era = new Era(periodList);
128 return era;
129
130 }
131
132 }