Skip to content

Commit

Permalink
perf: UnitsOfTimeFormatter instead of properties files
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Feb 9, 2025
1 parent a6de24c commit 06b8b1d
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -73,12 +74,11 @@ default CompletableFuture<String> convert(ResourceBundle bundle, C config, Q qua
/**
* Provides enhanced logic for converting units to a human-readable string.
*
* @param bundle The resource bundle for localization.
* @param config The configuration object for additional logic.
* @param quantity The quantity object.
* @return A CompletableFuture that resolves to the human-readable string for the unit.
*/
CompletableFuture<String> enhancedFromUnitToString(ResourceBundle bundle, C config, Q quantity);
CompletableFuture<String> enhancedFromUnitToString(C config, Q quantity);

/**
* Converts the comparator of a quantity to a human-readable string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,6 @@ default MessageFormat getPeriodMsg(ResourceBundle bundle, Locale locale) {
return new MessageFormat(msg, locale);
}

/**
* Retrieves the localized unit name for the given period unit and amount.
*
* @param bundle The bundle to extract the key
* @param periodUnit The unit code of the period (e.g., "d", "h").
* @param amount The quantity associated with the period unit.
* @return A localized string representing the unit.
*/
default String getUnit(ResourceBundle bundle, String periodUnit, BigDecimal amount) {
var unitMsg = bundle.getString("withoutCount." + periodUnit);
return MessageFormat.format(unitMsg, amount);
}

/** {@inheritDoc} */
@Override
default CompletableFuture<String> convert(D dosage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ibm.icu.text.MessageFormat;
import io.github.jy95.fds.common.functions.QuantityToString;
import io.github.jy95.fds.common.functions.UnitsOfTimeFormatter;
import io.github.jy95.fds.r4.config.FDSConfigR4;
import org.hl7.fhir.r4.model.Quantity;

Expand Down Expand Up @@ -48,14 +49,13 @@ public BigDecimal getValue(Quantity quantity) {

/** {@inheritDoc} */
@Override
public CompletableFuture<String> enhancedFromUnitToString(ResourceBundle bundle, FDSConfigR4 config, Quantity quantity) {
public CompletableFuture<String> enhancedFromUnitToString(FDSConfigR4 config, Quantity quantity) {
// Duration units are built-in supported
if (quantity.hasSystem() && quantity.hasCode() && TIME_SYSTEMS.contains(quantity.getSystem())) {
return CompletableFuture.supplyAsync(() -> {
String code = quantity.getCode();
BigDecimal amount = quantity.hasValue() ? quantity.getValue() : BigDecimal.ONE;
String message = bundle.getString("withoutCount." + code);
return MessageFormat.format(message, amount);
return UnitsOfTimeFormatter.formatWithoutCount(config.getLocale(), code, amount);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public CompletableFuture<String> getUnitText(ResourceBundle bundle, FDSConfigR4
return QuantityToStringR4
.getInstance()
.enhancedFromUnitToString(
bundle,
config,
(hasHigh) ? range.getHigh() : range.getLow()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public CompletableFuture<String> convertDenominator(ResourceBundle bundle, FDSCo
if (BigDecimal.ONE.equals(denominatorValue)) {
return QuantityToStringR4
.getInstance()
.enhancedFromUnitToString(bundle, config, denominator);
.enhancedFromUnitToString(config, denominator);
}

return QuantityToStringR4
Expand Down
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.UnitsOfTimeFormatter;
import io.github.jy95.fds.common.translators.PeriodPeriodMax;
import io.github.jy95.fds.r4.config.FDSConfigR4;
import org.hl7.fhir.r4.model.Dosage;
Expand All @@ -23,9 +24,9 @@ public class PeriodPeriodMaxR4 implements PeriodPeriodMax<FDSConfigR4, Dosage> {
protected final MessageFormat periodMsg;

/**
* The resource bundle containing localized strings for translation.
* The configuration object used by this API.
*/
private final ResourceBundle bundle;
private final FDSConfigR4 config;

/**
* Constructor for {@code PeriodPeriodMaxR4}.
Expand All @@ -34,7 +35,7 @@ public class PeriodPeriodMaxR4 implements PeriodPeriodMax<FDSConfigR4, Dosage> {
* @param bundle a {@link java.util.ResourceBundle} object
*/
public PeriodPeriodMaxR4(FDSConfigR4 config, ResourceBundle bundle) {
this.bundle = bundle;
this.config = config;
this.periodMaxMsg = getPeriodMaxMsg(bundle, config.getLocale());
this.periodMsg = getPeriodMsg(bundle, config.getLocale());
}
Expand Down Expand Up @@ -73,7 +74,7 @@ public String turnPeriodAndPeriodMaxToString(Dosage dosage) {
var periodMin = repeat.getPeriod();
var periodUnit = repeat.getPeriodUnit().toCode();

var unitText = getUnit(bundle, periodUnit, periodMax);
var unitText = UnitsOfTimeFormatter.formatWithoutCount(config.getLocale(), periodUnit, periodMax);
return formatPeriodAndPeriodMaxText(periodMin, periodMax, unitText);
}

Expand All @@ -85,7 +86,7 @@ public String turnPeriodToString(Dosage dosage) {
var period = repeat.getPeriod();
var periodUnit = repeat.getPeriodUnit().toCode();

var unitText = getUnit(bundle, periodUnit, period);
var unitText = UnitsOfTimeFormatter.formatWithoutCount(config.getLocale(), periodUnit, period);
return formatPeriodText(period, unitText);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.ibm.icu.text.MessageFormat;
import io.github.jy95.fds.common.functions.QuantityToString;
import io.github.jy95.fds.common.functions.UnitsOfTimeFormatter;
import io.github.jy95.fds.r5.config.FDSConfigR5;
import org.hl7.fhir.r5.model.Quantity;

import java.math.BigDecimal;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -48,14 +50,13 @@ public BigDecimal getValue(Quantity quantity) {

/** {@inheritDoc} */
@Override
public CompletableFuture<String> enhancedFromUnitToString(ResourceBundle bundle, FDSConfigR5 config, Quantity quantity) {
public CompletableFuture<String> enhancedFromUnitToString(FDSConfigR5 config, Quantity quantity) {
// Duration units are built-in supported
if (quantity.hasSystem() && quantity.hasCode() && TIME_SYSTEMS.contains(quantity.getSystem())) {
return CompletableFuture.supplyAsync(() -> {
String code = quantity.getCode();
BigDecimal amount = quantity.hasValue() ? quantity.getValue() : BigDecimal.ONE;
String message = bundle.getString("withoutCount." + code);
return MessageFormat.format(message, amount);
return UnitsOfTimeFormatter.formatWithoutCount(config.getLocale(), code, amount);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public CompletableFuture<String> getUnitText(ResourceBundle bundle, FDSConfigR5
return QuantityToStringR5
.getInstance()
.enhancedFromUnitToString(
bundle,
config,
(hasHigh) ? range.getHigh() : range.getLow()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public CompletableFuture<String> convertDenominator(ResourceBundle bundle, FDSCo
if (BigDecimal.ONE.equals(denominatorValue)) {
return QuantityToStringR5
.getInstance()
.enhancedFromUnitToString(bundle, config, denominator);
.enhancedFromUnitToString(config, denominator);
}

return QuantityToStringR5
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.UnitsOfTimeFormatter;
import io.github.jy95.fds.common.translators.PeriodPeriodMax;
import io.github.jy95.fds.r5.config.FDSConfigR5;
import org.hl7.fhir.r5.model.Dosage;
Expand All @@ -23,9 +24,9 @@ public class PeriodPeriodMaxR5 implements PeriodPeriodMax<FDSConfigR5, Dosage> {
protected final MessageFormat periodMsg;

/**
* The resource bundle containing localized strings for translation.
* The configuration object used by this API.
*/
private final ResourceBundle bundle;
private final FDSConfigR5 config;

/**
* Constructor for {@code PeriodPeriodMaxR5}.
Expand All @@ -34,7 +35,7 @@ public class PeriodPeriodMaxR5 implements PeriodPeriodMax<FDSConfigR5, Dosage> {
* @param bundle a {@link java.util.ResourceBundle} object
*/
public PeriodPeriodMaxR5(FDSConfigR5 config, ResourceBundle bundle) {
this.bundle = bundle;
this.config = config;
this.periodMaxMsg = getPeriodMaxMsg(bundle, config.getLocale());
this.periodMsg = getPeriodMsg(bundle, config.getLocale());
}
Expand Down Expand Up @@ -73,7 +74,7 @@ public String turnPeriodAndPeriodMaxToString(Dosage dosage) {
var periodMin = repeat.getPeriod();
var periodUnit = repeat.getPeriodUnit().toCode();

var unitText = getUnit(bundle, periodUnit, periodMax);
var unitText = UnitsOfTimeFormatter.formatWithoutCount(config.getLocale(), periodUnit, periodMax);
return formatPeriodAndPeriodMaxText(periodMin, periodMax, unitText);
}

Expand All @@ -85,7 +86,7 @@ public String turnPeriodToString(Dosage dosage) {
var period = repeat.getPeriod();
var periodUnit = repeat.getPeriodUnit().toCode();

var unitText = getUnit(bundle, periodUnit, period);
var unitText = UnitsOfTimeFormatter.formatWithoutCount(config.getLocale(), periodUnit, period);
return formatPeriodText(period, unitText);
}

Expand Down

0 comments on commit 06b8b1d

Please sign in to comment.