diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/base/composite/BaseCodingDt.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/base/composite/BaseCodingDt.java index ae028096768..aefeb55e392 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/base/composite/BaseCodingDt.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/base/composite/BaseCodingDt.java @@ -91,6 +91,46 @@ public abstract class BaseCodingDt extends BaseIdentifiableElement implements IC return getCodeElement().equals(theCoding.getCodeElement()) && getSystemElement().equals(theCoding.getSystemElement()); } + /** + * returns true if this Coding matches a search for the coding specified by theSearchParam, according + * to the following: + * + * @param theSearchParam - coding to test this against + * @return true if the coding matches, false otherwise + */ + public boolean matchesToken(BaseCodingDt theSearchParam) { + if (theSearchParam.isSystemPresent()) { + if (theSearchParam.isSystemBlank()) { + // [parameter]=|[code] matches a code/value that has no system namespace + if (isSystemPresent() && !isSystemBlank()) + return false; + } else { + // [parameter]=[namespace]|[code] matches a code/value in the given system namespace + if (!isSystemPresent()) + return false; + if (!getSystemElement().equals(theSearchParam.getSystemElement())) + return false; + } + } else { + // [parameter]=[code] matches a code/value irrespective of it's system namespace + // (nothing to do for system for this case) + } + + return getCodeElement().equals(theSearchParam.getCodeElement()); + } + + private boolean isSystemPresent() { + return !getSystemElement().isEmpty(); + } + + private boolean isSystemBlank() { + return isSystemPresent() && getSystemElement().getValueAsString().equals(""); + } + /** * Sets the value for code (Symbol in syntax defined by the system) * @@ -109,4 +149,5 @@ public abstract class BaseCodingDt extends BaseIdentifiableElement implements IC */ public abstract BaseCodingDt setSystem(String theUri); + } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/TokenOrListParam.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/TokenOrListParam.java index ad733aee9ce..aeb7bdb093b 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/TokenOrListParam.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/TokenOrListParam.java @@ -94,4 +94,15 @@ public class TokenOrListParam extends BaseOrListParam { return new TokenParam(); } + public boolean doesCodingListMatch(List theCodings) { + List paramCodings = getListAsCodings(); + for (BaseCodingDt coding : theCodings) { + for (BaseCodingDt paramCoding : paramCodings) { + if (coding.matchesToken(paramCoding)) { + return true; + } + } + } + return false; + } } diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/model/base/composite/BaseCodingDtTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/model/base/composite/BaseCodingDtTest.java new file mode 100644 index 00000000000..3de4d73e83b --- /dev/null +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/model/base/composite/BaseCodingDtTest.java @@ -0,0 +1,71 @@ +package ca.uhn.fhir.model.base.composite; + +import static org.junit.Assert.*; + +import ca.uhn.fhir.model.dstu.composite.CodingDt; +import org.junit.Test; + +/** + * Created by Bill de Beaubien on 12/30/2014. + */ +public class BaseCodingDtTest { + private final CodingDt myTokenWithSystem = new CodingDt("http://foo.org", "53"); + private final CodingDt myTokenWithEmptySystem = new CodingDt("", "53"); + private final CodingDt myTokenWithoutSystem = new CodingDt(null, "53"); + + // [parameter]=[namespace]|[code] matches a code/value in the given system namespace + @Test + public void whenTokenIncludesSystem_CodingWithSameSystemAndCode_shouldMatch() { + assertTrue(new CodingDt("http://foo.org", "53").matchesToken(myTokenWithSystem)); + } + + @Test + public void whenTokenIncludesSystem_CodingWithDifferentSystem_shouldNotMatch() { + assertFalse(new CodingDt("http://bar.org", "53").matchesToken(myTokenWithSystem)); + } + + @Test + public void whenTokenIncludesSystem_CodingWithBlankSystem_shouldNotMatch() { + assertFalse(new CodingDt("", "53").matchesToken(myTokenWithSystem)); + } + + @Test + public void whenTokenIncludesSystem_CodingWithNoSystem_shouldNotMatch() { + assertFalse(new CodingDt(null, "53").matchesToken(myTokenWithSystem)); + } + + @Test + public void whenTokenIncludesSystem_CodingWithSameSystemAndDifferentCode_shouldNotMatch() { + assertFalse(new CodingDt("http://foo.org", "11").matchesToken(myTokenWithSystem)); + } + + @Test + public void whenTokenIncludesSystem_CodingWithSameSystemAndNoCode_shouldNotMatch() { + assertFalse(new CodingDt("http://foo.org", null).matchesToken(myTokenWithSystem)); + } + + // [parameter]=[code] matches a code/value irrespective of it's system namespace + @Test + public void whenTokenIncludesNoSystem_CodingWithAnySystemAndCode_shouldMatch() { + assertTrue(new CodingDt("http://foo.org", "53").matchesToken(myTokenWithoutSystem)); + assertTrue(new CodingDt("http://bar.org", "53").matchesToken(myTokenWithoutSystem)); + assertTrue(new CodingDt("", "53").matchesToken(myTokenWithoutSystem)); + assertTrue(new CodingDt(null, "53").matchesToken(myTokenWithoutSystem)); + } + + // [parameter]=|[code] matches a code/value that has no system namespace + @Test + public void whenTokenIncludesEmptySystem_CodeWithNoSystem_shouldMatch() { + assertTrue(new CodingDt(null, "53").matchesToken(myTokenWithEmptySystem)); + } + + @Test + public void whenTokenIncludesEmptySystem_CodeWithBlankSystem_shouldMatch() { + assertTrue(new CodingDt("", "53").matchesToken(myTokenWithEmptySystem)); + } + + @Test + public void whenTokenIncludesEmptySystem_CodeWithSystem_shouldNotMatch() { + assertFalse(new CodingDt("http://bar.org", "53").matchesToken(myTokenWithEmptySystem)); + } +} diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/param/TokenOrListParamTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/param/TokenOrListParamTest.java new file mode 100644 index 00000000000..2c941236ab7 --- /dev/null +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/param/TokenOrListParamTest.java @@ -0,0 +1,37 @@ +package ca.uhn.fhir.rest.param; + +import static org.junit.Assert.*; + +import ca.uhn.fhir.model.dstu.composite.CodingDt; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class TokenOrListParamTest { + @Test + public void whenParamListHasAnyMatchingCodingsForCodingList_doesCodingListMatch_shouldBeTrue() { + TokenOrListParam params = new TokenOrListParam(); + params.add("http://foo.org", "53"); + params.add("http://bar.org", "52"); + + List codings = new ArrayList(); + codings.add(new CodingDt("http://baz.org", "53")); + codings.add(new CodingDt("http://bar.org", "52")); + + assertTrue(params.doesCodingListMatch(codings)); + } + + @Test + public void whenParamListHasNoMatchingCodingsForCodingList_doesCodingListMatch_shouldBeFalse() { + TokenOrListParam params = new TokenOrListParam(); + params.add("http://foo.org", "53"); + params.add("http://bar.org", "52"); + + List codings = new ArrayList(); + codings.add(new CodingDt("http://baz.org", "53")); + codings.add(new CodingDt("http://bar.org", "11")); + + assertFalse(params.doesCodingListMatch(codings)); + } +}