#73 - added matchesToken method to BaseCodingDt and doesCodingListMatch to TokenOrListParam
This commit is contained in:
parent
8e73705000
commit
f7209b45ef
|
@ -91,6 +91,46 @@ public abstract class BaseCodingDt extends BaseIdentifiableElement implements IC
|
|||
return getCodeElement().equals(theCoding.getCodeElement()) && getSystemElement().equals(theCoding.getSystemElement());
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if <code>this</code> Coding matches a search for the coding specified by <code>theSearchParam</code>, according
|
||||
* to the following:
|
||||
* <ul>
|
||||
* <li>[parameter]=[namespace]|[code] matches a code/value in the given system namespace</li>
|
||||
* <li>[parameter]=[code] matches a code/value irrespective of it's system namespace</li>
|
||||
* <li>[parameter]=|[code] matches a code/value that has no system namespace</li>
|
||||
* </ul>
|
||||
* @param theSearchParam - coding to test <code>this</code> 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 <b>code</b> (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);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -94,4 +94,15 @@ public class TokenOrListParam extends BaseOrListParam<TokenParam> {
|
|||
return new TokenParam();
|
||||
}
|
||||
|
||||
public boolean doesCodingListMatch(List<? extends BaseCodingDt> theCodings) {
|
||||
List<BaseCodingDt> paramCodings = getListAsCodings();
|
||||
for (BaseCodingDt coding : theCodings) {
|
||||
for (BaseCodingDt paramCoding : paramCodings) {
|
||||
if (coding.matchesToken(paramCoding)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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<CodingDt> codings = new ArrayList<CodingDt>();
|
||||
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<CodingDt> codings = new ArrayList<CodingDt>();
|
||||
codings.add(new CodingDt("http://baz.org", "53"));
|
||||
codings.add(new CodingDt("http://bar.org", "11"));
|
||||
|
||||
assertFalse(params.doesCodingListMatch(codings));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue