Skip to content

Commit

Permalink
feat: MaxDosePerPeriod
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Jan 5, 2025
1 parent cf7dfa0 commit acf629e
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
Expand All @@ -13,6 +14,8 @@
public final class QuantityToString {

final static String DURATION_SYSTEM = "http://hl7.org/fhir/ValueSet/duration-units";
final static String UNITS_OF_TIME_SYSTEM = "http://hl7.org/fhir/ValueSet/units-of-time";
final static List<String> TIME_SYSTEMS = List.of(DURATION_SYSTEM, UNITS_OF_TIME_SYSTEM);

public static CompletableFuture<String> convert(ResourceBundle bundle, FDUConfig config, Quantity quantity) {
var comparator = comparatorToString(bundle, config, quantity);
Expand All @@ -39,7 +42,7 @@ public static boolean hasUnit(Quantity quantity) {
public static CompletableFuture<String> enhancedFromFHIRQuantityUnitToString(ResourceBundle bundle, FDUConfig config, Quantity quantity) {

// duration units are built-in supported
if (quantity.hasSystem() && quantity.hasCode() && quantity.getSystem().equals(DURATION_SYSTEM)) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public Translators(FDUConfig config) {
Map.entry(DisplayOrder.RATE_RATIO, new RateRatio(config)),
Map.entry(DisplayOrder.OFFSET_WHEN, new OffsetWhen(config)),
Map.entry(DisplayOrder.MAX_DOSE_PER_LIFETIME, new MaxDosePerLifetime(config)),
Map.entry(DisplayOrder.MAX_DOSE_PER_ADMINISTRATION, new MaxDosePerAdministration(config))
Map.entry(DisplayOrder.MAX_DOSE_PER_ADMINISTRATION, new MaxDosePerAdministration(config)),
Map.entry(DisplayOrder.MAX_DOSE_PER_PERIOD, new MaxDosePerPeriod(config))
)
);
this.bundleControl = new MultiResourceBundleControl(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package jy95.fhir.r4.dosage.utils.translators;

import jy95.fhir.r4.dosage.utils.classes.AbstractTranslator;
import jy95.fhir.r4.dosage.utils.config.FDUConfig;
import jy95.fhir.r4.dosage.utils.functions.RatioToString;
import org.hl7.fhir.r4.model.Dosage;

import java.text.MessageFormat;
import java.util.concurrent.CompletableFuture;

public class MaxDosePerPeriod extends AbstractTranslator {

public MaxDosePerPeriod(FDUConfig config) {
super(config);
}

@Override
public CompletableFuture<String> convert(Dosage dosage) {
var ratio = dosage.getMaxDosePerPeriod();
var bundle = getResources();

return RatioToString
.convert(bundle, getConfig(), ratio)
.thenApplyAsync((ratioText) -> {
String msg = bundle.getString("fields.maxDosePerPeriod");
return new MessageFormat(msg, getConfig().getLocale()).format(new Object[] {ratioText});
});
}

@Override
public boolean isPresent(Dosage dosage) {
return dosage.hasMaxDosePerPeriod();
}
}
2 changes: 2 additions & 0 deletions src/main/resources/unitsOfTime_de.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
withCount.ms = {0, choice, 0#{0} Millisekunde|1#{0} Millisekunde|1<{0} Millisekunden}
withCount.s = {0, choice, 0#{0} Sekunde|1#{0} Sekunde|1<{0} Sekunden}
withCount.min = {0, choice, 0#{0} Minute|1#{0} Minute|1<{0} Minuten}
withCount.h = {0, choice, 0#{0} Stunde|1#{0} Stunde|1<{0} Stunden}
Expand All @@ -6,6 +7,7 @@ withCount.wk = {0, choice, 0#{0} Woche|1#{0} Woche|1<{0} Wochen}
withCount.mo = {0, choice, 0#{0} Monat|1#{0} Monat|1<{0} Monate}
withCount.a = {0, choice, 0#{0} Jahr|1#{0} Jahr|1<{0} Jahre}

withoutCount.ms = {0, choice, 0#Millisekunde|1#Millisekunde|1<Millisekunden}
withoutCount.s = {0, choice, 0#Sekunde|1#Sekunde|1<Sekunden}
withoutCount.min = {0, choice, 0#Minute|1#Minute|1<Minuten}
withoutCount.h = {0, choice, 0#Stunde|1#Stunde|1<Stunden}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/unitsOfTime_en.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
withCount.ms = {0, choice, 0#{0} millisecond|1#{0} millisecond|1<{0} milliseconds}
withCount.s = {0, choice, 0#{0} second|1#{0} second|1<{0} seconds}
withCount.min = {0, choice, 0#{0} minute|1#{0} minute|1<{0} minutes}
withCount.h = {0, choice, 0#{0} hour|1#{0} hour|1<{0} hours}
Expand All @@ -6,6 +7,7 @@ withCount.wk = {0, choice, 0#{0} week|1#{0} week|1<{0} weeks}
withCount.mo = {0, choice, 0#{0} month|1#{0} month|1<{0} months}
withCount.a = {0, choice, 0#{0} year|1#{0} year|1<{0} years}

withoutCount.ms = {0, choice, 0#millisecond|1#millisecond|1<milliseconds}
withoutCount.s = {0, choice, 0#second|1#second|1<seconds}
withoutCount.min = {0, choice, 0#minute|1#minute|1<minutes}
withoutCount.h = {0, choice, 0#hour|1#hour|1<hours}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/unitsOfTime_fr.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
withCount.ms = {0, choice, 0#{0} milliseconde|1#{0} milliseconde|1<{0} millisecondes}
withCount.s = {0, choice, 0#{0} seconde|1#{0} seconde|1<{0} secondes}
withCount.min = {0, choice, 0#{0} minute|1#{0} minute|1<{0} minutes}
withCount.h = {0, choice, 0#{0} heure|1#{0} heure|1<{0} heures}
Expand All @@ -6,6 +7,7 @@ withCount.wk = {0, choice, 0#{0} semaine|1#{0} semaine|1<{0} semaines}
withCount.mo = {0, choice, 0#{0} mois|1#{0} mois|1<{0} mois}
withCount.a = {0, choice, 0#{0} année|1#{0} année|1<{0} années}

withoutCount.ms = {0, choice, 0#milliseconde|1#milliseconde|1<millisecondes}
withoutCount.s = {0, choice, 0#seconde|1#seconde|1<secondes}
withoutCount.min = {0, choice, 0#minute|1#minute|1<minutes}
withoutCount.h = {0, choice, 0#heure|1#heure|1<heures}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/unitsOfTime_nl.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
withCount.ms = {0, choice, 0#{0} milliseconde|1#{0} milliseconde|1<{0} milliseconden}
withCount.s = {0, choice, 0#{0} seconde|1#{0} seconde|1<{0} seconden}
withCount.min = {0, choice, 0#{0} minuut|1#{0} minuut|1<{0} minuten}
withCount.h = {0, choice, 0#{0} uur|1#{0} uur|1<{0} uren}
Expand All @@ -6,6 +7,7 @@ withCount.wk = {0, choice, 0#{0} week|1#{0} week|1<{0} weken}
withCount.mo = {0, choice, 0#{0} maand|1#{0} maand|1<{0} maanden}
withCount.a = {0, choice, 0#{0} jaar|1#{0} jaar|1<{0} jaren}

withoutCount.ms = {0, choice, 0#milliseconde|1#milliseconde|1<milliseconden}
withoutCount.s = {0, choice, 0#seconde|1#seconde|1<seconden}
withoutCount.min = {0, choice, 0#minuut|1#minuut|1<minuten}
withoutCount.h = {0, choice, 0#uur|1#uur|1<uren}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package jy95.fhir.r4.dosage.utils.translators;

import jy95.fhir.r4.dosage.utils.AbstractFhirTest;
import jy95.fhir.r4.dosage.utils.classes.FhirDosageUtils;
import jy95.fhir.r4.dosage.utils.types.DisplayOrder;
import org.hl7.fhir.r4.model.Dosage;
import org.hl7.fhir.r4.model.Quantity;
import org.hl7.fhir.r4.model.Ratio;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Locale;
import java.util.concurrent.ExecutionException;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MaxDosePerPeriodTest extends AbstractFhirTest {

@ParameterizedTest
@MethodSource("localeProvider")
void testNoMaxDosePerPeriod(Locale locale) throws ExecutionException, InterruptedException {
Dosage dosage = new Dosage();
FhirDosageUtils dosageUtils = getDosageUtilsInstance(locale, DisplayOrder.MAX_DOSE_PER_PERIOD);
String result = dosageUtils.asHumanReadableText(dosage).get();
assertEquals("", result);
}

@ParameterizedTest
@MethodSource("localeProvider")
void testWithMaxDosePerPeriod(Locale locale) throws ExecutionException, InterruptedException {
Dosage dosage = new Dosage();
Ratio ratio = new Ratio();
Quantity numerator = new Quantity(10);
numerator.setUnit("mg");
ratio.setNumerator(numerator);
Quantity denominator = new Quantity(1);
denominator.setCode("d");
denominator.setSystem("http://hl7.org/fhir/ValueSet/units-of-time");
ratio.setDenominator(denominator);
dosage.setMaxDosePerPeriod(ratio);
FhirDosageUtils dosageUtils = getDosageUtilsInstance(locale, DisplayOrder.MAX_DOSE_PER_PERIOD);
String result = dosageUtils.asHumanReadableText(dosage).get();
String expected = getExpectedText(locale);
assertEquals(expected, result);
}

private String getExpectedText(Locale locale) {
if (locale.equals(Locale.ENGLISH)) {
return "up to a maximum of 10 mg per day";
} else if (locale.equals(Locale.FRENCH)) {
return "jusqu’à un maximum de 10 mg par jour";
} else if (locale.equals(Locale.GERMAN)) {
return "bis zu einer maximalen Menge von 10 mg pro Tag";
} else {
return "tot een maximum van 10 mg per dag";
}
}
}

0 comments on commit acf629e

Please sign in to comment.