Skip to content

Commit

Permalink
perf: DayOfWeekFormatter
Browse files Browse the repository at this point in the history
Remove properties files for this part already covered by ICU4J
  • Loading branch information
jy95 committed Feb 9, 2025
1 parent 4490e0a commit 65d67d8
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static ResourceBundle selectResourceBundle(Locale locale) {
var bundleControl = new MultiResourceBundleControl(
"translations",
"common",
"daysOfWeek",
"eventTiming",
"quantityComparator"
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.jy95.fds.common.functions;

import com.ibm.icu.text.DateFormatSymbols;
import com.ibm.icu.util.Calendar;

import java.util.Locale;
import java.util.Map;

/**
* Class to format day of week
*/
public final class DayOfWeekFormatter {

/**
* Map each code of <a href="https://build.fhir.org/valueset-days-of-week.html">Day Of Week</a> to their ICU4J values
*/
private static final Map<String, Integer> DAY_MAPPING = Map.ofEntries(
Map.entry("mon", Calendar.MONDAY),
Map.entry("tue", Calendar.TUESDAY),
Map.entry("wed", Calendar.WEDNESDAY),
Map.entry("thu", Calendar.THURSDAY),
Map.entry("fri", Calendar.FRIDAY),
Map.entry("sat", Calendar.SATURDAY),
Map.entry("sun", Calendar.SUNDAY)
);

/**
* Bundle that contains translations
*/
private final String[] longWeekDays;

/**
* Constructor for the formatter
* @param locale The locale to use
*/
public DayOfWeekFormatter(Locale locale) {
longWeekDays = DateFormatSymbols
.getInstance(locale)
.getWeekdays();
}

/**
* Translates a single-day code into its corresponding day of the week in text.
* @param code The <a href="https://build.fhir.org/valueset-days-of-week.html">Day Of Week</a> code to translate
* @return the translated day of the week as a string.
*/
public String codeToLongText(String code) {
return longWeekDays[DAY_MAPPING.get(code)];
}
}
7 changes: 0 additions & 7 deletions common/src/main/resources/daysOfWeek_de.properties

This file was deleted.

7 changes: 0 additions & 7 deletions common/src/main/resources/daysOfWeek_en.properties

This file was deleted.

7 changes: 0 additions & 7 deletions common/src/main/resources/daysOfWeek_fr.properties

This file was deleted.

7 changes: 0 additions & 7 deletions common/src/main/resources/daysOfWeek_nl.properties

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.jy95.fds.r4.translators;

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

/**
* The configuration object used by this API.
* Day of week formatter
*/
private final FDSConfigR4 config;
private final DayOfWeekFormatter dayOfWeekFormatter;

/**
* The resource bundle containing localized strings for translation.
Expand All @@ -40,9 +41,9 @@ public class DayOfWeekR4 implements DayOfWeek<FDSConfigR4, Dosage> {
* @param bundle a {@link java.util.ResourceBundle} object
*/
public DayOfWeekR4(FDSConfigR4 config, ResourceBundle bundle) {
this.config = config;
this.bundle = bundle;
this.dayOfWeekMsg = getDayOfWeekMsg(bundle, config.getLocale());
this.dayOfWeekFormatter = new DayOfWeekFormatter(config.getLocale());
}

/** {@inheritDoc} */
Expand All @@ -58,34 +59,17 @@ public CompletableFuture<String> convert(Dosage dosage) {
var dayOfWeeks = dosage.getTiming().getRepeat().getDayOfWeek();
var dayOfWeeksCodes = dayOfWeeks
.stream()
.map(day -> {
String dayCode = day.getCode().toLowerCase(); // Get the lowercase day code
return dayToText(dayCode);
})
.map(day -> dayOfWeekFormatter
.codeToLongText(
day.getCode().toLowerCase()
)
)
.collect(Collectors.toList());

return daysToText(dayOfWeeksCodes);
});
}

/**
* Translates a single-day code into its corresponding day of the week in text.
*
* @param dayCode the code representing the day (e.g., "mon", "tue").
* @return the translated day of the week as a string.
*/
private String dayToText(String dayCode) {
String dayTranslation = bundle.getString("day." + dayCode);

// Use ICU's MessageFormat to handle the translation with choice formatting
MessageFormat messageFormat = new MessageFormat(dayTranslation, config.getLocale());
Map<String, Object> dayArguments = Map.of(
"dayType", "long"
);

return messageFormat.format(dayArguments);
}

/** {@inheritDoc} */
@Override
public boolean hasTiming(Dosage dosage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.jy95.fds.r5.translators;

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

/**
* The configuration object used by this API.
* Day of week formatter
*/
private final FDSConfigR5 config;
private final DayOfWeekFormatter dayOfWeekFormatter;

/**
* The resource bundle containing localized strings for translation.
Expand All @@ -40,9 +41,9 @@ public class DayOfWeekR5 implements DayOfWeek<FDSConfigR5, Dosage> {
* @param bundle a {@link java.util.ResourceBundle} object
*/
public DayOfWeekR5(FDSConfigR5 config, ResourceBundle bundle) {
this.config = config;
this.bundle = bundle;
this.dayOfWeekMsg = getDayOfWeekMsg(bundle, config.getLocale());
this.dayOfWeekFormatter = new DayOfWeekFormatter(config.getLocale());
}

/** {@inheritDoc} */
Expand All @@ -58,34 +59,17 @@ public CompletableFuture<String> convert(Dosage dosage) {
var dayOfWeeks = dosage.getTiming().getRepeat().getDayOfWeek();
var dayOfWeeksCodes = dayOfWeeks
.stream()
.map(day -> {
String dayCode = day.getCode().toLowerCase(); // Get the lowercase day code
return dayToText(dayCode);
})
.map(day -> dayOfWeekFormatter
.codeToLongText(
day.getCode().toLowerCase()
)
)
.collect(Collectors.toList());

return daysToText(dayOfWeeksCodes);
});
}

/**
* Translates a single-day code into its corresponding day of the week in text.
*
* @param dayCode the code representing the day (e.g., "mon", "tue").
* @return the translated day of the week as a string.
*/
private String dayToText(String dayCode) {
String dayTranslation = bundle.getString("day." + dayCode);

// Use ICU's MessageFormat to handle the translation with choice formatting
MessageFormat messageFormat = new MessageFormat(dayTranslation, config.getLocale());
Map<String, Object> dayArguments = Map.of(
"dayType", "long"
);

return messageFormat.format(dayArguments);
}

/** {@inheritDoc} */
@Override
public boolean hasTiming(Dosage dosage) {
Expand Down

0 comments on commit 65d67d8

Please sign in to comment.