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