001package org.hl7.fhir.r4.model; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030*/ 031 032// Generated on Thu, Sep 13, 2018 09:04-0400 for FHIR v3.5.0 033 034import java.util.*; 035 036import org.hl7.fhir.utilities.Utilities; 037import ca.uhn.fhir.model.api.annotation.Child; 038import ca.uhn.fhir.model.api.annotation.ChildOrder; 039import ca.uhn.fhir.model.api.annotation.Description; 040import ca.uhn.fhir.model.api.annotation.DatatypeDef; 041import ca.uhn.fhir.model.api.annotation.Block; 042import org.hl7.fhir.instance.model.api.*; 043import org.hl7.fhir.exceptions.FHIRException; 044/** 045 * Base definition for all elements in a resource. 046 */ 047public abstract class Element extends Base implements IBaseHasExtensions, IBaseElement { 048 049 /** 050 * Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces. 051 */ 052 @Child(name = "id", type = {StringType.class}, order=0, min=0, max=1, modifier=false, summary=false) 053 @Description(shortDefinition="xml:id (or equivalent in JSON)", formalDefinition="Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces." ) 054 protected StringType id; 055 056 /** 057 * May be used to represent additional information that is not part of the basic definition of the element. 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 can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. 058 */ 059 @Child(name = "extension", type = {Extension.class}, order=1, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=false) 060 @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 element. 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 can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension." ) 061 protected List<Extension> extension; 062 063 private static final long serialVersionUID = -1452745816L; 064 065 /** 066 * Constructor 067 */ 068 public Element() { 069 super(); 070 } 071 072 /** 073 * @return {@link #id} (Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.). This is the underlying object with id, value and extensions. The accessor "getId" gives direct access to the value 074 */ 075 public StringType getIdElement() { 076 if (this.id == null) 077 if (Configuration.errorOnAutoCreate()) 078 throw new Error("Attempt to auto-create Element.id"); 079 else if (Configuration.doAutoCreate()) 080 this.id = new StringType(); // bb 081 return this.id; 082 } 083 084 public boolean hasIdElement() { 085 return this.id != null && !this.id.isEmpty(); 086 } 087 088 public boolean hasId() { 089 return this.id != null && !this.id.isEmpty(); 090 } 091 092 /** 093 * @param value {@link #id} (Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.). This is the underlying object with id, value and extensions. The accessor "getId" gives direct access to the value 094 */ 095 public Element setIdElement(StringType value) { 096 this.id = value; 097 return this; 098 } 099 100 /** 101 * @return Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces. 102 */ 103 public String getId() { 104 return this.id == null ? null : this.id.getValue(); 105 } 106 107 /** 108 * @param value Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces. 109 */ 110 public Element setId(String value) { 111 if (Utilities.noString(value)) 112 this.id = null; 113 else { 114 if (this.id == null) 115 this.id = new StringType(); 116 this.id.setValue(value); 117 } 118 return this; 119 } 120 121 /** 122 * @return {@link #extension} (May be used to represent additional information that is not part of the basic definition of the element. 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 can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension.) 123 */ 124 public List<Extension> getExtension() { 125 if (this.extension == null) 126 this.extension = new ArrayList<Extension>(); 127 return this.extension; 128 } 129 130 /** 131 * @return Returns a reference to <code>this</code> for easy method chaining 132 */ 133 public Element setExtension(List<Extension> theExtension) { 134 this.extension = theExtension; 135 return this; 136 } 137 138 public boolean hasExtension() { 139 if (this.extension == null) 140 return false; 141 for (Extension item : this.extension) 142 if (!item.isEmpty()) 143 return true; 144 return false; 145 } 146 147 public Extension addExtension() { //3 148 Extension t = new Extension(); 149 if (this.extension == null) 150 this.extension = new ArrayList<Extension>(); 151 this.extension.add(t); 152 return t; 153 } 154 155 public Element addExtension(Extension t) { //3 156 if (t == null) 157 return this; 158 if (this.extension == null) 159 this.extension = new ArrayList<Extension>(); 160 this.extension.add(t); 161 return this; 162 } 163 164 /** 165 * @return The first repetition of repeating field {@link #extension}, creating it if it does not already exist 166 */ 167 public Extension getExtensionFirstRep() { 168 if (getExtension().isEmpty()) { 169 addExtension(); 170 } 171 return getExtension().get(0); 172 } 173 174 /** 175 * Returns an unmodifiable list containing all extensions on this element which 176 * match the given URL. 177 * 178 * @param theUrl The URL. Must not be blank or null. 179 * @return an unmodifiable list containing all extensions on this element which 180 * match the given URL 181 */ 182 public List<Extension> getExtensionsByUrl(String theUrl) { 183 org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null"); 184 ArrayList<Extension> retVal = new ArrayList<Extension>(); 185 for (Extension next : getExtension()) { 186 if (theUrl.equals(next.getUrl())) { 187 retVal.add(next); 188 } 189 } 190 return java.util.Collections.unmodifiableList(retVal); 191 } 192 public boolean hasExtension(String theUrl) { 193 return !getExtensionsByUrl(theUrl).isEmpty(); 194 } 195 196 public String getExtensionString(String theUrl) throws FHIRException { 197 List<Extension> ext = getExtensionsByUrl(theUrl); 198 if (ext.isEmpty()) 199 return null; 200 if (ext.size() > 1) 201 throw new FHIRException("Multiple matching extensions found"); 202 if (!ext.get(0).getValue().isPrimitive()) 203 throw new FHIRException("Extension could not be converted to a string"); 204 return ext.get(0).getValue().primitiveValue(); 205 } 206 207 protected void listChildren(List<Property> children) { 208 children.add(new Property("id", "string", "Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.", 0, 1, id)); 209 children.add(new Property("extension", "Extension", "May be used to represent additional information that is not part of the basic definition of the element. 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 can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension.", 0, java.lang.Integer.MAX_VALUE, extension)); 210 } 211 212 @Override 213 public Property getNamedProperty(int _hash, String _name, boolean _checkValid) throws FHIRException { 214 switch (_hash) { 215 case 3355: /*id*/ return new Property("id", "string", "Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.", 0, 1, id); 216 case -612557761: /*extension*/ return new Property("extension", "Extension", "May be used to represent additional information that is not part of the basic definition of the element. 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 can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension.", 0, java.lang.Integer.MAX_VALUE, extension); 217 default: return super.getNamedProperty(_hash, _name, _checkValid); 218 } 219 220 } 221 222 @Override 223 public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException { 224 switch (hash) { 225 case 3355: /*id*/ return this.id == null ? new Base[0] : new Base[] {this.id}; // StringType 226 case -612557761: /*extension*/ return this.extension == null ? new Base[0] : this.extension.toArray(new Base[this.extension.size()]); // Extension 227 default: return super.getProperty(hash, name, checkValid); 228 } 229 230 } 231 232 @Override 233 public Base setProperty(int hash, String name, Base value) throws FHIRException { 234 switch (hash) { 235 case 3355: // id 236 this.id = castToString(value); // StringType 237 return value; 238 case -612557761: // extension 239 this.getExtension().add(castToExtension(value)); // Extension 240 return value; 241 default: return super.setProperty(hash, name, value); 242 } 243 244 } 245 246 @Override 247 public Base setProperty(String name, Base value) throws FHIRException { 248 if (name.equals("id")) { 249 this.id = castToString(value); // StringType 250 } else if (name.equals("extension")) { 251 this.getExtension().add(castToExtension(value)); 252 } else 253 return super.setProperty(name, value); 254 return value; 255 } 256 257 @Override 258 public Base makeProperty(int hash, String name) throws FHIRException { 259 switch (hash) { 260 case 3355: return getIdElement(); 261 case -612557761: return addExtension(); 262 default: return super.makeProperty(hash, name); 263 } 264 265 } 266 267 @Override 268 public String[] getTypesForProperty(int hash, String name) throws FHIRException { 269 switch (hash) { 270 case 3355: /*id*/ return new String[] {"string"}; 271 case -612557761: /*extension*/ return new String[] {"Extension"}; 272 default: return super.getTypesForProperty(hash, name); 273 } 274 275 } 276 277 @Override 278 public Base addChild(String name) throws FHIRException { 279 if (name.equals("id")) { 280 throw new FHIRException("Cannot call addChild on a primitive type Element.id"); 281 } 282 else if (name.equals("extension")) { 283 return addExtension(); 284 } 285 else 286 return super.addChild(name); 287 } 288 289 public String fhirType() { 290 return "Element"; 291 292 } 293 294 public abstract Element copy(); 295 296 public void copyValues(Element dst) { 297 dst.id = id == null ? null : id.copy(); 298 if (extension != null) { 299 dst.extension = new ArrayList<Extension>(); 300 for (Extension i : extension) 301 dst.extension.add(i.copy()); 302 }; 303 } 304 305 @Override 306 public boolean equalsDeep(Base other_) { 307 if (!super.equalsDeep(other_)) 308 return false; 309 if (!(other_ instanceof Element)) 310 return false; 311 Element o = (Element) other_; 312 return compareDeep(id, o.id, true) && compareDeep(extension, o.extension, true); 313 } 314 315 @Override 316 public boolean equalsShallow(Base other_) { 317 if (!super.equalsShallow(other_)) 318 return false; 319 if (!(other_ instanceof Element)) 320 return false; 321 Element o = (Element) other_; 322 return compareValues(id, o.id, true); 323 } 324 325 public boolean isEmpty() { 326 return super.isEmpty() && ca.uhn.fhir.util.ElementUtil.isEmpty(id, extension); 327 } 328 329 @Override 330 public String getIdBase() { 331 return getId(); 332 } 333 334 @Override 335 public void setIdBase(String value) { 336 setId(value); 337 } 338// added from java-adornments.txt: 339 public void addExtension(String url, Type value) { 340 if (disallowExtensions) 341 throw new Error("Extensions are not allowed in this context"); 342 Extension ex = new Extension(); 343 ex.setUrl(url); 344 ex.setValue(value); 345 getExtension().add(ex); 346 } 347 348 349 public Extension getExtensionByUrl(String theUrl) { 350 org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null"); 351 ArrayList<Extension> retVal = new ArrayList<Extension>(); 352 for (Extension next : getExtension()) { 353 if (theUrl.equals(next.getUrl())) { 354 retVal.add(next); 355 } 356 } 357 if (retVal.size() == 0) 358 return null; 359 else { 360 org.apache.commons.lang3.Validate.isTrue(retVal.size() == 1, "Url "+theUrl+" must have only one match"); 361 return retVal.get(0); 362 } 363 } 364 365 public void removeExtension(String theUrl) { 366 for (int i = getExtension().size()-1; i >= 0; i--) { 367 if (theUrl.equals(getExtension().get(i).getUrl())) 368 getExtension().remove(i); 369 } 370 } 371 372 /** 373 * This is used in the FHIRPath engine to record that no extensions are allowed for this item in the context in which it is used. 374 * todo: enforce this.... 375 */ 376 private boolean disallowExtensions; 377 378 public boolean isDisallowExtensions() { 379 return disallowExtensions; 380 } 381 382 public Element setDisallowExtensions(boolean disallowExtensions) { 383 this.disallowExtensions = disallowExtensions; 384 return this; 385 } 386 387 public Element noExtensions() { 388 this.disallowExtensions = true; 389 return this; 390 } 391 392 393// end addition 394 395} 396