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