-
-
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.
Merge pull request #2 from jy95/doseAndRate
feat: doseAndRate translations
- Loading branch information
Showing
23 changed files
with
914 additions
and
46 deletions.
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
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
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
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
47 changes: 47 additions & 0 deletions
47
src/main/java/jy95/fhir/r4/dosage/utils/functions/QuantityToString.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,47 @@ | ||
package jy95.fhir.r4.dosage.utils.functions; | ||
|
||
import jy95.fhir.r4.dosage.utils.config.FDUConfig; | ||
import org.hl7.fhir.r4.model.Quantity; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.ResourceBundle; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public final class QuantityToString { | ||
public static CompletableFuture<String> convert(ResourceBundle bundle, FDUConfig config, Quantity quantity) { | ||
var comparator = comparatorToString(bundle, config, quantity); | ||
var unit = unitToString(config, quantity); | ||
var amount = quantity.getValue().toString(); | ||
|
||
return comparator.thenCombineAsync(unit, (comparatorText, unitText) -> | ||
Stream | ||
.of(comparatorText, amount, unitText) | ||
.filter(part -> !part.isEmpty()) | ||
.collect(Collectors.joining(" ")) | ||
); | ||
} | ||
|
||
// See if unit (code or text) could be found in quantity | ||
private static boolean hasUnit(Quantity quantity) { | ||
return quantity.hasUnit() || quantity.hasCode(); | ||
} | ||
|
||
private static CompletableFuture<String> comparatorToString(ResourceBundle bundle, FDUConfig config, Quantity quantity) { | ||
if (quantity.hasComparator()) { | ||
var code = quantity.getComparator().toCode(); | ||
var comparatorMsg = bundle.getString(code); | ||
var text = new MessageFormat(comparatorMsg, config.getLocale()).format(new Object[]{}); | ||
return CompletableFuture.completedFuture(text); | ||
} | ||
return CompletableFuture.completedFuture(""); | ||
} | ||
|
||
private static CompletableFuture<String> unitToString(FDUConfig config, Quantity quantity) { | ||
if (hasUnit(quantity)) { | ||
return config.getFromFHIRQuantityUnitToString().apply(quantity); | ||
} | ||
return CompletableFuture.completedFuture(""); | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
src/main/java/jy95/fhir/r4/dosage/utils/functions/RatioToString.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,77 @@ | ||
package jy95.fhir.r4.dosage.utils.functions; | ||
|
||
import jy95.fhir.r4.dosage.utils.config.FDUConfig; | ||
import org.hl7.fhir.r4.model.Quantity; | ||
import org.hl7.fhir.r4.model.Ratio; | ||
|
||
import java.math.BigDecimal; | ||
import java.text.MessageFormat; | ||
import java.util.Map; | ||
import java.util.ResourceBundle; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public final class RatioToString { | ||
public static CompletableFuture<String> convert(ResourceBundle bundle, FDUConfig config, Ratio ratio) { | ||
var linkword = retrieveRatioLinkWord(bundle, config, ratio); | ||
|
||
var numeratorText = ratio.hasNumerator() | ||
? QuantityToString.convert(bundle, config, ratio.getNumerator()) | ||
: CompletableFuture.completedFuture(""); | ||
|
||
var denominatorText = ratio.hasDenominator() | ||
? turnDenominatorToText(bundle, config, ratio) | ||
: CompletableFuture.completedFuture(""); | ||
|
||
return numeratorText.thenCombineAsync(denominatorText, (num, dem) -> Stream | ||
.of(num, linkword, dem) | ||
.filter(s -> !s.isEmpty()) | ||
.collect(Collectors.joining(" ")) | ||
); | ||
} | ||
|
||
private static String retrieveRatioLinkWord(ResourceBundle bundle, FDUConfig config, Ratio ratio) { | ||
var hasNumerator = ratio.hasNumerator(); | ||
var hasDenominator = ratio.hasDenominator(); | ||
var hasNumeratorUnit = hasNumerator && hasUnit(ratio.getNumerator()); | ||
var hasBothElements = hasNumerator && hasDenominator; | ||
var hasDenominatorUnit = hasDenominator && hasUnit(ratio.getDenominator()); | ||
var hasUnitRatio = hasNumeratorUnit || hasDenominatorUnit; | ||
var denominatorValue = hasDenominator ? ratio.getDenominator().getValue() : BigDecimal.ONE; | ||
|
||
if (hasUnitRatio && hasBothElements) { | ||
var linkWordMsg = bundle.getString("amount.ratio.denominatorLinkword"); | ||
return new MessageFormat(linkWordMsg, config.getLocale()).format(new Object[]{denominatorValue}); | ||
} | ||
|
||
return hasBothElements ? ":" : ""; | ||
} | ||
|
||
private static CompletableFuture<String> turnDenominatorToText( | ||
ResourceBundle bundle, | ||
FDUConfig config, | ||
Ratio ratio | ||
) { | ||
var denominator = ratio.getDenominator(); | ||
// Where the denominator value is known to be fixed to "1", Quantity should be used instead of Ratio | ||
var denominatorValue = denominator.getValue(); | ||
|
||
// For titers cases (e.g. 1:128) | ||
if (!hasUnit(denominator)) { | ||
return CompletableFuture.completedFuture(denominatorValue.toString()); | ||
} | ||
|
||
// For the per case | ||
if (BigDecimal.ONE.equals(denominatorValue)) { | ||
return config.getFromFHIRQuantityUnitToString().apply(denominator); | ||
} | ||
|
||
return QuantityToString.convert(bundle, config, denominator); | ||
} | ||
|
||
// See if unit (code or text) could be found in quantity | ||
private static boolean hasUnit(Quantity quantity) { | ||
return quantity.hasUnit() || quantity.hasCode(); | ||
} | ||
} |
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
src/main/java/jy95/fhir/r4/dosage/utils/translators/DoseQuantity.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 com.ibm.icu.text.MessageFormat; | ||
import jy95.fhir.r4.dosage.utils.classes.AbstractTranslator; | ||
import jy95.fhir.r4.dosage.utils.config.FDUConfig; | ||
import jy95.fhir.r4.dosage.utils.functions.QuantityToString; | ||
import jy95.fhir.r4.dosage.utils.types.DoseAndRateKey; | ||
import org.hl7.fhir.r4.model.Dosage; | ||
import org.hl7.fhir.r4.model.Quantity; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class DoseQuantity extends AbstractTranslator { | ||
|
||
public DoseQuantity(FDUConfig config) { | ||
super(config); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<String> convert(Dosage dosage) { | ||
var bundle = getResources(); | ||
var doseAndRate = dosage.getDoseAndRate(); | ||
var doseQuantity = getConfig() | ||
.getSelectDosageAndRateField() | ||
.apply(doseAndRate, DoseAndRateKey.DOSE_QUANTITY); | ||
return QuantityToString | ||
.convert(bundle, getConfig(), (Quantity) doseQuantity) | ||
.thenApplyAsync(quantityText -> { | ||
var doseMsg = bundle.getString("fields.doseQuantity"); | ||
return new MessageFormat(doseMsg, getConfig().getLocale()).format(new Object[]{quantityText}); | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isPresent(Dosage dosage) { | ||
return dosage.hasDoseAndRate() && dosage | ||
.getDoseAndRate() | ||
.stream() | ||
.anyMatch(Dosage.DosageDoseAndRateComponent::hasDoseQuantity); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/jy95/fhir/r4/dosage/utils/translators/DoseRange.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,42 @@ | ||
package jy95.fhir.r4.dosage.utils.translators; | ||
|
||
import com.ibm.icu.text.MessageFormat; | ||
import jy95.fhir.r4.dosage.utils.classes.AbstractTranslator; | ||
import jy95.fhir.r4.dosage.utils.config.FDUConfig; | ||
import jy95.fhir.r4.dosage.utils.functions.RangeToString; | ||
import jy95.fhir.r4.dosage.utils.types.DoseAndRateKey; | ||
import org.hl7.fhir.r4.model.Dosage; | ||
import org.hl7.fhir.r4.model.Range; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class DoseRange extends AbstractTranslator { | ||
|
||
public DoseRange(FDUConfig config) { | ||
super(config); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<String> convert(Dosage dosage) { | ||
var bundle = getResources(); | ||
var doseAndRate = dosage.getDoseAndRate(); | ||
var doseRange = getConfig() | ||
.getSelectDosageAndRateField() | ||
.apply(doseAndRate, DoseAndRateKey.DOSE_RANGE); | ||
|
||
return RangeToString | ||
.convert(bundle, getConfig(), (Range) doseRange) | ||
.thenApplyAsync(rangeText -> { | ||
var rangeMsg = bundle.getString("fields.doseRange"); | ||
return new MessageFormat(rangeMsg, getConfig().getLocale()).format(new Object[]{rangeText}); | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isPresent(Dosage dosage) { | ||
return dosage.hasDoseAndRate() && dosage | ||
.getDoseAndRate() | ||
.stream() | ||
.anyMatch(Dosage.DosageDoseAndRateComponent::hasDoseRange); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/jy95/fhir/r4/dosage/utils/translators/RateQuantity.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,42 @@ | ||
package jy95.fhir.r4.dosage.utils.translators; | ||
|
||
import com.ibm.icu.text.MessageFormat; | ||
import jy95.fhir.r4.dosage.utils.classes.AbstractTranslator; | ||
import jy95.fhir.r4.dosage.utils.config.FDUConfig; | ||
import jy95.fhir.r4.dosage.utils.functions.QuantityToString; | ||
import jy95.fhir.r4.dosage.utils.types.DoseAndRateKey; | ||
import org.hl7.fhir.r4.model.Dosage; | ||
import org.hl7.fhir.r4.model.Quantity; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class RateQuantity extends AbstractTranslator { | ||
|
||
public RateQuantity(FDUConfig config) { | ||
super(config); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<String> convert(Dosage dosage) { | ||
var bundle = getResources(); | ||
var doseAndRate = dosage.getDoseAndRate(); | ||
var rateQuantity = getConfig() | ||
.getSelectDosageAndRateField() | ||
.apply(doseAndRate, DoseAndRateKey.RATE_QUANTITY); | ||
return QuantityToString | ||
.convert(bundle, getConfig(), (Quantity) rateQuantity) | ||
.thenApplyAsync(rateQuantityText -> { | ||
var doseMsg = bundle.getString("fields.rateQuantity"); | ||
return new MessageFormat(doseMsg, getConfig().getLocale()).format(new Object[]{rateQuantityText}); | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isPresent(Dosage dosage) { | ||
return dosage.hasDoseAndRate() && dosage | ||
.getDoseAndRate() | ||
.stream() | ||
.anyMatch(Dosage.DosageDoseAndRateComponent::hasRateQuantity); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/jy95/fhir/r4/dosage/utils/translators/RateRange.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,42 @@ | ||
package jy95.fhir.r4.dosage.utils.translators; | ||
|
||
import com.ibm.icu.text.MessageFormat; | ||
import jy95.fhir.r4.dosage.utils.classes.AbstractTranslator; | ||
import jy95.fhir.r4.dosage.utils.config.FDUConfig; | ||
import jy95.fhir.r4.dosage.utils.functions.RangeToString; | ||
import jy95.fhir.r4.dosage.utils.types.DoseAndRateKey; | ||
import org.hl7.fhir.r4.model.Dosage; | ||
import org.hl7.fhir.r4.model.Range; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class RateRange extends AbstractTranslator { | ||
|
||
public RateRange(FDUConfig config) { | ||
super(config); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<String> convert(Dosage dosage) { | ||
var bundle = getResources(); | ||
var doseAndRate = dosage.getDoseAndRate(); | ||
var rateRange = getConfig() | ||
.getSelectDosageAndRateField() | ||
.apply(doseAndRate, DoseAndRateKey.RATE_RANGE); | ||
|
||
return RangeToString | ||
.convert(bundle, getConfig(), (Range) rateRange) | ||
.thenApplyAsync(rateRatioText -> { | ||
var doseRateMsg = bundle.getString("fields.rateRange"); | ||
return new MessageFormat(doseRateMsg, getConfig().getLocale()).format(new Object[]{rateRatioText}); | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isPresent(Dosage dosage) { | ||
return dosage.hasDoseAndRate() && dosage | ||
.getDoseAndRate() | ||
.stream() | ||
.anyMatch(Dosage.DosageDoseAndRateComponent::hasRateRange); | ||
} | ||
} |
Oops, something went wrong.