From fb9b123c655f75fd04e4be4d302ca5237861682a Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 10 Jun 2020 09:00:51 +1000 Subject: [PATCH] override Element extension methods to also check for modifierExtensions --- .../hl7/fhir/r5/model/BackboneElement.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BackboneElement.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BackboneElement.java index fd2c5bca1..74c7029e8 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BackboneElement.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BackboneElement.java @@ -241,6 +241,81 @@ Modifier extensions SHALL NOT change the meaning of any elements on Resource or } + public void addModifierExtension(String url, DataType value) { + if (isDisallowExtensions()) + throw new Error("Extensions are not allowed in this context"); + Extension ex = new Extension(); + ex.setUrl(url); + ex.setValue(value); + getModifierExtension().add(ex); + } + + + public Extension getExtensionByUrl(String theUrl) { + org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null"); + ArrayList retVal = new ArrayList(); + Extension res = super.getExtensionByUrl(theUrl); + if (res != null) { + retVal.add(res); + } + for (Extension next : getModifierExtension()) { + if (theUrl.equals(next.getUrl())) { + retVal.add(next); + } + } + if (retVal.size() == 0) + return null; + else { + org.apache.commons.lang3.Validate.isTrue(retVal.size() == 1, "Url "+theUrl+" must have only one match"); + return retVal.get(0); + } + } + + public void removeExtension(String theUrl) { + for (int i = getModifierExtension().size()-1; i >= 0; i--) { + if (theUrl.equals(getExtension().get(i).getUrl())) + getExtension().remove(i); + } + super.removeExtension(theUrl); + } + + + /** + * Returns an unmodifiable list containing all extensions on this element which + * match the given URL. + * + * @param theUrl The URL. Must not be blank or null. + * @return an unmodifiable list containing all extensions on this element which + * match the given URL + */ + public List getExtensionsByUrl(String theUrl) { + org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null"); + ArrayList retVal = new ArrayList(); + retVal.addAll(super.getExtensionsByUrl(theUrl)); + for (Extension next : getModifierExtension()) { + if (theUrl.equals(next.getUrl())) { + retVal.add(next); + } + } + return java.util.Collections.unmodifiableList(retVal); + } + + public boolean hasExtension(String theUrl) { + return !getExtensionsByUrl(theUrl).isEmpty(); + } + + + public String getExtensionString(String theUrl) throws FHIRException { + List ext = getExtensionsByUrl(theUrl); + if (ext.isEmpty()) + return null; + if (ext.size() > 1) + throw new FHIRException("Multiple matching extensions found"); + if (!ext.get(0).getValue().isPrimitive()) + throw new FHIRException("Extension could not be converted to a string"); + return ext.get(0).getValue().primitiveValue(); + } + // end addition } \ No newline at end of file