override Element extension methods to also check for modifierExtensions

This commit is contained in:
Grahame Grieve 2020-06-10 09:00:51 +10:00
parent 77d2cc394d
commit fb9b123c65
1 changed files with 75 additions and 0 deletions

View File

@ -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<Extension> retVal = new ArrayList<Extension>();
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<Extension> getExtensionsByUrl(String theUrl) {
org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null");
ArrayList<Extension> retVal = new ArrayList<Extension>();
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<Extension> 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
}