-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: FrequencyFrequencyMaxPeriodPeriodMax
- Loading branch information
Showing
3 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...main/java/jy95/fhir/r4/dosage/utils/translators/FrequencyFrequencyMaxPeriodPeriodMax.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package jy95.fhir.r4.dosage.utils.translators; | ||
|
||
import jy95.fhir.r4.dosage.utils.classes.AbstractTranslator; | ||
import jy95.fhir.r4.dosage.utils.config.FDUConfig; | ||
import org.hl7.fhir.r4.model.Dosage; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class FrequencyFrequencyMaxPeriodPeriodMax extends AbstractTranslator { | ||
|
||
private final FrequencyFrequencyMax frequencyFrequencyMax; | ||
private final PeriodPeriodMax periodPeriodMax; | ||
|
||
public FrequencyFrequencyMaxPeriodPeriodMax(FDUConfig config) { | ||
super(config); | ||
this.frequencyFrequencyMax = new FrequencyFrequencyMax(config); | ||
this.periodPeriodMax = new PeriodPeriodMax(config); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<String> convert(Dosage dosage) { | ||
var frequencyPart = frequencyFrequencyMax.isPresent(dosage) | ||
? frequencyFrequencyMax.convert(dosage) | ||
: CompletableFuture.completedFuture(""); | ||
|
||
var periodPart = periodPeriodMax.isPresent(dosage) | ||
? periodPeriodMax.convert(dosage) | ||
: CompletableFuture.completedFuture(""); | ||
|
||
return frequencyPart.thenCombineAsync(periodPart, (freq, period) -> Stream.of(freq, period) | ||
.filter(part -> !part.isEmpty()) | ||
.collect(Collectors.joining(" "))); | ||
} | ||
|
||
@Override | ||
public boolean isPresent(Dosage dosage) { | ||
return frequencyFrequencyMax.isPresent(dosage) || periodPeriodMax.isPresent(dosage); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
.../java/jy95/fhir/r4/dosage/utils/translators/FrequencyFrequencyMaxPeriodPeriodMaxTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
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.Timing; | ||
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 FrequencyFrequencyMaxPeriodPeriodMaxTest extends AbstractFhirTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("localeProvider") | ||
void testNoFrequencyOrPeriod(Locale locale) throws ExecutionException, InterruptedException { | ||
Dosage dosage = new Dosage(); | ||
FhirDosageUtils dosageUtils = DayOfWeekTest.getDosageUtilsInstance( | ||
locale, | ||
DisplayOrder.FREQUENCY_FREQUENCY_MAX_PERIOD_PERIOD_MAX | ||
); | ||
String result = dosageUtils.asHumanReadableText(dosage).get(); | ||
assertEquals("", result); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("localeProvider") | ||
void testWithFrequencyOnly(Locale locale) throws ExecutionException, InterruptedException { | ||
FhirDosageUtils dosageUtils = DayOfWeekTest.getDosageUtilsInstance( | ||
locale, | ||
DisplayOrder.FREQUENCY_FREQUENCY_MAX_PERIOD_PERIOD_MAX | ||
); | ||
Dosage dosage = new Dosage(); | ||
Timing timing = new Timing(); | ||
Timing.TimingRepeatComponent repeatComponent = new Timing.TimingRepeatComponent(); | ||
repeatComponent.setFrequency(3); | ||
|
||
timing.setRepeat(repeatComponent); | ||
dosage.setTiming(timing); | ||
|
||
String result = dosageUtils.asHumanReadableText(dosage).get(); | ||
String expectedResult = getExpectedText1(locale); | ||
assertEquals(expectedResult, result); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("localeProvider") | ||
void testWithPeriodOnly(Locale locale) throws ExecutionException, InterruptedException { | ||
FhirDosageUtils dosageUtils = DayOfWeekTest.getDosageUtilsInstance( | ||
locale, | ||
DisplayOrder.FREQUENCY_FREQUENCY_MAX_PERIOD_PERIOD_MAX | ||
); | ||
Dosage dosage = new Dosage(); | ||
Timing timing = new Timing(); | ||
Timing.TimingRepeatComponent repeatComponent = new Timing.TimingRepeatComponent(); | ||
repeatComponent.setPeriod(2); | ||
repeatComponent.setPeriodUnit(Timing.UnitsOfTime.D); | ||
|
||
timing.setRepeat(repeatComponent); | ||
dosage.setTiming(timing); | ||
|
||
String result = dosageUtils.asHumanReadableText(dosage).get(); | ||
String expectedResult = getExpectedText2(locale); | ||
assertEquals(expectedResult, result); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("localeProvider") | ||
void testBothFrequencyAndPeriod(Locale locale) throws ExecutionException, InterruptedException { | ||
FhirDosageUtils dosageUtils = DayOfWeekTest.getDosageUtilsInstance( | ||
locale, | ||
DisplayOrder.FREQUENCY_FREQUENCY_MAX_PERIOD_PERIOD_MAX | ||
); | ||
Dosage dosage = new Dosage(); | ||
Timing timing = new Timing(); | ||
Timing.TimingRepeatComponent repeatComponent = new Timing.TimingRepeatComponent(); | ||
repeatComponent.setPeriod(2); | ||
repeatComponent.setPeriodUnit(Timing.UnitsOfTime.D); | ||
repeatComponent.setFrequency(3); | ||
|
||
timing.setRepeat(repeatComponent); | ||
dosage.setTiming(timing); | ||
|
||
String result = dosageUtils.asHumanReadableText(dosage).get(); | ||
String expectedResult = getExpectedText3(locale); | ||
assertEquals(expectedResult, result); | ||
} | ||
|
||
private String getExpectedText1(Locale locale) { | ||
if (locale.equals(Locale.ENGLISH)) { | ||
return "3 times"; | ||
} else if (locale.equals(Locale.FRENCH)) { | ||
return "3 fois"; | ||
} else if (locale.equals(Locale.GERMAN)) { | ||
return "3 Mal"; | ||
} else { | ||
return "3 keer"; | ||
} | ||
} | ||
|
||
private String getExpectedText2(Locale locale) { | ||
if (locale.equals(Locale.ENGLISH)) { | ||
return "every 2 days"; | ||
} else if (locale.equals(Locale.FRENCH)) { | ||
return "chaque 2 jours"; | ||
} else if (locale.equals(Locale.GERMAN)) { | ||
return "alle 2 Tage"; | ||
} else { | ||
return "per 2 dagen"; | ||
} | ||
} | ||
|
||
private String getExpectedText3(Locale locale) { | ||
if (locale.equals(Locale.ENGLISH)) { | ||
return "3 times every 2 days"; | ||
} else if (locale.equals(Locale.FRENCH)) { | ||
return "3 fois chaque 2 jours"; | ||
} else if (locale.equals(Locale.GERMAN)) { | ||
return "3 Mal alle 2 Tage"; | ||
} else { | ||
return "3 keer per 2 dagen"; | ||
} | ||
} | ||
|
||
} |