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.CommaSeparatedStringBuilder;
052import org.hl7.fhir.utilities.FhirPublication;
053import  org.hl7.fhir.utilities.StandardsStatus;
054/**
055 * Element Type: Base definition for all elements in a resource.
056 */
057@DatatypeDef(name="Element")
058public abstract class Element extends Base implements IBaseHasExtensions, IBaseElement {
059
060    /**
061     * Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.
062     */
063    @Child(name = "id", type = {StringType.class}, order=0, min=0, max=1, modifier=false, summary=false)
064    @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." )
065    protected StringType id;
066
067    /**
068     * 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.
069     */
070    @Child(name = "extension", type = {Extension.class}, order=1, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=false)
071    @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." )
072    protected List<Extension> extension;
073
074    private static final long serialVersionUID = -1452745816L;
075
076  /**
077   * Constructor
078   */
079    public Element() {
080      super();
081    }
082
083    /**
084     * @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
085     */
086    public StringType getIdElement() { 
087      if (this.id == null)
088        if (Configuration.errorOnAutoCreate())
089          throw new Error("Attempt to auto-create Element.id");
090        else if (Configuration.doAutoCreate())
091          this.id = new StringType(); // bb
092      return this.id;
093    }
094
095    public boolean hasIdElement() { 
096      return this.id != null && !this.id.isEmpty();
097    }
098
099    public boolean hasId() { 
100      return this.id != null && !this.id.isEmpty();
101    }
102
103    /**
104     * @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
105     */
106    public Element setIdElement(StringType value) { 
107      this.id = value;
108      return this;
109    }
110
111    /**
112     * @return Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.
113     */
114    public String getId() { 
115      return this.id == null ? null : this.id.getValue();
116    }
117
118    /**
119     * @param value Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.
120     */
121    public Element setId(String value) { 
122      if (Utilities.noString(value))
123        this.id = null;
124      else {
125        if (this.id == null)
126          this.id = new StringType();
127        this.id.setValue(value);
128      }
129      return this;
130    }
131
132    /**
133     * @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.)
134     */
135    public List<Extension> getExtension() { 
136      if (this.extension == null)
137        this.extension = new ArrayList<Extension>();
138      return this.extension;
139    }
140
141    /**
142     * @return Returns a reference to <code>this</code> for easy method chaining
143     */
144    public Element setExtension(List<Extension> theExtension) { 
145      this.extension = theExtension;
146      return this;
147    }
148
149    public boolean hasExtension() { 
150      if (this.extension == null)
151        return false;
152      for (Extension item : this.extension)
153        if (!item.isEmpty())
154          return true;
155      return false;
156    }
157
158    public Extension addExtension() { //3
159      Extension t = new Extension();
160      if (this.extension == null)
161        this.extension = new ArrayList<Extension>();
162      this.extension.add(t);
163      return t;
164    }
165
166    public Element addExtension(Extension t) { //3
167      if (t == null)
168        return this;
169      if (this.extension == null)
170        this.extension = new ArrayList<Extension>();
171      this.extension.add(t);
172      return this;
173    }
174
175    /**
176     * @return The first repetition of repeating field {@link #extension}, creating it if it does not already exist {3}
177     */
178    public Extension getExtensionFirstRep() { 
179      if (getExtension().isEmpty()) {
180        addExtension();
181      }
182      return getExtension().get(0);
183    }
184
185      protected void listChildren(List<Property> children) {
186        super.listChildren(children);
187        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));
188        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));
189      }
190
191      @Override
192      public Property getNamedProperty(int _hash, String _name, boolean _checkValid) throws FHIRException {
193        switch (_hash) {
194        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);
195        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);
196        default: return super.getNamedProperty(_hash, _name, _checkValid);
197        }
198
199      }
200
201      @Override
202      public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException {
203        switch (hash) {
204        case 3355: /*id*/ return this.id == null ? new Base[0] : new Base[] {this.id}; // StringType
205        case -612557761: /*extension*/ return this.extension == null ? new Base[0] : this.extension.toArray(new Base[this.extension.size()]); // Extension
206        default: return super.getProperty(hash, name, checkValid);
207        }
208
209      }
210
211      @Override
212      public Base setProperty(int hash, String name, Base value) throws FHIRException {
213        switch (hash) {
214        case 3355: // id
215          this.id = TypeConvertor.castToString(value); // StringType
216          return value;
217        case -612557761: // extension
218          this.getExtension().add(TypeConvertor.castToExtension(value)); // Extension
219          return value;
220        default: return super.setProperty(hash, name, value);
221        }
222
223      }
224
225      @Override
226      public Base setProperty(String name, Base value) throws FHIRException {
227        if (name.equals("id")) {
228          this.id = TypeConvertor.castToString(value); // StringType
229        } else if (name.equals("extension")) {
230          this.getExtension().add(TypeConvertor.castToExtension(value));
231        } else
232          return super.setProperty(name, value);
233        return value;
234      }
235
236      @Override
237      public void removeChild(String name, Base value) throws FHIRException {
238        if (name.equals("id")) {
239          this.id = null;
240        } else if (name.equals("extension")) {
241          this.getExtension().remove(value);
242        } else
243          super.removeChild(name, value);
244        
245      }
246
247      @Override
248      public Base makeProperty(int hash, String name) throws FHIRException {
249        switch (hash) {
250        case 3355:  return getIdElement();
251        case -612557761:  return addExtension(); 
252        default: return super.makeProperty(hash, name);
253        }
254
255      }
256
257      @Override
258      public String[] getTypesForProperty(int hash, String name) throws FHIRException {
259        switch (hash) {
260        case 3355: /*id*/ return new String[] {"string"};
261        case -612557761: /*extension*/ return new String[] {"Extension"};
262        default: return super.getTypesForProperty(hash, name);
263        }
264
265      }
266
267      @Override
268      public Base addChild(String name) throws FHIRException {
269        if (name.equals("id")) {
270          throw new FHIRException("Cannot call addChild on a singleton property Element.id");
271        }
272        else if (name.equals("extension")) {
273          return addExtension();
274        }
275        else
276          return super.addChild(name);
277      }
278
279  public String fhirType() {
280    return "Element";
281
282  }
283
284      public abstract Element copy();
285
286      public void copyValues(Element dst) {
287        super.copyValues(dst);
288        dst.id = id == null ? null : id.copy();
289        if (extension != null) {
290          dst.extension = new ArrayList<Extension>();
291          for (Extension i : extension)
292            dst.extension.add(i.copy());
293        };
294      }
295
296      @Override
297      public boolean equalsDeep(Base other_) {
298        if (!super.equalsDeep(other_))
299          return false;
300        if (!(other_ instanceof Element))
301          return false;
302        Element o = (Element) other_;
303        return compareDeep(id, o.id, true) && compareDeep(extension, o.extension, true);
304      }
305
306      @Override
307      public boolean equalsShallow(Base other_) {
308        if (!super.equalsShallow(other_))
309          return false;
310        if (!(other_ instanceof Element))
311          return false;
312        Element o = (Element) other_;
313        return compareValues(id, o.id, true);
314      }
315
316      public boolean isEmpty() {
317        return super.isEmpty() && ca.uhn.fhir.util.ElementUtil.isEmpty(id, extension);
318      }
319
320// Manual code (from Configuration.txt):
321 @Override
322  public String getIdBase() {
323    return getId();
324  }
325  
326  @Override
327  public void setIdBase(String value) {
328    setId(value);
329  }
330  
331  public void addExtension(String url, DataType value) {
332    if (disallowExtensions)
333      throw new Error("Extensions are not allowed in this context");
334    Extension ex = new Extension();
335    ex.setUrl(url);
336    ex.setValue(value);
337    getExtension().add(ex);    
338  }
339
340 
341  /**
342   * Returns an extension if one (and only one) matches the given URL.
343   * 
344   * Note: BackboneElements override this to look in matching Modifier Extensions too
345   * 
346   * @param theUrl The URL. Must not be blank or null.
347   * @return the matching extension, or null
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   /**
366    * Returns an extension if one (and only one) matches the given URL.
367    * 
368    * Note: BackboneElements override this to look in matching Modifier Extensions too
369    * 
370    * @param theUrls One or more URLs to match. Must not be blank or null.
371    * @return the matching extension, or null
372    */
373    public Extension getExtensionByUrl(String... theUrls) {
374      ArrayList<Extension> retVal = new ArrayList<Extension>();
375      for (Extension next : getExtension()) {
376        if (Utilities.existsInList(next.getUrl(), theUrls)) {
377          retVal.add(next);
378        }
379      }
380      if (retVal.size() == 0)
381        return null;
382      else {
383        org.apache.commons.lang3.Validate.isTrue(retVal.size() == 1, "Url "+CommaSeparatedStringBuilder.join(",", theUrls)+" must have only one match");
384        return retVal.get(0);
385      }
386    }
387   
388   /**
389    * Remove any extensions that match (by given URL).
390    * 
391    * Note: BackboneElements override this to remove from Modifier Extensions too
392    * 
393    * @param theUrl The URL. Must not be blank or null.
394    */
395   public void removeExtension(String theUrl) {
396     for (int i = getExtension().size()-1; i >= 0; i--) {
397       if (theUrl.equals(getExtension().get(i).getUrl()))
398         getExtension().remove(i);
399     }
400   }
401   
402   /**
403    * 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.
404    * todo: enforce this....
405    */
406    private boolean disallowExtensions;
407
408    public boolean isDisallowExtensions() {
409      return disallowExtensions;
410    }
411
412    public Element setDisallowExtensions(boolean disallowExtensions) {
413      this.disallowExtensions = disallowExtensions;
414      return this;
415    }
416
417    public Element noExtensions() {
418      this.disallowExtensions = true;
419      return this;
420    }
421  
422   /**
423    * Returns an unmodifiable list containing all extensions on this element which 
424    * match the given URL.
425    * 
426    * Note: BackboneElements override this to add matching Modifier Extensions too
427    * 
428    * @param theUrl The URL. Must not be blank or null.
429    * @return an unmodifiable list containing all extensions on this element which match the given URL
430    */
431   public List<Extension> getExtensionsByUrl(String theUrl) {
432     if (theUrl == null) {
433       throw new NullPointerException("theUrl must not be null");
434     } else if (theUrl.length() == 0) {
435       throw new IllegalArgumentException("theUrl must not be empty");
436     }
437     ArrayList<Extension> retVal = new ArrayList<>();
438     for (Extension next : getExtension()) {
439       if (theUrl.equals(next.getUrl())) {
440         retVal.add(next);
441       }
442     }
443     return java.util.Collections.unmodifiableList(retVal);
444   }
445   
446   public List<Extension> getExtensionsByUrl(String... theUrls) {
447     
448     ArrayList<Extension> retVal = new ArrayList<>();
449     for (Extension next : getExtension()) {
450       if (Utilities.existsInList(next.getUrl(), theUrls)) {
451         retVal.add(next);
452       }
453     }
454     return java.util.Collections.unmodifiableList(retVal);
455   }
456   
457
458   public boolean hasExtension(String... theUrls) {
459     for (Extension next : getExtension()) {
460       if (Utilities.existsInList(next.getUrl(), theUrls)) {
461         return true;
462       }
463     }
464     return false;
465   }
466
467   public Base getExtensionValue(String... theUrls) {
468     
469     for (Extension next : getExtension()) {
470       if (Utilities.existsInList(next.getUrl(), theUrls)) {
471         return next.getValue();
472       }
473     }
474     return null;
475   }
476
477   /**
478    * Returns an true if this element has an extension that matchs the given URL.
479    * 
480    * Note: BackboneElements override this to check Modifier Extensions too
481    * 
482    * @param theUrl The URL. Must not be blank or null.
483    */
484   public boolean hasExtension(String theUrl) {
485     if (extension == null || extension.size() == 0) {
486       return false;
487     }
488
489     for (Extension ext : extension) {
490       if (theUrl.equals(ext.getUrl())) {
491         return true;
492       }
493     }
494
495     return false;
496   }
497
498   /**
499    * 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.
500    * 
501    * Note: BackboneElements override this to check Modifier Extensions too
502    * 
503    * @param theUrl The URL. Must not be blank or null.
504    */
505   public String getExtensionString(String theUrl) throws FHIRException {
506     List<Extension> ext = getExtensionsByUrl(theUrl); 
507     if (ext.isEmpty()) 
508       return null; 
509     if (ext.size() > 1) 
510       throw new FHIRException("Multiple matching extensions found for extension '"+theUrl+"'");
511     if (!ext.get(0).hasValue())
512       return null;
513     if (!ext.get(0).getValue().isPrimitive())
514       throw new FHIRException("Extension '"+theUrl+"' could not be converted to a string");
515     return ext.get(0).getValue().primitiveValue();
516   }
517
518   public String getExtensionString(String... theUrls) throws FHIRException {
519     for (String url : theUrls) {
520       if (hasExtension(url)) {
521         return getExtensionString(url);
522       }
523     }
524     return null;
525   }
526
527
528  public StandardsStatus getStandardsStatus() {
529    return ToolingExtensions.getStandardsStatus(this);
530  }
531  
532  public void setStandardsStatus(StandardsStatus status) {
533    ToolingExtensions.setStandardsStatus(this, status, null);
534  }
535
536  public boolean hasExtension(Extension ext) {
537    if (hasExtension()) {
538      for (Extension t : getExtension()) {
539        if (Base.compareDeep(t, ext, false)) {
540          return true;
541        }
542      }
543    }
544    return false;
545  }
546  
547  public void copyExtensions(org.hl7.fhir.r5.model.Element src, String... urls) {
548    for (Extension e : src.getExtension()) {
549      if (Utilities.existsInList(e.getUrl(), urls)) {
550        addExtension(e.copy());
551      }
552    }    
553  }
554
555  public void copyNewExtensions(org.hl7.fhir.r5.model.Element src, String... urls) {
556    for (Extension e : src.getExtension()) {
557      if (Utilities.existsInList(e.getUrl(), urls) && !!hasExtension(e.getUrl())) {
558        addExtension(e.copy());
559      }
560    }    
561  }
562  
563
564  public FhirPublication getFHIRPublicationVersion() {
565    return FhirPublication.R5;
566  }
567// end addition
568
569}
570