refactored terserUtil, added equals function in ExtensionUtil

This commit is contained in:
long 2021-03-17 13:02:59 -06:00
parent 660777f855
commit 989e9e7903
7 changed files with 67 additions and 12 deletions

View File

@ -23,9 +23,12 @@ package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseBooleanDatatype;
import org.hl7.fhir.instance.model.api.IBaseDatatype;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
import org.hl7.fhir.instance.model.api.IBaseIntegerDatatype;
import java.util.List;
import java.util.stream.Collectors;
@ -175,4 +178,13 @@ public class ExtensionUtil {
setExtension(theFhirContext, ext, theValue);
}
/**
* Compares two extensions, returns true if they have the same value and url
* @param leftExtension
* @param rightExtension
* @return Result of the comparison
*/
public static boolean equals(IBaseExtension leftExtension, IBaseExtension rightExtension){
return TerserUtil.equals(leftExtension, rightExtension);
}
}

View File

@ -23,6 +23,7 @@ package ca.uhn.fhir.util;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import java.lang.reflect.Field;
import java.util.List;
import java.util.function.BiPredicate;
/**

View File

@ -30,6 +30,7 @@ import org.hl7.fhir.instance.model.api.IBaseResource;
import org.slf4j.Logger;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -157,6 +158,30 @@ public final class TerserUtil {
});
}
public static boolean equals(IBase theItem1, IBase theItem2){
Method method = null;
for (Method m : theItem1.getClass().getDeclaredMethods()) {
if (m.getName().equals("equalsDeep")) {
method = m;
break;
}
}
final Method m = method;
return equals(theItem1, theItem2, m);
}
public static boolean equals(IBase theItem1, IBase theItem2, Method m) {
if (m != null) {
try {
return (Boolean) m.invoke(theItem1, theItem2);
} catch (Exception e) {
throw new RuntimeException("Unable to compare equality via equalsDeep", e);
}
}
return theItem1.equals(theItem2);
}
private static boolean contains(IBase theItem, List<IBase> theItems) {
Method method = null;
for (Method m : theItem.getClass().getDeclaredMethods()) {
@ -168,14 +193,7 @@ public final class TerserUtil {
final Method m = method;
return theItems.stream().anyMatch(i -> {
if (m != null) {
try {
return (Boolean) m.invoke(theItem, i);
} catch (Exception e) {
throw new RuntimeException("Unable to compare equality via equalsDeep", e);
}
}
return theItem.equals(i);
return equals(i, theItem, m);
});
}

View File

@ -0,0 +1,4 @@
---
type: add
issue: 2478
title: "Added matching based on extension, when given the path to a fhir resource the matcher will take the extensions and match if the url and string value are the same"

View File

@ -403,6 +403,14 @@ The following algorithms are currently supported:
</td>
<td>If an optional "identifierSystem" is provided, then the identifiers only match when they belong to that system</td>
</tr>
<tr>
<td>EXTENSION_ANY_ORDER</td>
<td>matcher</td>
<td>
Matches when at least one system and value extension pair are identical.
</td>
<td>If an optional "identifierSystem" is provided, then the extensions only match when they belong to that system</td>
</tr>
<tr>
<td>EMPTY_FIELD</td>
<td>matcher</td>

View File

@ -1,11 +1,10 @@
package ca.uhn.fhir.mdm.rules.matcher;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.util.ExtensionUtil;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.StringType;
import java.util.List;
@ -28,8 +27,7 @@ public class ExtensionMatcher implements IMdmFieldMatcher{
if (leftExtensionValue.getUrl().equals(theIdentifierSystem) || theIdentifierSystem == null) {
for (IBaseExtension rightExtensionValue : rightExtension) {
if (rightExtensionValue.getUrl().equals(theIdentifierSystem) || theIdentifierSystem == null) {
match |= ((StringType) ((Extension) leftExtensionValue).getValue()).getValueAsString().equals(((StringType) ((Extension) rightExtensionValue).getValue()).getValueAsString())
&& leftExtensionValue.getUrl().equals(rightExtensionValue.getUrl());
match |= ExtensionUtil.equals(leftExtensionValue, rightExtensionValue);
}
}
}

View File

@ -1,6 +1,7 @@
package ca.uhn.fhir.mdm.rules.matcher;
import org.hl7.fhir.r4.model.IntegerType;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.StringType;
import org.junit.jupiter.api.Test;
@ -70,4 +71,17 @@ public class ExtensionMatcherR4Test extends BaseMatcherR4Test {
assertTrue(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, "asd"));
assertFalse(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, "url1"));
}
@Test
public void testPatientWithoutIntExtension(){
Patient patient1 = new Patient();
Patient patient2 = new Patient();
patient1.addExtension("asd", new IntegerType(123));
patient2.addExtension("asd", new IntegerType(123));
assertTrue(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, null));
}
}