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 Tue, May 12, 2020 07:26+1000 for FHIR v4.0.1
033import java.util.ArrayList;
034import java.util.List;
035
036import org.hl7.fhir.exceptions.FHIRException;
037import org.hl7.fhir.instance.model.api.IBaseElement;
038import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
039import org.hl7.fhir.utilities.StandardsStatus;
040import org.hl7.fhir.utilities.Utilities;
041
042import ca.uhn.fhir.model.api.annotation.Child;
043import ca.uhn.fhir.model.api.annotation.Description;
044
045/**
046 * Base definition for all elements in a resource.
047 */
048public abstract class Element extends Base implements IBaseHasExtensions, IBaseElement {
049
050  /**
051   * Unique id for the element within a resource (for internal references). This
052   * may be any string value that does not contain spaces.
053   */
054  @Child(name = "id", type = { StringType.class }, order = 0, min = 0, max = 1, modifier = false, summary = false)
055  @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.")
056  protected StringType id;
057
058  /**
059   * May be used to represent additional information that is not part of the basic
060   * definition of the element. To make the use of extensions safe and manageable,
061   * there is a strict set of governance applied to the definition and use of
062   * extensions. Though any implementer can define an extension, there is a set of
063   * requirements that SHALL be met as part of the definition of the extension.
064   */
065  @Child(name = "extension", type = {
066      Extension.class }, order = 1, min = 0, max = Child.MAX_UNLIMITED, modifier = false, summary = false)
067  @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.")
068  protected List<Extension> extension;
069
070  private static final long serialVersionUID = -1452745816L;
071
072  /**
073   * Constructor
074   */
075  public Element() {
076    super();
077  }
078
079  /**
080   * @return {@link #id} (Unique id for the element within a resource (for
081   *         internal references). This may be any string value that does not
082   *         contain spaces.). This is the underlying object with id, value and
083   *         extensions. The accessor "getId" gives direct access to the value
084   */
085  public StringType getIdElement() {
086    if (this.id == null)
087      if (Configuration.errorOnAutoCreate())
088        throw new Error("Attempt to auto-create Element.id");
089      else if (Configuration.doAutoCreate())
090        this.id = new StringType(); // bb
091    return this.id;
092  }
093
094  public boolean hasIdElement() {
095    return this.id != null && !this.id.isEmpty();
096  }
097
098  public boolean hasId() {
099    return this.id != null && !this.id.isEmpty();
100  }
101
102  /**
103   * @param value {@link #id} (Unique id for the element within a resource (for
104   *              internal references). This may be any string value that does not
105   *              contain spaces.). This is the underlying object with id, value
106   *              and extensions. The accessor "getId" gives direct access to the
107   *              value
108   */
109  public Element setIdElement(StringType value) {
110    this.id = value;
111    return this;
112  }
113
114  /**
115   * @return Unique id for the element within a resource (for internal
116   *         references). This may be any string value that does not contain
117   *         spaces.
118   */
119  public String getId() {
120    return this.id == null ? null : this.id.getValue();
121  }
122
123  /**
124   * @param value Unique id for the element within a resource (for internal
125   *              references). This may be any string value that does not contain
126   *              spaces.
127   */
128  public Element setId(String value) {
129    if (Utilities.noString(value))
130      this.id = null;
131    else {
132      if (this.id == null)
133        this.id = new StringType();
134      this.id.setValue(value);
135    }
136    return this;
137  }
138
139  /**
140   * @return {@link #extension} (May be used to represent additional information
141   *         that is not part of the basic definition of the element. To make the
142   *         use of extensions safe and manageable, there is a strict set of
143   *         governance applied to the definition and use of extensions. Though
144   *         any implementer can define an extension, there is a set of
145   *         requirements that SHALL be met as part of the definition of the
146   *         extension.)
147   */
148  public List<Extension> getExtension() {
149    if (this.extension == null)
150      this.extension = new ArrayList<Extension>();
151    return this.extension;
152  }
153
154  /**
155   * @return Returns a reference to <code>this</code> for easy method chaining
156   */
157  public Element setExtension(List<Extension> theExtension) {
158    this.extension = theExtension;
159    return this;
160  }
161
162  public boolean hasExtension() {
163    if (this.extension == null)
164      return false;
165    for (Extension item : this.extension)
166      if (!item.isEmpty())
167        return true;
168    return false;
169  }
170
171  public Extension addExtension() { // 3
172    Extension t = new Extension();
173    if (this.extension == null)
174      this.extension = new ArrayList<Extension>();
175    this.extension.add(t);
176    return t;
177  }
178
179  public Element addExtension(Extension t) { // 3
180    if (t == null)
181      return this;
182    if (this.extension == null)
183      this.extension = new ArrayList<Extension>();
184    this.extension.add(t);
185    return this;
186  }
187
188  /**
189   * @return The first repetition of repeating field {@link #extension}, creating
190   *         it if it does not already exist
191   */
192  public Extension getExtensionFirstRep() {
193    if (getExtension().isEmpty()) {
194      addExtension();
195    }
196    return getExtension().get(0);
197  }
198
199  /**
200   * Returns an unmodifiable list containing all extensions on this element which
201   * match the given URL.
202   * 
203   * @param theUrl The URL. Must not be blank or null.
204   * @return an unmodifiable list containing all extensions on this element which
205   *         match the given URL
206   */
207  public List<Extension> getExtensionsByUrl(String theUrl) {
208    org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null");
209    ArrayList<Extension> retVal = new ArrayList<Extension>();
210    for (Extension next : getExtension()) {
211      if (theUrl.equals(next.getUrl())) {
212        retVal.add(next);
213      }
214    }
215    return java.util.Collections.unmodifiableList(retVal);
216  }
217
218  public boolean hasExtension(String theUrl) {
219    return !getExtensionsByUrl(theUrl).isEmpty();
220  }
221
222  public String getExtensionString(String theUrl) throws FHIRException {
223    List<Extension> ext = getExtensionsByUrl(theUrl);
224    if (ext.isEmpty())
225      return null;
226    if (ext.size() > 1)
227      throw new FHIRException("Multiple matching extensions found for "+theUrl);
228    if (!ext.get(0).hasValue())
229      return null;
230    if (!ext.get(0).getValue().isPrimitive())
231      throw new FHIRException("Extension "+theUrl+" could not be converted to a string");
232    return ext.get(0).getValue().primitiveValue();
233  }
234
235  protected void listChildren(List<Property> children) {
236    children.add(new Property("id", "string",
237        "Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.",
238        0, 1, id));
239    children.add(new Property("extension", "Extension",
240        "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.",
241        0, java.lang.Integer.MAX_VALUE, extension));
242  }
243
244  @Override
245  public Property getNamedProperty(int _hash, String _name, boolean _checkValid) throws FHIRException {
246    switch (_hash) {
247    case 3355:
248      /* id */ return new Property("id", "string",
249          "Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.",
250          0, 1, id);
251    case -612557761:
252      /* extension */ return new Property("extension", "Extension",
253          "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.",
254          0, java.lang.Integer.MAX_VALUE, extension);
255    default:
256      return super.getNamedProperty(_hash, _name, _checkValid);
257    }
258
259  }
260
261  @Override
262  public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException {
263    switch (hash) {
264    case 3355:
265      /* id */ return this.id == null ? new Base[0] : new Base[] { this.id }; // StringType
266    case -612557761:
267      /* extension */ return this.extension == null ? new Base[0]
268          : this.extension.toArray(new Base[this.extension.size()]); // Extension
269    default:
270      return super.getProperty(hash, name, checkValid);
271    }
272
273  }
274
275  @Override
276  public Base setProperty(int hash, String name, Base value) throws FHIRException {
277    switch (hash) {
278    case 3355: // id
279      this.id = castToString(value); // StringType
280      return value;
281    case -612557761: // extension
282      this.getExtension().add(castToExtension(value)); // Extension
283      return value;
284    default:
285      return super.setProperty(hash, name, value);
286    }
287
288  }
289
290  @Override
291  public Base setProperty(String name, Base value) throws FHIRException {
292    if (name.equals("id")) {
293      this.id = castToString(value); // StringType
294    } else if (name.equals("extension")) {
295      this.getExtension().add(castToExtension(value));
296    } else
297      return super.setProperty(name, value);
298    return value;
299  }
300
301  @Override
302  public void removeChild(String name, Base value) throws FHIRException {
303    if (name.equals("id")) {
304      this.id = null;
305    } else if (name.equals("extension")) {
306      this.getExtension().remove(castToExtension(value));
307    } else
308      super.removeChild(name, value);
309
310  }
311
312  @Override
313  public Base makeProperty(int hash, String name) throws FHIRException {
314    switch (hash) {
315    case 3355:
316      return getIdElement();
317    case -612557761:
318      return addExtension();
319    default:
320      return super.makeProperty(hash, name);
321    }
322
323  }
324
325  @Override
326  public String[] getTypesForProperty(int hash, String name) throws FHIRException {
327    switch (hash) {
328    case 3355:
329      /* id */ return new String[] { "string" };
330    case -612557761:
331      /* extension */ return new String[] { "Extension" };
332    default:
333      return super.getTypesForProperty(hash, name);
334    }
335
336  }
337
338  @Override
339  public Base addChild(String name) throws FHIRException {
340    if (name.equals("id")) {
341      throw new FHIRException("Cannot call addChild on a singleton property Element.id");
342    } else if (name.equals("extension")) {
343      return addExtension();
344    } else
345      return super.addChild(name);
346  }
347
348  public String fhirType() {
349    return "Element";
350
351  }
352
353  public abstract Element copy();
354
355  public void copyValues(Element dst) {
356    dst.id = id == null ? null : id.copy();
357    if (extension != null) {
358      dst.extension = new ArrayList<Extension>();
359      for (Extension i : extension)
360        dst.extension.add(i.copy());
361    }
362    ;
363  }
364
365  @Override
366  public boolean equalsDeep(Base other_) {
367    if (!super.equalsDeep(other_))
368      return false;
369    if (!(other_ instanceof Element))
370      return false;
371    Element o = (Element) other_;
372    return compareDeep(id, o.id, true) && compareDeep(extension, o.extension, true);
373  }
374
375  @Override
376  public boolean equalsShallow(Base other_) {
377    if (!super.equalsShallow(other_))
378      return false;
379    if (!(other_ instanceof Element))
380      return false;
381    Element o = (Element) other_;
382    return compareValues(id, o.id, true);
383  }
384
385  public boolean isEmpty() {
386    return super.isEmpty() && ca.uhn.fhir.util.ElementUtil.isEmpty(id, extension);
387  }
388
389  @Override
390  public String getIdBase() {
391    return getId();
392  }
393
394  @Override
395  public void setIdBase(String value) {
396    setId(value);
397  }
398
399  // added from java-adornments.txt:
400  public void addExtension(String url, Type value) {
401    if (disallowExtensions)
402      throw new Error("Extensions are not allowed in this context");
403    Extension ex = new Extension();
404    ex.setUrl(url);
405    ex.setValue(value);
406    getExtension().add(ex);
407  }
408
409  public Extension getExtensionByUrl(String theUrl) {
410    org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null");
411    ArrayList<Extension> retVal = new ArrayList<Extension>();
412    for (Extension next : getExtension()) {
413      if (theUrl.equals(next.getUrl())) {
414        retVal.add(next);
415      }
416    }
417    if (retVal.size() == 0)
418      return null;
419    else {
420      org.apache.commons.lang3.Validate.isTrue(retVal.size() == 1, "Url " + theUrl + " must have only one match");
421      return retVal.get(0);
422    }
423  }
424
425  public void removeExtension(String theUrl) {
426    for (int i = getExtension().size() - 1; i >= 0; i--) {
427      if (theUrl.equals(getExtension().get(i).getUrl()))
428        getExtension().remove(i);
429    }
430  }
431
432  /**
433   * This is used in the FHIRPath engine to record that no extensions are allowed
434   * for this item in the context in which it is used. todo: enforce this....
435   */
436  private boolean disallowExtensions;
437
438  public boolean isDisallowExtensions() {
439    return disallowExtensions;
440  }
441
442  public Element setDisallowExtensions(boolean disallowExtensions) {
443    this.disallowExtensions = disallowExtensions;
444    return this;
445  }
446
447  public Element noExtensions() {
448    this.disallowExtensions = true;
449    return this;
450  }
451
452
453  public List<Extension> getExtensionsByUrl(String... theUrls) {
454
455    ArrayList<Extension> retVal = new ArrayList<>();
456    for (Extension next : getExtension()) {
457      if (Utilities.existsInList(next.getUrl(), theUrls)) {
458        retVal.add(next);
459      }
460    }
461    return java.util.Collections.unmodifiableList(retVal);
462  }
463
464
465  public boolean hasExtension(String... theUrls) {
466    for (Extension next : getExtension()) {
467      if (Utilities.existsInList(next.getUrl(), theUrls)) {
468        return true;
469      }
470    }
471    return false;
472  }
473
474
475
476
477  public boolean hasExtension(Extension ext) {
478    if (hasExtension()) {
479      for (Extension t : getExtension()) {
480        if (Base.compareDeep(t, ext, false)) {
481          return true;
482        }
483      }
484    }
485    return false;
486  }
487
488  public void copyExtensions(org.hl7.fhir.r4.model.Element src, String... urls) {
489    for (Extension e : src.getExtension()) {
490      if (Utilities.existsInList(e.getUrl(), urls)) {
491        addExtension(e.copy());
492      }
493    }    
494  }
495
496  public void copyNewExtensions(org.hl7.fhir.r4.model.Element src, String... urls) {
497    for (Extension e : src.getExtension()) {
498      if (Utilities.existsInList(e.getUrl(), urls) && !!hasExtension(e.getUrl())) {
499        addExtension(e.copy());
500      }
501    }    
502  }
503
504  // end addition
505
506}