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