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