Skip to content

Commit

Permalink
Overloading the constructor to load from strings (#69)
Browse files Browse the repository at this point in the history
* overloading the constructor to load units and systems from string instead of getting it from a url

* fix missing spacing

* upgrade version
  • Loading branch information
Jacob-Eliat-Eliat authored May 7, 2024
1 parent 499b57f commit e0eabcd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.cognite.units</groupId>
<artifactId>units-catalog</artifactId>
<version>0.1.8</version>
<version>0.1.9</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>A comprehensive unit catalog for Cognite Data Fusion (CDF) with a focus on standardization, comprehensiveness, and consistency.</description>
Expand Down
15 changes: 8 additions & 7 deletions src/main/kotlin/com/cognite/units/UnitService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import kotlin.math.log10
import kotlin.math.pow
import kotlin.math.roundToLong

class UnitService(unitsPath: URL, systemPath: URL) {
class UnitService(units: String, systems: String) {

constructor(unitsPath: URL, systemPath: URL) : this(unitsPath.readText(), systemPath.readText())

companion object {
val service: UnitService by lazy {
UnitService(
Expand All @@ -42,8 +45,8 @@ class UnitService(unitsPath: URL, systemPath: URL) {
private val defaultUnitByQuantityAndSystem = mutableMapOf<String, MutableMap<String, TypedUnit>>()

init {
loadUnits(unitsPath)
loadSystem(systemPath)
loadUnits(units)
loadSystem(systems)
}

private fun sanitizeIdentifier(identifier: String): String {
Expand Down Expand Up @@ -102,8 +105,7 @@ class UnitService(unitsPath: URL, systemPath: URL) {
}
}

private fun loadUnits(unitsPath: URL) {
val units = unitsPath.readText()
private fun loadUnits(units: String) {
val mapper: ObjectMapper = jacksonObjectMapper()

// 1. Syntax Check: Every unit item in `units.json` must have the specified keys
Expand Down Expand Up @@ -144,8 +146,7 @@ class UnitService(unitsPath: URL, systemPath: URL) {
}
}

private fun loadSystem(systemPath: URL) {
val systems = systemPath.readText()
private fun loadSystem(systems: String) {
val mapper: ObjectMapper = jacksonObjectMapper()
val listOfSystems: List<UnitSystem> = mapper.readValue<List<UnitSystem>>(systems)

Expand Down
8 changes: 8 additions & 0 deletions src/test/kotlin/UnitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class UnitTest {
UnitService.service
}

@Test
fun useStringConstructor() {
val units = UnitService::class.java.getResource("/units.json")!!.readText()
val systems = UnitService::class.java.getResource("/unitSystems.json")!!.readText()
val unitService = UnitService(units, systems)
unitService.getUnitByExternalId("temperature:deg_c")
}

@Test
fun convertBetweenUnits() {
val unitService = UnitService.service
Expand Down

0 comments on commit e0eabcd

Please sign in to comment.