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