Skip to content

Commit 547ddfd

Browse files
authored
perf: DayOfWeekFormatter (#52)
Remove properties files for this part already covered by ICU4J
1 parent 4490e0a commit 547ddfd

File tree

8 files changed

+68
-79
lines changed

8 files changed

+68
-79
lines changed

common/src/main/java/io/github/jy95/fds/common/config/DefaultImplementations.java

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public static ResourceBundle selectResourceBundle(Locale locale) {
2727
var bundleControl = new MultiResourceBundleControl(
2828
"translations",
2929
"common",
30-
"daysOfWeek",
3130
"eventTiming",
3231
"quantityComparator"
3332
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.jy95.fds.common.functions;
2+
3+
import com.ibm.icu.text.DateFormatSymbols;
4+
import com.ibm.icu.util.Calendar;
5+
6+
import java.util.Locale;
7+
import java.util.Map;
8+
9+
/**
10+
* Class to format day of week
11+
*/
12+
public final class DayOfWeekFormatter {
13+
14+
/**
15+
* Map each code of <a href="https://build.fhir.org/valueset-days-of-week.html">Day Of Week</a> to their ICU4J values
16+
*/
17+
private static final Map<String, Integer> DAY_MAPPING = Map.ofEntries(
18+
Map.entry("mon", Calendar.MONDAY),
19+
Map.entry("tue", Calendar.TUESDAY),
20+
Map.entry("wed", Calendar.WEDNESDAY),
21+
Map.entry("thu", Calendar.THURSDAY),
22+
Map.entry("fri", Calendar.FRIDAY),
23+
Map.entry("sat", Calendar.SATURDAY),
24+
Map.entry("sun", Calendar.SUNDAY)
25+
);
26+
27+
/**
28+
* Bundle that contains translations
29+
*/
30+
private final String[] longWeekDays;
31+
32+
/**
33+
* Constructor for the formatter
34+
* @param locale The locale to use
35+
*/
36+
public DayOfWeekFormatter(Locale locale) {
37+
longWeekDays = DateFormatSymbols
38+
.getInstance(locale)
39+
.getWeekdays();
40+
}
41+
42+
/**
43+
* Translates a single-day code into its corresponding day of the week in text.
44+
* @param code The <a href="https://build.fhir.org/valueset-days-of-week.html">Day Of Week</a> code to translate
45+
* @return the translated day of the week as a string.
46+
*/
47+
public String codeToLongText(String code) {
48+
return longWeekDays[DAY_MAPPING.get(code)];
49+
}
50+
}

common/src/main/resources/daysOfWeek_de.properties

-7
This file was deleted.

common/src/main/resources/daysOfWeek_en.properties

-7
This file was deleted.

common/src/main/resources/daysOfWeek_fr.properties

-7
This file was deleted.

common/src/main/resources/daysOfWeek_nl.properties

-7
This file was deleted.

r4/src/main/java/io/github/jy95/fds/r4/translators/DayOfWeekR4.java

+9-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.jy95.fds.r4.translators;
22

33
import com.ibm.icu.text.MessageFormat;
4+
import io.github.jy95.fds.common.functions.DayOfWeekFormatter;
45
import io.github.jy95.fds.common.functions.ListToString;
56
import io.github.jy95.fds.common.translators.DayOfWeek;
67
import io.github.jy95.fds.r4.config.FDSConfigR4;
@@ -24,9 +25,9 @@ public class DayOfWeekR4 implements DayOfWeek<FDSConfigR4, Dosage> {
2425
protected final MessageFormat dayOfWeekMsg;
2526

2627
/**
27-
* The configuration object used by this API.
28+
* Day of week formatter
2829
*/
29-
private final FDSConfigR4 config;
30+
private final DayOfWeekFormatter dayOfWeekFormatter;
3031

3132
/**
3233
* The resource bundle containing localized strings for translation.
@@ -40,9 +41,9 @@ public class DayOfWeekR4 implements DayOfWeek<FDSConfigR4, Dosage> {
4041
* @param bundle a {@link java.util.ResourceBundle} object
4142
*/
4243
public DayOfWeekR4(FDSConfigR4 config, ResourceBundle bundle) {
43-
this.config = config;
4444
this.bundle = bundle;
4545
this.dayOfWeekMsg = getDayOfWeekMsg(bundle, config.getLocale());
46+
this.dayOfWeekFormatter = new DayOfWeekFormatter(config.getLocale());
4647
}
4748

4849
/** {@inheritDoc} */
@@ -58,34 +59,17 @@ public CompletableFuture<String> convert(Dosage dosage) {
5859
var dayOfWeeks = dosage.getTiming().getRepeat().getDayOfWeek();
5960
var dayOfWeeksCodes = dayOfWeeks
6061
.stream()
61-
.map(day -> {
62-
String dayCode = day.getCode().toLowerCase(); // Get the lowercase day code
63-
return dayToText(dayCode);
64-
})
62+
.map(day -> dayOfWeekFormatter
63+
.codeToLongText(
64+
day.getCode().toLowerCase()
65+
)
66+
)
6567
.collect(Collectors.toList());
6668

6769
return daysToText(dayOfWeeksCodes);
6870
});
6971
}
7072

71-
/**
72-
* Translates a single-day code into its corresponding day of the week in text.
73-
*
74-
* @param dayCode the code representing the day (e.g., "mon", "tue").
75-
* @return the translated day of the week as a string.
76-
*/
77-
private String dayToText(String dayCode) {
78-
String dayTranslation = bundle.getString("day." + dayCode);
79-
80-
// Use ICU's MessageFormat to handle the translation with choice formatting
81-
MessageFormat messageFormat = new MessageFormat(dayTranslation, config.getLocale());
82-
Map<String, Object> dayArguments = Map.of(
83-
"dayType", "long"
84-
);
85-
86-
return messageFormat.format(dayArguments);
87-
}
88-
8973
/** {@inheritDoc} */
9074
@Override
9175
public boolean hasTiming(Dosage dosage) {

r5/src/main/java/io/github/jy95/fds/r5/translators/DayOfWeekR5.java

+9-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.jy95.fds.r5.translators;
22

33
import com.ibm.icu.text.MessageFormat;
4+
import io.github.jy95.fds.common.functions.DayOfWeekFormatter;
45
import io.github.jy95.fds.common.functions.ListToString;
56
import io.github.jy95.fds.common.translators.DayOfWeek;
67
import io.github.jy95.fds.r5.config.FDSConfigR5;
@@ -24,9 +25,9 @@ public class DayOfWeekR5 implements DayOfWeek<FDSConfigR5, Dosage> {
2425
protected final MessageFormat dayOfWeekMsg;
2526

2627
/**
27-
* The configuration object used by this API.
28+
* Day of week formatter
2829
*/
29-
private final FDSConfigR5 config;
30+
private final DayOfWeekFormatter dayOfWeekFormatter;
3031

3132
/**
3233
* The resource bundle containing localized strings for translation.
@@ -40,9 +41,9 @@ public class DayOfWeekR5 implements DayOfWeek<FDSConfigR5, Dosage> {
4041
* @param bundle a {@link java.util.ResourceBundle} object
4142
*/
4243
public DayOfWeekR5(FDSConfigR5 config, ResourceBundle bundle) {
43-
this.config = config;
4444
this.bundle = bundle;
4545
this.dayOfWeekMsg = getDayOfWeekMsg(bundle, config.getLocale());
46+
this.dayOfWeekFormatter = new DayOfWeekFormatter(config.getLocale());
4647
}
4748

4849
/** {@inheritDoc} */
@@ -58,34 +59,17 @@ public CompletableFuture<String> convert(Dosage dosage) {
5859
var dayOfWeeks = dosage.getTiming().getRepeat().getDayOfWeek();
5960
var dayOfWeeksCodes = dayOfWeeks
6061
.stream()
61-
.map(day -> {
62-
String dayCode = day.getCode().toLowerCase(); // Get the lowercase day code
63-
return dayToText(dayCode);
64-
})
62+
.map(day -> dayOfWeekFormatter
63+
.codeToLongText(
64+
day.getCode().toLowerCase()
65+
)
66+
)
6567
.collect(Collectors.toList());
6668

6769
return daysToText(dayOfWeeksCodes);
6870
});
6971
}
7072

71-
/**
72-
* Translates a single-day code into its corresponding day of the week in text.
73-
*
74-
* @param dayCode the code representing the day (e.g., "mon", "tue").
75-
* @return the translated day of the week as a string.
76-
*/
77-
private String dayToText(String dayCode) {
78-
String dayTranslation = bundle.getString("day." + dayCode);
79-
80-
// Use ICU's MessageFormat to handle the translation with choice formatting
81-
MessageFormat messageFormat = new MessageFormat(dayTranslation, config.getLocale());
82-
Map<String, Object> dayArguments = Map.of(
83-
"dayType", "long"
84-
);
85-
86-
return messageFormat.format(dayArguments);
87-
}
88-
8973
/** {@inheritDoc} */
9074
@Override
9175
public boolean hasTiming(Dosage dosage) {

0 commit comments

Comments
 (0)