Skip to content

Commit

Permalink
Support Reference equality. (google#2435)
Browse files Browse the repository at this point in the history
* Support Reference equality.

* Add negative test.

---------

Co-authored-by: Santosh Pingle <[email protected]>
  • Loading branch information
santosh-pingle and Santosh Pingle authored Feb 19, 2024
1 parent 4169a8d commit f107597
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 3 additions & 1 deletion common/src/main/java/com/google/android/fhir/MoreTypes.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@ import java.util.Date
import java.util.Locale
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Quantity
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.StringType
import org.hl7.fhir.r4.model.Type

Expand All @@ -40,6 +41,7 @@ fun equals(a: Type, b: Type): Boolean {
// Codes with the same system and code values are considered equal even if they have different
// display values.
if (a is Coding && b is Coding) return a.system == b.system && a.code == b.code
if (a is Reference && b is Reference) return a.reference == b.reference

throw NotImplementedError("Comparison for type ${a::class.java} not supported.")
}
Expand Down
19 changes: 13 additions & 6 deletions common/src/test/java/com/google/android/fhir/MoreTypesTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Google LLC
* Copyright 2021-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -105,12 +105,19 @@ class MoreTypesTest {
}

@Test
fun equals_reference_shouldThrowException() {
val exception =
assertThrows(NotImplementedError::class.java) { equals(Reference(), Reference()) }
fun equals_reference_shouldReturnTrue() {
assertThat(
equals(Reference("Location/123"), Reference("Location/123")),
)
.isTrue()
}

assertThat(exception.message)
.isEqualTo("Comparison for type ${Reference::class.java} not supported.")
@Test
fun equals_differentReferences_shouldReturnFalse() {
assertThat(
equals(Reference("Location/123"), Reference("Location/abc")),
)
.isFalse()
}

@Test
Expand Down

0 comments on commit f107597

Please sign in to comment.