Skip to content

Commit

Permalink
Use ktfmt and use google's kotlin style.
Browse files Browse the repository at this point in the history
  • Loading branch information
jingtang10 committed Mar 9, 2021
1 parent 637684e commit dbbfb55
Show file tree
Hide file tree
Showing 139 changed files with 7,531 additions and 7,600 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ subprojects {
spotless {
kotlin {
target '**/*.kt'
ktlint()userData(['max_line_length': '100'])
ktlint().userData(['indent_size': '2', 'continuation_indent_size': '2'])
ktfmt().googleStyle()
licenseHeaderFile "${project.rootProject.projectDir}/license-header.txt"
}
}
Expand Down

Large diffs are not rendered by default.

94 changes: 47 additions & 47 deletions core/src/main/java/com/google/android/fhir/FhirEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,61 @@ import com.google.android.fhir.sync.SyncConfiguration
import org.hl7.fhir.r4.model.Resource
import org.opencds.cqf.cql.execution.EvaluationResult

/** The FHIR Engine interface that handles the local storage of FHIR resources. */
/** The FHIR Engine interface that handles the local storage of FHIR resources. */
interface FhirEngine {
/**
* Saves a FHIR `resource` in the local storage. If the resource already exists, it will be
* overwritten
*
* @param <R> The resource type which should be a subtype of [Resource].
*/
fun <R : Resource> save(resource: R)
/**
* Saves a FHIR `resource` in the local storage. If the resource already exists, it will be
* overwritten
*
* @param <R> The resource type which should be a subtype of [Resource].
*/
fun <R : Resource> save(resource: R)

/**
* Saves a list of FHIR `resource` in the local storage. If any of the resources already
* exist, they will be overwritten.
*
* @param <R> The resource type which should be a subtype of [Resource].
*/
fun <R : Resource> saveAll(resources: List<R>)
/**
* Saves a list of FHIR `resource` in the local storage. If any of the resources already exist,
* they will be overwritten.
*
* @param <R> The resource type which should be a subtype of [Resource].
*/
fun <R : Resource> saveAll(resources: List<R>)

/**
* Updates a FHIR `resource` in the local storage.
*
* @param <R> The resource type which should be a subtype of [Resource].
*/
fun <R : Resource> update(resource: R)
/**
* Updates a FHIR `resource` in the local storage.
*
* @param <R> The resource type which should be a subtype of [Resource].
*/
fun <R : Resource> update(resource: R)

/**
* Returns a FHIR resource of type `clazz` with `id` from the local storage.
*
* @param <R> The resource type which should be a subtype of [Resource].
* @throws ResourceNotFoundException if the resource is not found
*/
@Throws(ResourceNotFoundException::class)
fun <R : Resource> load(clazz: Class<R>, id: String): R
/**
* Returns a FHIR resource of type `clazz` with `id` from the local storage.
*
* @param <R> The resource type which should be a subtype of [Resource].
* @throws ResourceNotFoundException if the resource is not found
*/
@Throws(ResourceNotFoundException::class) fun <R : Resource> load(clazz: Class<R>, id: String): R

/**
* Removes a FHIR resource of type `clazz` with `id` from the local storage.
*
* @param <R> The resource type which should be a subtype of [Resource].
*/
fun <R : Resource> remove(clazz: Class<R>, id: String)
/**
* Removes a FHIR resource of type `clazz` with `id` from the local storage.
*
* @param <R> The resource type which should be a subtype of [Resource].
*/
fun <R : Resource> remove(clazz: Class<R>, id: String)

/** Returns the result of a CQL evaluation provided with the ID of the library. */
fun evaluateCql(libraryVersionId: String, context: String, expression: String): EvaluationResult
/** Returns the result of a CQL evaluation provided with the ID of the library. */
fun evaluateCql(libraryVersionId: String, context: String, expression: String): EvaluationResult

/** Returns the entry point for [Search]. */
fun search(): Search
/** Returns the entry point for [Search]. */
fun search(): Search

/**
* One time sync.
*
* @param syncConfiguration - configuration of data that needs to be synchronised
*/
suspend fun sync(syncConfiguration: SyncConfiguration): Result
/**
* One time sync.
*
* @param syncConfiguration
* - configuration of data that needs to be synchronised
*/
suspend fun sync(syncConfiguration: SyncConfiguration): Result

suspend fun periodicSync(): Result
suspend fun periodicSync(): Result

fun updatePeriodicSyncConfiguration(syncConfig: PeriodicSyncConfiguration)
fun updatePeriodicSyncConfiguration(syncConfig: PeriodicSyncConfiguration)
}
43 changes: 13 additions & 30 deletions core/src/main/java/com/google/android/fhir/FhirEngineBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,21 @@ import android.content.Context
import com.google.android.fhir.sync.FhirDataSource
import com.google.android.fhir.sync.PeriodicSyncConfiguration

/**
* The builder for [FhirEngine] instance
*/
class FhirEngineBuilder constructor(
dataSource: FhirDataSource,
context: Context
) {
private val services = FhirServices.builder(dataSource, context)
/** The builder for [FhirEngine] instance */
class FhirEngineBuilder constructor(dataSource: FhirDataSource, context: Context) {
private val services = FhirServices.builder(dataSource, context)

/**
* Sets the database file name for the FhirEngine to use.
*/
fun databaseName(name: String) = apply {
services.databaseName(name)
}
/** Sets the database file name for the FhirEngine to use. */
fun databaseName(name: String) = apply { services.databaseName(name) }

/**
* Instructs the FhirEngine to use an in memory database which can be useful for tests.
*/
internal fun inMemory() = apply {
services.inMemory()
}
/** Instructs the FhirEngine to use an in memory database which can be useful for tests. */
internal fun inMemory() = apply { services.inMemory() }

/**
* Configures the FhirEngine periodic sync.
*/
fun periodicSyncConfiguration(config: PeriodicSyncConfiguration) = apply {
services.periodicSyncConfiguration(config)
}
/** Configures the FhirEngine periodic sync. */
fun periodicSyncConfiguration(config: PeriodicSyncConfiguration) = apply {
services.periodicSyncConfiguration(config)
}

/**
* Builds a new instance of the [FhirEngine].
*/
fun build() = services.build().fhirEngine
/** Builds a new instance of the [FhirEngine]. */
fun build() = services.build().fhirEngine
}
81 changes: 32 additions & 49 deletions core/src/main/java/com/google/android/fhir/FhirServices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,61 +30,44 @@ import com.google.android.fhir.sync.FhirDataSource
import com.google.android.fhir.sync.PeriodicSyncConfiguration

internal data class FhirServices(
val fhirEngine: FhirEngine,
val parser: IParser,
val database: Database
val fhirEngine: FhirEngine,
val parser: IParser,
val database: Database
) {
class Builder(
private val dataSource: FhirDataSource,
private val context: Context
) {
private var databaseName: String? = "fhirEngine"
private var periodicSyncConfiguration: PeriodicSyncConfiguration? = null
class Builder(private val dataSource: FhirDataSource, private val context: Context) {
private var databaseName: String? = "fhirEngine"
private var periodicSyncConfiguration: PeriodicSyncConfiguration? = null

fun inMemory() = apply {
databaseName = null
}
fun inMemory() = apply { databaseName = null }

fun databaseName(name: String) = apply {
databaseName = name
}
fun databaseName(name: String) = apply { databaseName = name }

fun periodicSyncConfiguration(config: PeriodicSyncConfiguration) = apply {
periodicSyncConfiguration = config
}
fun periodicSyncConfiguration(config: PeriodicSyncConfiguration) = apply {
periodicSyncConfiguration = config
}

fun build(): FhirServices {
val parser = FhirContext.forR4().newJsonParser()
val db = DatabaseImpl(
context = context,
iParser = parser,
databaseName = databaseName
)
fun build(): FhirServices {
val parser = FhirContext.forR4().newJsonParser()
val db = DatabaseImpl(context = context, iParser = parser, databaseName = databaseName)

val dataProvider = FhirEngineDataProvider.Factory.create(db)
val engine = FhirEngineImpl(
database = db,
search = SearchImpl(db),
libraryLoader = FhirEngineLibraryLoader(db),
dataProviderMap = mapOf("http://hl7.org/fhir" to dataProvider),
terminologyProvider = FhirEngineTerminologyProvider(),
periodicSyncConfiguration = periodicSyncConfiguration,
dataSource = dataSource,
context = context
)
return FhirServices(
fhirEngine = engine,
parser = parser,
database = db
)
}
val dataProvider = FhirEngineDataProvider.Factory.create(db)
val engine =
FhirEngineImpl(
database = db,
search = SearchImpl(db),
libraryLoader = FhirEngineLibraryLoader(db),
dataProviderMap = mapOf("http://hl7.org/fhir" to dataProvider),
terminologyProvider = FhirEngineTerminologyProvider(),
periodicSyncConfiguration = periodicSyncConfiguration,
dataSource = dataSource,
context = context
)
return FhirServices(fhirEngine = engine, parser = parser, database = db)
}
}

companion object {
@JvmStatic
fun builder(
dataSource: FhirDataSource,
context: Context
) = Builder(dataSource, context)
}
companion object {
@JvmStatic
fun builder(dataSource: FhirDataSource, context: Context) = Builder(dataSource, context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@
package com.google.android.fhir

/** Thrown to indicate that the resource already exists. */
class ResourceAlreadyExistsException(
val type: String,
val id: String,
cause: Throwable
) : Exception("Resource with type $type and id $id already exists!", cause)
class ResourceAlreadyExistsException(val type: String, val id: String, cause: Throwable) :
Exception("Resource with type $type and id $id already exists!", cause)
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@
package com.google.android.fhir

/** Thrown to indicate that the requested resource is not found. */
class ResourceNotFoundException(
val type: String,
val id: String,
cause: Throwable
) : Exception("Resource not found with type $type and id $id!", cause)
class ResourceNotFoundException(val type: String, val id: String, cause: Throwable) :
Exception("Resource not found with type $type and id $id!", cause)
13 changes: 5 additions & 8 deletions core/src/main/java/com/google/android/fhir/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ import java.time.format.DateTimeFormatter
import java.util.Date
import java.util.Locale

/**
* Utility function to format a [Date] object using the system's default locale.
*/
/** Utility function to format a [Date] object using the system's default locale. */
@SuppressLint("NewApi")
internal fun Date.toTimeZoneString(): String {
val simpleDateFormat = DateTimeFormatter.ofPattern(
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
Locale.getDefault()
).withZone(ZoneId.systemDefault())
return simpleDateFormat.format(this.toInstant())
val simpleDateFormat =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault())
.withZone(ZoneId.systemDefault())
return simpleDateFormat.format(this.toInstant())
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,39 @@ package com.google.android.fhir.cql
import org.hl7.fhir.instance.model.api.IBase
import org.opencds.cqf.cql.model.R4FhirModelResolver

/** An [R4FhirModelResolver] on Android. */
/** An [R4FhirModelResolver] on Android. */
internal class AndroidR4FhirModelResolver : R4FhirModelResolver() {
override fun getContextPath(contextType: String?, targetType: String?): Any? {
var targetType: String? = targetType
if (targetType == null || contextType == null) {
return null
}
when (contextType) {
"Unspecified", "Population" -> return null
targetType -> return "id"
}

// Workaround for the issue of target type incorrectly including a namespace URI prefix.
// TODO: remove this.
if (targetType.startsWith(NAMESPACE_URI_PREFIX)) {
targetType = targetType.substring(NAMESPACE_URI_PREFIX.length)
}

val resourceDefinition = fhirContext.getResourceDefinition(targetType)
val theValue = this.createInstance(contextType)
val type = theValue.javaClass as Class<out IBase?>
val children = resourceDefinition.children
return children.asSequence()
.mapNotNull { child -> innerGetContextPath(child, type) }
.firstOrNull()
override fun getContextPath(contextType: String?, targetType: String?): Any? {
var targetType: String? = targetType
if (targetType == null || contextType == null) {
return null
}
when (contextType) {
"Unspecified", "Population" -> return null
targetType -> return "id"
}

companion object {
/**
* A prefix that is incorrectly included on Android due to the inconsistency of JSON
* deserialization.
*/
const val NAMESPACE_URI_PREFIX = "{http://hl7.org/fhir}"
// Workaround for the issue of target type incorrectly including a namespace URI prefix.
// TODO: remove this.
if (targetType.startsWith(NAMESPACE_URI_PREFIX)) {
targetType = targetType.substring(NAMESPACE_URI_PREFIX.length)
}

val resourceDefinition = fhirContext.getResourceDefinition(targetType)
val theValue = this.createInstance(contextType)
val type = theValue.javaClass as Class<out IBase?>
val children = resourceDefinition.children
return children
.asSequence()
.mapNotNull { child -> innerGetContextPath(child, type) }
.firstOrNull()
}

companion object {
/**
* A prefix that is incorrectly included on Android due to the inconsistency of JSON
* deserialization.
*/
const val NAMESPACE_URI_PREFIX = "{http://hl7.org/fhir}"
}
}
Loading

0 comments on commit dbbfb55

Please sign in to comment.