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