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 Tue, Dec 28, 2021 07:16+1100 for FHIR v5.0.0-snapshot1
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;
050/**
051 * Base StructureDefinition for Element Type: Base definition for all elements in a resource.
052 */
053@DatatypeDef(name="Element")
054public abstract class Element extends Base implements IBaseHasExtensions, IBaseElement {
055
056    /**
057     * Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.
058     */
059    @Child(name = "id", type = {StringType.class}, order=0, min=0, max=1, modifier=false, summary=false)
060    @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." )
061    protected StringType id;
062
063    /**
064     * 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.
065     */
066    @Child(name = "extension", type = {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 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
081     */
082    public StringType getIdElement() { 
083      if (this.id == null)
084        if (Configuration.errorOnAutoCreate())
085          throw new Error("Attempt to auto-create Element.id");
086        else if (Configuration.doAutoCreate())
087          this.id = new StringType(); // bb
088      return this.id;
089    }
090
091    public boolean hasIdElement() { 
092      return this.id != null && !this.id.isEmpty();
093    }
094
095    public boolean hasId() { 
096      return this.id != null && !this.id.isEmpty();
097    }
098
099    /**
100     * @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
101     */
102    public Element setIdElement(StringType value) { 
103      this.id = value;
104      return this;
105    }
106
107    /**
108     * @return Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.
109     */
110    public String getId() { 
111      return this.id == null ? null : this.id.getValue();
112    }
113
114    /**
115     * @param value Unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.
116     */
117    public Element setId(String value) { 
118      if (Utilities.noString(value))
119        this.id = null;
120      else {
121        if (this.id == null)
122          this.id = new StringType();
123        this.id.setValue(value);
124      }
125      return this;
126    }
127
128    /**
129     * @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.)
130     */
131    public List<Extension> getExtension() { 
132      if (this.extension == null)
133        this.extension = new ArrayList<Extension>();
134      return this.extension;
135    }
136
137    /**
138     * @return Returns a reference to <code>this</code> for easy method chaining
139     */
140    public Element setExtension(List<Extension> theExtension) { 
141      this.extension = theExtension;
142      return this;
143    }
144
145    public boolean hasExtension() { 
146      if (this.extension == null)
147        return false;
148      for (Extension item : this.extension)
149        if (!item.isEmpty())
150          return true;
151      return false;
152    }
153
154    public Extension addExtension() { //3
155      Extension t = new Extension();
156      if (this.extension == null)
157        this.extension = new ArrayList<Extension>();
158      this.extension.add(t);
159      return t;
160    }
161
162    public Element addExtension(Extension t) { //3
163      if (t == null)
164        return this;
165      if (this.extension == null)
166        this.extension = new ArrayList<Extension>();
167      this.extension.add(t);
168      return this;
169    }
170
171    /**
172     * @return The first repetition of repeating field {@link #extension}, creating it if it does not already exist {3}
173     */
174    public Extension getExtensionFirstRep() { 
175      if (getExtension().isEmpty()) {
176        addExtension();
177      }
178      return getExtension().get(0);
179    }
180
181      protected void listChildren(List<Property> children) {
182        super.listChildren(children);
183        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));
184        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));
185      }
186
187      @Override
188      public Property getNamedProperty(int _hash, String _name, boolean _checkValid) throws FHIRException {
189        switch (_hash) {
190        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);
191        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);
192        default: return super.getNamedProperty(_hash, _name, _checkValid);
193        }
194
195      }
196
197      @Override
198      public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException {
199        switch (hash) {
200        case 3355: /*id*/ return this.id == null ? new Base[0] : new Base[] {this.id}; // StringType
201        case -612557761: /*extension*/ return this.extension == null ? new Base[0] : this.extension.toArray(new Base[this.extension.size()]); // Extension
202        default: return super.getProperty(hash, name, checkValid);
203        }
204
205      }
206
207      @Override
208      public Base setProperty(int hash, String name, Base value) throws FHIRException {
209        switch (hash) {
210        case 3355: // id
211          this.id = TypeConvertor.castToString(value); // StringType
212          return value;
213        case -612557761: // extension
214          this.getExtension().add(TypeConvertor.castToExtension(value)); // Extension
215          return value;
216        default: return super.setProperty(hash, name, value);
217        }
218
219      }
220
221      @Override
222      public Base setProperty(String name, Base value) throws FHIRException {
223        if (name.equals("id")) {
224          this.id = TypeConvertor.castToString(value); // StringType
225        } else if (name.equals("extension")) {
226          this.getExtension().add(TypeConvertor.castToExtension(value));
227        } else
228          return super.setProperty(name, value);
229        return value;
230      }
231
232      @Override
233      public Base makeProperty(int hash, String name) throws FHIRException {
234        switch (hash) {
235        case 3355:  return getIdElement();
236        case -612557761:  return addExtension(); 
237        default: return super.makeProperty(hash, name);
238        }
239
240      }
241
242      @Override
243      public String[] getTypesForProperty(int hash, String name) throws FHIRException {
244        switch (hash) {
245        case 3355: /*id*/ return new String[] {"string"};
246        case -612557761: /*extension*/ return new String[] {"Extension"};
247        default: return super.getTypesForProperty(hash, name);
248        }
249
250      }
251
252      @Override
253      public Base addChild(String name) throws FHIRException {
254        if (name.equals("id")) {
255          throw new FHIRException("Cannot call addChild on a primitive type Element.id");
256        }
257        else if (name.equals("extension")) {
258          return addExtension();
259        }
260        else
261          return super.addChild(name);
262      }
263
264  public String fhirType() {
265    return "Element";
266
267  }
268
269      public abstract Element copy();
270
271      public void copyValues(Element dst) {
272        super.copyValues(dst);
273        dst.id = id == null ? null : id.copy();
274        if (extension != null) {
275          dst.extension = new ArrayList<Extension>();
276          for (Extension i : extension)
277            dst.extension.add(i.copy());
278        };
279      }
280
281      @Override
282      public boolean equalsDeep(Base other_) {
283        if (!super.equalsDeep(other_))
284          return false;
285        if (!(other_ instanceof Element))
286          return false;
287        Element o = (Element) other_;
288        return compareDeep(id, o.id, true) && compareDeep(extension, o.extension, true);
289      }
290
291      @Override
292      public boolean equalsShallow(Base other_) {
293        if (!super.equalsShallow(other_))
294          return false;
295        if (!(other_ instanceof Element))
296          return false;
297        Element o = (Element) other_;
298        return compareValues(id, o.id, true);
299      }
300
301      public boolean isEmpty() {
302        return super.isEmpty() && ca.uhn.fhir.util.ElementUtil.isEmpty(id, extension);
303      }
304
305// Manual code (from Configuration.txt):
306 @Override
307  public String getIdBase() {
308    return getId();
309  }
310  
311  @Override
312  public void setIdBase(String value) {
313    setId(value);
314  }
315  
316  public void addExtension(String url, DataType value) {
317    if (disallowExtensions)
318      throw new Error("Extensions are not allowed in this context");
319    Extension ex = new Extension();
320    ex.setUrl(url);
321    ex.setValue(value);
322    getExtension().add(ex);    
323  }
324
325 
326  /**
327   * Returns an extension if one (and only one) matches the given URL.
328   * 
329   * Note: BackbdoneElements override this to look in matching Modifier Extensions too
330   * 
331   * @param theUrl The URL. Must not be blank or null.
332   * @return the matching extension, or null
333   */
334   public Extension getExtensionByUrl(String theUrl) {
335     org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null");
336     ArrayList<Extension> retVal = new ArrayList<Extension>();
337     for (Extension next : getExtension()) {
338       if (theUrl.equals(next.getUrl())) {
339         retVal.add(next);
340       }
341     }
342     if (retVal.size() == 0)
343       return null;
344     else {
345       org.apache.commons.lang3.Validate.isTrue(retVal.size() == 1, "Url "+theUrl+" must have only one match");
346       return retVal.get(0);
347     }
348   }
349  
350   /**
351    * Remove any extensions that match (by given URL).
352    * 
353    * Note: BackbdoneElements override this to remove from Modifier Extensions too
354    * 
355    * @param theUrl The URL. Must not be blank or null.
356    */
357   public void removeExtension(String theUrl) {
358     for (int i = getExtension().size()-1; i >= 0; i--) {
359       if (theUrl.equals(getExtension().get(i).getUrl()))
360         getExtension().remove(i);
361     }
362   }
363   
364   /**
365    * 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.
366    * todo: enforce this....
367    */
368    private boolean disallowExtensions;
369
370    public boolean isDisallowExtensions() {
371      return disallowExtensions;
372    }
373
374    public Element setDisallowExtensions(boolean disallowExtensions) {
375      this.disallowExtensions = disallowExtensions;
376      return this;
377    }
378
379    public Element noExtensions() {
380      this.disallowExtensions = true;
381      return this;
382    }
383  
384   /**
385    * Returns an unmodifiable list containing all extensions on this element which 
386    * match the given URL.
387    * 
388    * Note: BackbdoneElements override this to add matching Modifier Extensions too
389    * 
390    * @param theUrl The URL. Must not be blank or null.
391    * @return an unmodifiable list containing all extensions on this element which match the given URL
392    */
393   public List<Extension> getExtensionsByUrl(String theUrl) {
394     org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null");
395     ArrayList<Extension> retVal = new ArrayList<Extension>();
396     for (Extension next : getExtension()) {
397       if (theUrl.equals(next.getUrl())) {
398         retVal.add(next);
399       }
400     }
401     return java.util.Collections.unmodifiableList(retVal);
402   }
403   
404   /**
405    * Returns an true if this element has an extension that matchs the given URL.
406    * 
407    * Note: BackbdoneElements override this to check Modifier Extensions too
408    * 
409    * @param theUrl The URL. Must not be blank or null.
410    */
411   public boolean hasExtension(String theUrl) {
412     return !getExtensionsByUrl(theUrl).isEmpty(); 
413   }
414
415   /**
416    * 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.
417    * 
418    * Note: BackbdoneElements override this to check Modifier Extensions too
419    * 
420    * @param theUrl The URL. Must not be blank or null.
421    */
422  public String getExtensionString(String theUrl) throws FHIRException {
423    List<Extension> ext = getExtensionsByUrl(theUrl); 
424    if (ext.isEmpty()) 
425      return null; 
426    if (ext.size() > 1) 
427      throw new FHIRException("Multiple matching extensions found");
428    if (!ext.get(0).getValue().isPrimitive())
429      throw new FHIRException("Extension could not be converted to a string");
430    return ext.get(0).getValue().primitiveValue();
431  }
432
433// end addition
434
435}
436