001package org.hl7.fhir.dstu3.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// Generated on Fri, Mar 16, 2018 15:21+1100 for FHIR v3.0.x
035import java.util.ArrayList;
036import java.util.List;
037import java.util.stream.Stream;
038
039import org.hl7.fhir.exceptions.FHIRException;
040import org.hl7.fhir.instance.model.api.IBaseElement;
041import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
042import org.hl7.fhir.utilities.Utilities;
043
044import ca.uhn.fhir.model.api.annotation.Child;
045import ca.uhn.fhir.model.api.annotation.Description;
046/**
047 * Base definition for all elements in a resource.
048 */
049public abstract class Element extends Base implements IBaseHasExtensions, IBaseElement {
050
051    /**
052     * unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.
053     */
054    @Child(name = "id", type = {StringType.class}, order=0, min=0, max=1, modifier=false, summary=false)
055    @Description(shortDefinition="xml:id (or equivalent in JSON)", formalDefinition="unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces." )
056    protected StringType id;
057
058    /**
059     * May be used to represent additional information that is not part of the basic definition of the element. In order 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 is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension.
060     */
061    @Child(name = "extension", type = {Extension.class}, order=1, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=false)
062    @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. In order 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 is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension." )
063    protected List<Extension> extension;
064
065    private static final long serialVersionUID = -1452745816L;
066
067  /**
068   * Constructor
069   */
070    public Element() {
071      super();
072    }
073
074    /**
075     * @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
076     */
077    public StringType getIdElement() { 
078      if (this.id == null)
079        if (Configuration.errorOnAutoCreate())
080          throw new Error("Attempt to auto-create Element.id");
081        else if (Configuration.doAutoCreate())
082          this.id = new StringType(); // bb
083      return this.id;
084    }
085
086    public boolean hasIdElement() { 
087      return this.id != null && !this.id.isEmpty();
088    }
089
090    public boolean hasId() { 
091      return this.id != null && !this.id.isEmpty();
092    }
093
094    /**
095     * @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
096     */
097    public Element setIdElement(StringType value) { 
098      this.id = value;
099      return this;
100    }
101
102    /**
103     * @return unique id for the element within a resource (for internal references). This may be any string value that does not contain spaces.
104     */
105    public String getId() { 
106      return this.id == null ? null : this.id.getValue();
107    }
108
109    /**
110     * @param value 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 Element setId(String value) { 
113      if (Utilities.noString(value))
114        this.id = null;
115      else {
116        if (this.id == null)
117          this.id = new StringType();
118        this.id.setValue(value);
119      }
120      return this;
121    }
122
123    /**
124     * @return {@link #extension} (May be used to represent additional information that is not part of the basic definition of the element. In order 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 is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension.)
125     */
126    public List<Extension> getExtension() { 
127      if (this.extension == null)
128        this.extension = new ArrayList<Extension>();
129      return this.extension;
130    }
131
132    /**
133     * @return Returns a reference to <code>this</code> for easy method chaining
134     */
135    public Element setExtension(List<Extension> theExtension) { 
136      this.extension = theExtension;
137      return this;
138    }
139
140    public boolean hasExtension() { 
141      if (this.extension == null)
142        return false;
143      for (Extension item : this.extension)
144        if (!item.isEmpty())
145          return true;
146      return false;
147    }
148
149    public Extension addExtension() { //3
150      Extension t = new Extension();
151      if (this.extension == null)
152        this.extension = new ArrayList<Extension>();
153      this.extension.add(t);
154      return t;
155    }
156
157    public Element addExtension(Extension t) { //3
158      if (t == null)
159        return this;
160      if (this.extension == null)
161        this.extension = new ArrayList<Extension>();
162      this.extension.add(t);
163      return this;
164    }
165
166    /**
167     * @return The first repetition of repeating field {@link #extension}, creating it if it does not already exist
168     */
169    public Extension getExtensionFirstRep() { 
170      if (getExtension().isEmpty()) {
171        addExtension();
172      }
173      return getExtension().get(0);
174    }
175
176
177    public Extension getExtensionByUrl(String theUrl) {
178      org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null");
179      ArrayList<Extension> retVal = new ArrayList<Extension>();
180      for (Extension next : getExtension()) {
181        if (theUrl.equals(next.getUrl())) {
182          retVal.add(next);
183        }
184      }
185      if (retVal.size() == 0)
186        return null;
187      else {
188        org.apache.commons.lang3.Validate.isTrue(retVal.size() == 1, "Url "+theUrl+" must have only one match");
189        return retVal.get(0);
190      }
191    }
192
193   /**
194    * Returns an unmodifiable list containing all extensions on this element which 
195    * match the given URL.
196    * 
197    * @param theUrl The URL. Must not be blank or null.
198    * @return an unmodifiable list containing all extensions on this element which 
199    * match the given URL
200    */
201   public List<Extension> getExtensionsByUrl(String theUrl) {
202     org.apache.commons.lang3.Validate.notBlank(theUrl, "theUrl must not be blank or null");
203     ArrayList<Extension> retVal = new ArrayList<Extension>();
204     for (Extension next : getExtension()) {
205       if (theUrl.equals(next.getUrl())) {
206         retVal.add(next);
207       }
208     }
209     return java.util.Collections.unmodifiableList(retVal);
210   }
211  public boolean hasExtension(String theUrl) {
212    return !getExtensionsByUrl(theUrl).isEmpty(); 
213  }
214
215  public String getExtensionString(String theUrl) throws FHIRException {
216    List<Extension> ext = getExtensionsByUrl(theUrl); 
217    if (ext.isEmpty()) 
218      return null; 
219    if (ext.size() > 1) 
220      throw new FHIRException("Multiple matching extensions found");
221    if (!ext.get(0).getValue().isPrimitive())
222      throw new FHIRException("Extension could not be converted to a string");
223    return ext.get(0).getValue().primitiveValue();
224  }
225
226      protected void listChildren(List<Property> children) {
227        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));
228        children.add(new Property("extension", "Extension", "May be used to represent additional information that is not part of the basic definition of the element. In order 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 is allowed to 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));
229      }
230
231      @Override
232      public Property getNamedProperty(int _hash, String _name, boolean _checkValid) throws FHIRException {
233        switch (_hash) {
234        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);
235        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. In order 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 is allowed to 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);
236        default: return super.getNamedProperty(_hash, _name, _checkValid);
237        }
238
239      }
240
241      @Override
242      public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException {
243        switch (hash) {
244        case 3355: /*id*/ return this.id == null ? new Base[0] : new Base[] {this.id}; // StringType
245        case -612557761: /*extension*/ return this.extension == null ? new Base[0] : this.extension.toArray(new Base[this.extension.size()]); // Extension
246        default: return super.getProperty(hash, name, checkValid);
247        }
248
249      }
250
251      @Override
252      public Base setProperty(int hash, String name, Base value) throws FHIRException {
253        switch (hash) {
254        case 3355: // id
255          this.id = castToString(value); // StringType
256          return value;
257        case -612557761: // extension
258          this.getExtension().add(castToExtension(value)); // Extension
259          return value;
260        default: return super.setProperty(hash, name, value);
261        }
262
263      }
264
265      @Override
266      public Base setProperty(String name, Base value) throws FHIRException {
267        if (name.equals("id")) {
268          this.id = castToString(value); // StringType
269        } else if (name.equals("extension")) {
270          this.getExtension().add(castToExtension(value));
271        } else
272          return super.setProperty(name, value);
273        return value;
274      }
275
276      @Override
277      public Base makeProperty(int hash, String name) throws FHIRException {
278        switch (hash) {
279        case 3355:  return getIdElement();
280        case -612557761:  return addExtension(); 
281        default: return super.makeProperty(hash, name);
282        }
283
284      }
285
286      @Override
287      public String[] getTypesForProperty(int hash, String name) throws FHIRException {
288        switch (hash) {
289        case 3355: /*id*/ return new String[] {"string"};
290        case -612557761: /*extension*/ return new String[] {"Extension"};
291        default: return super.getTypesForProperty(hash, name);
292        }
293
294      }
295
296      @Override
297      public Base addChild(String name) throws FHIRException {
298        if (name.equals("id")) {
299          throw new FHIRException("Cannot call addChild on a primitive type Element.id");
300        }
301        else if (name.equals("extension")) {
302          return addExtension();
303        }
304        else
305          return super.addChild(name);
306      }
307
308  public String fhirType() {
309    return "Element";
310
311  }
312
313      public abstract Element copy();
314
315      public void copyValues(Element dst) {
316        dst.id = id == null ? null : id.copy();
317        if (extension != null) {
318          dst.extension = new ArrayList<Extension>();
319          for (Extension i : extension)
320            dst.extension.add(i.copy());
321        };
322      }
323
324      @Override
325      public boolean equalsDeep(Base other_) {
326        if (!super.equalsDeep(other_))
327          return false;
328        if (!(other_ instanceof Element))
329          return false;
330        Element o = (Element) other_;
331        return compareDeep(id, o.id, true) && compareDeep(extension, o.extension, true);
332      }
333
334      @Override
335      public boolean equalsShallow(Base other_) {
336        if (!super.equalsShallow(other_))
337          return false;
338        if (!(other_ instanceof Element))
339          return false;
340        Element o = (Element) other_;
341        return compareValues(id, o.id, true);
342      }
343
344      public boolean isEmpty() {
345        return super.isEmpty() && ca.uhn.fhir.util.ElementUtil.isEmpty(id, extension);
346      }
347
348  @Override
349  public String getIdBase() {
350    return getId();
351  }
352  
353  @Override
354  public void setIdBase(String value) {
355    setId(value);
356  }
357// added from java-adornments.txt:
358  public void addExtension(String url, Type value) {
359    Extension ex = new Extension();
360    ex.setUrl(url);
361    ex.setValue(value);
362    getExtension().add(ex);    
363  }
364
365
366// end addition
367
368}