
001package ca.uhn.fhir.model.api; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import ca.uhn.fhir.model.api.annotation.Child; 024import ca.uhn.fhir.model.api.annotation.Description; 025import org.apache.commons.lang3.Validate; 026import org.hl7.fhir.instance.model.api.IBaseDatatype; 027 028import java.util.*; 029 030public abstract class BaseElement implements /*IElement, */ISupportsUndeclaredExtensions { 031 032 private static final long serialVersionUID = -3092659584634499332L; 033 private List<String> myFormatCommentsPost; 034 private List<String> myFormatCommentsPre; 035 private Map<String, Object> userData; 036 037 @Child(name = "extension", type = {ExtensionDt.class}, order = 0, min = 0, max = Child.MAX_UNLIMITED, modifier = false, summary = false) 038 @Description(shortDefinition = "Additional Content defined by implementations", formalDefinition = "May be used to represent additional information that is not part of the basic definition of the resource. In order to make the use of extensions safe and manageable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension.") 039 private List<ExtensionDt> myUndeclaredExtensions; 040 041 /** 042 * May be used to represent additional information that is not part of the basic definition of the resource, and that modifies the understanding of the element that contains it. Usually modifier elements provide negation or qualification. In order to make the use of extensions safe and manageable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. Applications processing a resource are required to check for modifier extensions. 043 */ 044 @Child(name = "modifierExtension", type = {ExtensionDt.class}, order = 1, min = 0, max = Child.MAX_UNLIMITED, modifier = true, summary = false) 045 @Description(shortDefinition = "Extensions that cannot be ignored", formalDefinition = "May be used to represent additional information that is not part of the basic definition of the resource, and that modifies the understanding of the element that contains it. Usually modifier elements provide negation or qualification. In order to make the use of extensions safe and manageable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. Applications processing a resource are required to check for modifier extensions.") 046 private List<ExtensionDt> myUndeclaredModifierExtensions; 047 048 @Override 049 public ExtensionDt addUndeclaredExtension(boolean theIsModifier, String theUrl) { 050 Validate.notEmpty(theUrl, "URL must be populated"); 051 052 ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl); 053 if (theIsModifier) { 054 getUndeclaredModifierExtensions(); 055 myUndeclaredModifierExtensions.add(retVal); 056 } else { 057 getUndeclaredExtensions(); 058 myUndeclaredExtensions.add(retVal); 059 } 060 return retVal; 061 } 062 063 @Override 064 public ExtensionDt addUndeclaredExtension(boolean theIsModifier, String theUrl, IBaseDatatype theValue) { 065 Validate.notEmpty(theUrl, "URL must be populated"); 066 Validate.notNull(theValue, "Value must not be null"); 067 ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl, theValue); 068 if (theIsModifier) { 069 getUndeclaredModifierExtensions(); 070 myUndeclaredModifierExtensions.add(retVal); 071 } else { 072 getUndeclaredExtensions(); 073 myUndeclaredExtensions.add(retVal); 074 } 075 return retVal; 076 } 077 078 @Override 079 public void addUndeclaredExtension(ExtensionDt theExtension) { 080 Validate.notNull(theExtension, "Extension can not be null"); 081 if (theExtension.isModifier()) { 082 getUndeclaredModifierExtensions(); 083 myUndeclaredModifierExtensions.add(theExtension); 084 } else { 085 getUndeclaredExtensions(); 086 myUndeclaredExtensions.add(theExtension); 087 } 088 } 089 090 @Override 091 public List<ExtensionDt> getAllUndeclaredExtensions() { 092 ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>(); 093 if (myUndeclaredExtensions != null) { 094 retVal.addAll(myUndeclaredExtensions); 095 } 096 if (myUndeclaredModifierExtensions != null) { 097 retVal.addAll(myUndeclaredModifierExtensions); 098 } 099 return Collections.unmodifiableList(retVal); 100 } 101 102 @Override 103 public List<String> getFormatCommentsPost() { 104 if (myFormatCommentsPost == null) 105 myFormatCommentsPost = new ArrayList<String>(); 106 return myFormatCommentsPost; 107 } 108 109 @Override 110 public List<String> getFormatCommentsPre() { 111 if (myFormatCommentsPre == null) 112 myFormatCommentsPre = new ArrayList<String>(); 113 return myFormatCommentsPre; 114 } 115 116 @Override 117 public List<ExtensionDt> getUndeclaredExtensions() { 118 if (myUndeclaredExtensions == null) { 119 myUndeclaredExtensions = new ArrayList<ExtensionDt>(); 120 } 121 return (myUndeclaredExtensions); 122 } 123 124 @Override 125 public List<ExtensionDt> getUndeclaredExtensionsByUrl(String theUrl) { 126 org.apache.commons.lang3.Validate.notNull(theUrl, "URL can not be null"); 127 ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>(); 128 for (ExtensionDt next : getAllUndeclaredExtensions()) { 129 if (theUrl.equals(next.getUrlAsString())) { 130 retVal.add(next); 131 } 132 } 133 return Collections.unmodifiableList(retVal); 134 } 135 136 @Override 137 public List<ExtensionDt> getUndeclaredModifierExtensions() { 138 if (myUndeclaredModifierExtensions == null) { 139 myUndeclaredModifierExtensions = new ArrayList<ExtensionDt>(); 140 } 141 return (myUndeclaredModifierExtensions); 142 } 143 144 @Override 145 public boolean hasFormatComment() { 146 return (myFormatCommentsPre != null && !myFormatCommentsPre.isEmpty()) || (myFormatCommentsPost != null && !myFormatCommentsPost.isEmpty()); 147 } 148 149 @Override 150 public Object getUserData(String name) { 151 if (userData == null) 152 return null; 153 return userData.get(name); 154 } 155 156 @Override 157 public void setUserData(String name, Object value) { 158 if (userData == null) { 159 userData = new HashMap<>(); 160 } 161 userData.put(name, value); 162 } 163 164 /** 165 * Intended to be called by extending classes {@link #isEmpty()} implementations, returns <code>true</code> if all 166 * content in this superclass instance is empty per the semantics of {@link #isEmpty()}. 167 */ 168 protected boolean isBaseEmpty() { 169 if (myUndeclaredExtensions != null) { 170 for (ExtensionDt next : myUndeclaredExtensions) { 171 if (next == null) { 172 continue; 173 } 174 if (!next.isEmpty()) { 175 return false; 176 } 177 } 178 } 179 if (myUndeclaredModifierExtensions != null) { 180 for (ExtensionDt next : myUndeclaredModifierExtensions) { 181 if (next == null) { 182 continue; 183 } 184 if (!next.isEmpty()) { 185 return false; 186 } 187 } 188 } 189 return true; 190 } 191 192}