Code review fixes

This commit is contained in:
Nick Goupinets 2021-03-23 15:01:22 -04:00
parent 7a74a6c856
commit 8ead3c5f22
6 changed files with 48 additions and 42 deletions

View File

@ -23,14 +23,10 @@ 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 javax.annotation.Nonnull;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@ -50,7 +46,7 @@ public class ExtensionUtil {
*/
public static IBaseExtension<?, ?> getOrCreateExtension(IBase theBase, String theUrl) {
IBaseHasExtensions baseHasExtensions = validateExtensionSupport(theBase);
IBaseExtension extension = getExtension(baseHasExtensions, theUrl);
IBaseExtension extension = getExtensionByUrl(baseHasExtensions, theUrl);
if (extension == null) {
extension = baseHasExtensions.addExtension();
extension.setUrl(theUrl);
@ -108,7 +104,7 @@ public class ExtensionUtil {
return false;
}
return getExtension(baseHasExtensions, theExtensionUrl) != null;
return getExtensionByUrl(baseHasExtensions, theExtensionUrl) != null;
}
/**
@ -122,7 +118,7 @@ public class ExtensionUtil {
if (!hasExtension(theBase, theExtensionUrl)) {
return false;
}
IBaseDatatype value = getExtension((IBaseHasExtensions) theBase, theExtensionUrl).getValue();
IBaseDatatype value = getExtensionByUrl((IBaseHasExtensions) theBase, theExtensionUrl).getValue();
if (value == null) {
return theExtensionValue == null;
}
@ -136,7 +132,7 @@ public class ExtensionUtil {
* @param theExtensionUrl URL of the extension to get. Must be non-null
* @return Returns the first available extension with the specified URL, or null if such extension doesn't exist
*/
public static IBaseExtension<?, ?> getExtension(IBase theBase, String theExtensionUrl) {
public static IBaseExtension<?, ?> getExtensionByUrl(IBase theBase, String theExtensionUrl) {
Predicate<IBaseExtension> filter;
if (theExtensionUrl == null) {
filter = (e -> true);
@ -144,7 +140,7 @@ public class ExtensionUtil {
filter = (e -> theExtensionUrl.equals(e.getUrl()));
}
return getExtensions(theBase, filter)
return getExtensionsMatchingPredicate(theBase, filter)
.stream()
.findFirst()
.orElse(null);
@ -157,7 +153,7 @@ public class ExtensionUtil {
* @param theFilter Predicate to match the extension against
* @return Returns all extension with the specified URL, or an empty list if such extensions do not exist
*/
public static List<IBaseExtension<?, ?>> getExtensions(IBase theBase, Predicate<? super IBaseExtension> theFilter) {
public static List<IBaseExtension<?, ?>> getExtensionsMatchingPredicate(IBase theBase, Predicate<? super IBaseExtension> theFilter) {
return validateExtensionSupport(theBase)
.getExtension()
.stream()
@ -171,8 +167,19 @@ public class ExtensionUtil {
* @param theBase The resource to clear the extension for
* @return Returns all extension that were removed
*/
public static List<IBaseExtension<?, ?>> clearExtensions(IBase theBase) {
return clearExtensions(theBase, (e -> true));
public static List<IBaseExtension<?, ?>> clearAllExtensions(IBase theBase) {
return clearExtensionsMatchingPredicate(theBase, (e -> true));
}
/**
* Removes all extensions by URL.
*
* @param theBase The resource to clear the extension for
* @param theUrl The url to clear extensions for
* @return Returns all extension that were removed
*/
public static List<IBaseExtension<?, ?>> clearExtensionsByUrl(IBase theBase, String theUrl) {
return clearExtensionsMatchingPredicate(theBase, (e -> theUrl.equals(e.getUrl())));
}
/**
@ -182,8 +189,8 @@ public class ExtensionUtil {
* @param theFilter Defines which extensions should be cleared
* @return Returns all extension that were removed
*/
public static List<IBaseExtension<?, ?>> clearExtensions(IBase theBase, Predicate<? super IBaseExtension> theFilter) {
List<IBaseExtension<?, ?>> retVal = getExtensions(theBase, theFilter);
private static List<IBaseExtension<?, ?>> clearExtensionsMatchingPredicate(IBase theBase, Predicate<? super IBaseExtension> theFilter) {
List<IBaseExtension<?, ?>> retVal = getExtensionsMatchingPredicate(theBase, theFilter);
validateExtensionSupport(theBase)
.getExtension()
.removeIf(theFilter);
@ -197,9 +204,9 @@ public class ExtensionUtil {
* @param theExtensionUrl URL of the extension to get. Must be non-null
* @return Returns all extension with the specified URL, or an empty list if such extensions do not exist
*/
public static List<IBaseExtension<?, ?>> getExtensions(IBaseHasExtensions theBase, String theExtensionUrl) {
public static List<IBaseExtension<?, ?>> getExtensionsByUrl(IBaseHasExtensions theBase, String theExtensionUrl) {
Predicate<IBaseExtension> urlEqualityPredicate = e -> theExtensionUrl.equals(e.getUrl());
return getExtensions(theBase, urlEqualityPredicate);
return getExtensionsMatchingPredicate(theBase, urlEqualityPredicate);
}
/**
@ -210,7 +217,7 @@ public class ExtensionUtil {
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBaseExtension theExtension, String theValue) {
setExtension(theFhirContext, theExtension, "string", theValue);
setExtension(theFhirContext, theExtension, "string", (Object) theValue);
}
/**
@ -221,7 +228,7 @@ public class ExtensionUtil {
* @param theValue The value to set
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBaseExtension theExtension, String theExtensionType, String theValue) {
public static void setExtension(FhirContext theFhirContext, IBaseExtension theExtension, String theExtensionType, Object theValue) {
theExtension.setValue(TerserUtil.newElement(theFhirContext, theExtensionType, theValue));
}
@ -233,7 +240,7 @@ public class ExtensionUtil {
* @param theValue Extension value
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBase theBase, String theUrl, String theValue) {
public static void setExtensionAsString(FhirContext theFhirContext, IBase theBase, String theUrl, String theValue) {
IBaseExtension ext = getOrCreateExtension(theBase, theUrl);
setExtension(theFhirContext, ext, theValue);
}
@ -247,18 +254,19 @@ public class ExtensionUtil {
* @param theValue Extension value
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBase theBase, String theUrl, String theValueType, String theValue) {
public static void setExtension(FhirContext theFhirContext, IBase theBase, String theUrl, String theValueType, Object theValue) {
IBaseExtension ext = getOrCreateExtension(theBase, theUrl);
setExtension(theFhirContext, ext, theValue);
setExtension(theFhirContext, ext, theValueType, theValue);
}
/**
* Compares two extensions, returns true if they have the same value and url
*
* @param theLeftExtension : Extension to be evaluated #1
* @param theRightExtension : Extension to be evaluated #2
* @return Result of the comparison
*/
public static boolean equals(IBaseExtension theLeftExtension, IBaseExtension theRightExtension){
public static boolean equals(IBaseExtension theLeftExtension, IBaseExtension theRightExtension) {
return TerserUtil.equals(theLeftExtension, theRightExtension);
}
}

View File

@ -23,7 +23,6 @@ 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

@ -125,7 +125,7 @@ public final class TerserUtil {
* @return Returns the first value for the specified field or null if field with the provided name doesn't exist or
* has no values
*/
public static IBase getValue(FhirContext theFhirContext, IBaseResource theResource, String theFieldName) {
public static IBase getValueFirstRep(FhirContext theFhirContext, IBaseResource theResource, String theFieldName) {
List<IBase> values = getValues(theFhirContext, theResource, theFieldName);
if (values == null || values.isEmpty()) {
return null;

View File

@ -129,11 +129,11 @@ public class AddressValidatingInterceptor {
AddressValidationResult validationResult = getAddressValidator().isValid(theAddress, theFhirContext);
ourLog.debug("Validated address {}", validationResult);
ExtensionUtil.setExtension(theFhirContext, theAddress, IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL,
ExtensionUtil.setExtensionAsString(theFhirContext, theAddress, IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL,
validationResult.isValid() ? IAddressValidator.EXT_VALUE_VALID : IAddressValidator.EXT_VALUE_INVALID);
} catch (Exception ex) {
ourLog.warn("Unable to validate address", ex);
ExtensionUtil.setExtension(theFhirContext, theAddress, IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, IAddressValidator.EXT_UNABLE_TO_VALIDATE);
ExtensionUtil.setExtensionAsString(theFhirContext, theAddress, IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, IAddressValidator.EXT_UNABLE_TO_VALIDATE);
}
}

View File

@ -2,14 +2,15 @@ package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.instance.model.api.IBaseDatatype;
import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.PrimitiveType;
import org.hl7.fhir.r4.model.StringType;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ExtensionUtilTest {
@ -21,7 +22,7 @@ class ExtensionUtilTest {
void testExtensionsWork() {
Patient p1 = new Patient();
assertFalse(ExtensionUtil.hasExtension(p1, EXT_URL));
ExtensionUtil.setExtension(ourFhirContext, p1, EXT_URL, "value");
ExtensionUtil.setExtensionAsString(ourFhirContext, p1, EXT_URL, "value");
assertTrue(ExtensionUtil.hasExtension(p1, EXT_URL));
}
@ -32,10 +33,11 @@ class ExtensionUtilTest {
ExtensionUtil.setExtension(ourFhirContext, p1, EXT_URL, "integer", "1");
assertTrue(ExtensionUtil.hasExtension(p1, EXT_URL));
assertEquals(1, ExtensionUtil.getExtensions(p1, EXT_URL).size());
assertEquals(1, ExtensionUtil.getExtensionsByUrl(p1, EXT_URL).size());
IBaseDatatype ext = ExtensionUtil.getExtension(p1, EXT_URL).getValue();
assertEquals("1", ext.toString());
IBaseDatatype ext = ExtensionUtil.getExtensionByUrl(p1, EXT_URL).getValue();
assertEquals("integer", ext.fhirType());
assertEquals("1", ((PrimitiveType) ext).asStringValue());
}
@Test
@ -63,13 +65,13 @@ class ExtensionUtilTest {
p.addExtension("URL", new StringType("VALUE"));
p.addExtension("URL2", new StringType("VALUE2"));
ExtensionUtil.clearExtensions(p, e -> e.getUrl().equals("URL"));
ExtensionUtil.clearExtensionsByUrl(p, "URL");
assertEquals(1, p.getExtension().size());
assertFalse(ExtensionUtil.hasExtension(p, "URL"));
assertTrue(ExtensionUtil.hasExtension(p, "URL2"));
ExtensionUtil.clearExtensions(p);
ExtensionUtil.clearAllExtensions(p);
assertEquals(0, p.getExtension().size());
}

View File

@ -2,7 +2,6 @@ package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeResourceDefinition;
import org.checkerframework.checker.units.qual.A;
import org.hl7.fhir.r4.model.Address;
import org.hl7.fhir.r4.model.DateTimeType;
import org.hl7.fhir.r4.model.DateType;
@ -14,8 +13,6 @@ import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.PrimitiveType;
import org.junit.jupiter.api.Test;
import java.util.GregorianCalendar;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.*;
@ -278,10 +275,10 @@ class TerserUtilTest {
Patient p1 = new Patient();
p1.addName().setFamily("Doe");
assertEquals("Doe", ((HumanName) TerserUtil.getValue(ourFhirContext, p1, "name")).getFamily());
assertEquals("Doe", ((HumanName) TerserUtil.getValueFirstRep(ourFhirContext, p1, "name")).getFamily());
assertFalse(TerserUtil.getValues(ourFhirContext, p1, "name").isEmpty());
assertNull(TerserUtil.getValues(ourFhirContext, p1, "whoaIsThatReal"));
assertNull(TerserUtil.getValue(ourFhirContext, p1, "whoaIsThatReal"));
assertNull(TerserUtil.getValueFirstRep(ourFhirContext, p1, "whoaIsThatReal"));
}
@Test