-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature-kotlin' into feature-trackable-ext
- Loading branch information
Showing
13 changed files
with
128 additions
and
39 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
23 changes: 23 additions & 0 deletions
23
Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/ucum/UcumService.kt
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,23 @@ | ||
package org.cqframework.cql.cql2elm.ucum | ||
|
||
import java.math.BigDecimal | ||
|
||
interface UcumService { | ||
/** | ||
* Converts a quantity from one unit to another | ||
* | ||
* @param value the quantity to convert | ||
* @param sourceUnit the unit of the quantity | ||
* @param destUnit the unit to convert to | ||
* @return the converted value in terms of the destination unit | ||
*/ | ||
fun convert(value: BigDecimal, sourceUnit: String, destUnit: String): BigDecimal | ||
|
||
/** | ||
* Validate checks that a string is valid ucum unit | ||
* | ||
* @param unit | ||
* @return null if valid, error message if invalid | ||
*/ | ||
fun validate(unit: String): String? | ||
} |
14 changes: 14 additions & 0 deletions
14
Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/ucum/UcumServiceFactory.kt
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,14 @@ | ||
package org.cqframework.cql.cql2elm.ucum | ||
|
||
import java.util.* | ||
|
||
object UcumServiceFactory { | ||
fun load(): UcumService { | ||
return ServiceLoader.load(UcumService::class.java).firstOrNull() | ||
?: error( | ||
"""No UCUM service implementation found. | ||
Please ensure a UCUM service implementation is available on the classpath. | ||
The 'ucum' module is a reference implementation that can be used for this purpose.""" | ||
) | ||
} | ||
} |
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
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,8 @@ | ||
plugins { | ||
id("cql.library-conventions") | ||
} | ||
|
||
dependencies { | ||
api(project(":cql-to-elm")) | ||
api("org.fhir:ucum:1.0.8") | ||
} |
45 changes: 45 additions & 0 deletions
45
Src/java/ucum/src/main/java/org/cqframework/cql/ucum/DefaultUcumService.kt
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,45 @@ | ||
package org.cqframework.cql.ucum | ||
|
||
import org.cqframework.cql.cql2elm.ucum.UcumService | ||
import org.fhir.ucum.Decimal | ||
import org.fhir.ucum.UcumEssenceService | ||
|
||
@ConsistentCopyVisibility | ||
data class DefaultUcumService private constructor(private val ucumService: UcumService) : | ||
UcumService by ucumService { | ||
constructor() : this(createUcumService()) | ||
|
||
companion object { | ||
private fun createUcumService(): UcumService { | ||
return try { | ||
UcumEssenceService( | ||
UcumEssenceService::class.java.getResourceAsStream("/ucum-essence.xml") | ||
) | ||
.let { u -> | ||
object : UcumService { | ||
override fun convert( | ||
value: java.math.BigDecimal, | ||
sourceUnit: String, | ||
destUnit: String | ||
): java.math.BigDecimal { | ||
val ucumValue = Decimal(value.toString()) | ||
val converted = u.convert(ucumValue, sourceUnit, destUnit) | ||
return java.math.BigDecimal(converted.asDecimal()) | ||
} | ||
|
||
override fun validate(unit: String): String? { | ||
return u.validate(unit) | ||
} | ||
} | ||
} | ||
} catch (e: org.fhir.ucum.UcumException) { | ||
throw IllegalStateException( | ||
"""Failed to create UCUM service. | ||
Please ensure the 'ucum-essence.xml' file is available on the classpath. | ||
The 'ucum' module is a reference implementation that can be used for this purpose.""", | ||
e | ||
) | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...va/ucum/src/main/resources/META-INF/services/org.cqframework.cql.cql2elm.ucum.UcumService
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 @@ | ||
org.cqframework.cql.ucum.DefaultUcumService |
24 changes: 24 additions & 0 deletions
24
Src/java/ucum/src/test/java/org/cqframework/cql/ucum/DefaultUcumServiceTest.kt
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,24 @@ | ||
package org.cqframework.cql.ucum | ||
|
||
import java.math.BigDecimal | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertNull | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
|
||
class DefaultUcumServiceTest { | ||
|
||
@Test | ||
fun testConvert() { | ||
val ucumService = DefaultUcumService() | ||
val result = ucumService.convert(BigDecimal("1"), "mg", "g") | ||
assertEquals(BigDecimal("0.0010"), result) | ||
} | ||
|
||
@Test | ||
fun testValidate() { | ||
val ucumService = DefaultUcumService() | ||
assertNull(ucumService.validate("mg")) | ||
assertTrue(ucumService.validate("foo")?.contains("The unit 'foo' is unknown") ?: false) | ||
} | ||
} |