001package org.hl7.fhir.dstu2.model;
002
003import java.math.BigDecimal;
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 Wed, Jul 13, 2016 05:32+1000 for FHIR v1.0.2
035import java.util.List;
036
037import ca.uhn.fhir.model.api.annotation.Child;
038import ca.uhn.fhir.model.api.annotation.DatatypeDef;
039import ca.uhn.fhir.model.api.annotation.Description;
040import org.hl7.fhir.instance.model.api.ICompositeType;
041import org.hl7.fhir.exceptions.FHIRException;
042import org.hl7.fhir.utilities.Utilities;
043
044/**
045 * A measured amount (or an amount that can potentially be measured). Note that
046 * measured amounts include amounts that are not precisely quantified, including
047 * amounts involving arbitrary units and floating currencies.
048 */
049@DatatypeDef(name = "Quantity")
050public class Quantity extends Type implements ICompositeType {
051
052  public enum QuantityComparator {
053    /**
054     * The actual value is less than the given value.
055     */
056    LESS_THAN,
057    /**
058     * The actual value is less than or equal to the given value.
059     */
060    LESS_OR_EQUAL,
061    /**
062     * The actual value is greater than or equal to the given value.
063     */
064    GREATER_OR_EQUAL,
065    /**
066     * The actual value is greater than the given value.
067     */
068    GREATER_THAN,
069    /**
070     * added to help the parsers
071     */
072    NULL;
073
074    public static QuantityComparator fromCode(String codeString) throws FHIRException {
075      if (codeString == null || "".equals(codeString))
076        return null;
077      if ("<".equals(codeString))
078        return LESS_THAN;
079      if ("<=".equals(codeString))
080        return LESS_OR_EQUAL;
081      if (">=".equals(codeString))
082        return GREATER_OR_EQUAL;
083      if (">".equals(codeString))
084        return GREATER_THAN;
085      throw new FHIRException("Unknown QuantityComparator code '" + codeString + "'");
086    }
087
088    public String toCode() {
089      switch (this) {
090      case LESS_THAN:
091        return "<";
092      case LESS_OR_EQUAL:
093        return "<=";
094      case GREATER_OR_EQUAL:
095        return ">=";
096      case GREATER_THAN:
097        return ">";
098      case NULL:
099        return null;
100      default:
101        return "?";
102      }
103    }
104
105    public String getSystem() {
106      switch (this) {
107      case LESS_THAN:
108        return "http://hl7.org/fhir/quantity-comparator";
109      case LESS_OR_EQUAL:
110        return "http://hl7.org/fhir/quantity-comparator";
111      case GREATER_OR_EQUAL:
112        return "http://hl7.org/fhir/quantity-comparator";
113      case GREATER_THAN:
114        return "http://hl7.org/fhir/quantity-comparator";
115      case NULL:
116        return null;
117      default:
118        return "?";
119      }
120    }
121
122    public String getDefinition() {
123      switch (this) {
124      case LESS_THAN:
125        return "The actual value is less than the given value.";
126      case LESS_OR_EQUAL:
127        return "The actual value is less than or equal to the given value.";
128      case GREATER_OR_EQUAL:
129        return "The actual value is greater than or equal to the given value.";
130      case GREATER_THAN:
131        return "The actual value is greater than the given value.";
132      case NULL:
133        return null;
134      default:
135        return "?";
136      }
137    }
138
139    public String getDisplay() {
140      switch (this) {
141      case LESS_THAN:
142        return "Less than";
143      case LESS_OR_EQUAL:
144        return "Less or Equal to";
145      case GREATER_OR_EQUAL:
146        return "Greater or Equal to";
147      case GREATER_THAN:
148        return "Greater than";
149      case NULL:
150        return null;
151      default:
152        return "?";
153      }
154    }
155  }
156
157  public static class QuantityComparatorEnumFactory implements EnumFactory<QuantityComparator> {
158    public QuantityComparator fromCode(String codeString) throws IllegalArgumentException {
159      if (codeString == null || "".equals(codeString))
160        if (codeString == null || "".equals(codeString))
161          return null;
162      if ("<".equals(codeString))
163        return QuantityComparator.LESS_THAN;
164      if ("<=".equals(codeString))
165        return QuantityComparator.LESS_OR_EQUAL;
166      if (">=".equals(codeString))
167        return QuantityComparator.GREATER_OR_EQUAL;
168      if (">".equals(codeString))
169        return QuantityComparator.GREATER_THAN;
170      throw new IllegalArgumentException("Unknown QuantityComparator code '" + codeString + "'");
171    }
172
173    public Enumeration<QuantityComparator> fromType(Base code) throws FHIRException {
174      if (code == null || code.isEmpty())
175        return null;
176      String codeString = ((PrimitiveType) code).asStringValue();
177      if (codeString == null || "".equals(codeString))
178        return null;
179      if ("<".equals(codeString))
180        return new Enumeration<QuantityComparator>(this, QuantityComparator.LESS_THAN);
181      if ("<=".equals(codeString))
182        return new Enumeration<QuantityComparator>(this, QuantityComparator.LESS_OR_EQUAL);
183      if (">=".equals(codeString))
184        return new Enumeration<QuantityComparator>(this, QuantityComparator.GREATER_OR_EQUAL);
185      if (">".equals(codeString))
186        return new Enumeration<QuantityComparator>(this, QuantityComparator.GREATER_THAN);
187      throw new FHIRException("Unknown QuantityComparator code '" + codeString + "'");
188    }
189
190    public String toCode(QuantityComparator code) {
191      if (code == QuantityComparator.LESS_THAN)
192        return "<";
193      if (code == QuantityComparator.LESS_OR_EQUAL)
194        return "<=";
195      if (code == QuantityComparator.GREATER_OR_EQUAL)
196        return ">=";
197      if (code == QuantityComparator.GREATER_THAN)
198        return ">";
199      return "?";
200    }
201  }
202
203  /**
204   * The value of the measured amount. The value includes an implicit precision in
205   * the presentation of the value.
206   */
207  @Child(name = "value", type = { DecimalType.class }, order = 0, min = 0, max = 1, modifier = false, summary = true)
208  @Description(shortDefinition = "Numerical value (with implicit precision)", formalDefinition = "The value of the measured amount. The value includes an implicit precision in the presentation of the value.")
209  protected DecimalType value;
210
211  /**
212   * How the value should be understood and represented - whether the actual value
213   * is greater or less than the stated value due to measurement issues; e.g. if
214   * the comparator is "<" , then the real value is < stated value.
215   */
216  @Child(name = "comparator", type = { CodeType.class }, order = 1, min = 0, max = 1, modifier = true, summary = true)
217  @Description(shortDefinition = "< | <= | >= | > - how to understand the value", formalDefinition = "How the value should be understood and represented - whether the actual value is greater or less than the stated value due to measurement issues; e.g. if the comparator is \"<\" , then the real value is < stated value.")
218  protected Enumeration<QuantityComparator> comparator;
219
220  /**
221   * A human-readable form of the unit.
222   */
223  @Child(name = "unit", type = { StringType.class }, order = 2, min = 0, max = 1, modifier = false, summary = true)
224  @Description(shortDefinition = "Unit representation", formalDefinition = "A human-readable form of the unit.")
225  protected StringType unit;
226
227  /**
228   * The identification of the system that provides the coded form of the unit.
229   */
230  @Child(name = "system", type = { UriType.class }, order = 3, min = 0, max = 1, modifier = false, summary = true)
231  @Description(shortDefinition = "System that defines coded unit form", formalDefinition = "The identification of the system that provides the coded form of the unit.")
232  protected UriType system;
233
234  /**
235   * A computer processable form of the unit in some unit representation system.
236   */
237  @Child(name = "code", type = { CodeType.class }, order = 4, min = 0, max = 1, modifier = false, summary = true)
238  @Description(shortDefinition = "Coded form of the unit", formalDefinition = "A computer processable form of the unit in some unit representation system.")
239  protected CodeType code;
240
241  private static final long serialVersionUID = 1069574054L;
242
243  /*
244   * Constructor
245   */
246  public Quantity() {
247    super();
248  }
249
250  /**
251   * @return {@link #value} (The value of the measured amount. The value includes
252   *         an implicit precision in the presentation of the value.). This is the
253   *         underlying object with id, value and extensions. The accessor
254   *         "getValue" gives direct access to the value
255   */
256  public DecimalType getValueElement() {
257    if (this.value == null)
258      if (Configuration.errorOnAutoCreate())
259        throw new Error("Attempt to auto-create Quantity.value");
260      else if (Configuration.doAutoCreate())
261        this.value = new DecimalType(); // bb
262    return this.value;
263  }
264
265  public boolean hasValueElement() {
266    return this.value != null && !this.value.isEmpty();
267  }
268
269  public boolean hasValue() {
270    return this.value != null && !this.value.isEmpty();
271  }
272
273  /**
274   * @param value {@link #value} (The value of the measured amount. The value
275   *              includes an implicit precision in the presentation of the
276   *              value.). This is the underlying object with id, value and
277   *              extensions. The accessor "getValue" gives direct access to the
278   *              value
279   */
280  public Quantity setValueElement(DecimalType value) {
281    this.value = value;
282    return this;
283  }
284
285  /**
286   * @return The value of the measured amount. The value includes an implicit
287   *         precision in the presentation of the value.
288   */
289  public BigDecimal getValue() {
290    return this.value == null ? null : this.value.getValue();
291  }
292
293  /**
294   * @param value The value of the measured amount. The value includes an implicit
295   *              precision in the presentation of the value.
296   */
297  public Quantity setValue(BigDecimal value) {
298    if (value == null)
299      this.value = null;
300    else {
301      if (this.value == null)
302        this.value = new DecimalType();
303      this.value.setValue(value);
304    }
305    return this;
306  }
307
308  /**
309   * @return {@link #comparator} (How the value should be understood and
310   *         represented - whether the actual value is greater or less than the
311   *         stated value due to measurement issues; e.g. if the comparator is "<"
312   *         , then the real value is < stated value.). This is the underlying
313   *         object with id, value and extensions. The accessor "getComparator"
314   *         gives direct access to the value
315   */
316  public Enumeration<QuantityComparator> getComparatorElement() {
317    if (this.comparator == null)
318      if (Configuration.errorOnAutoCreate())
319        throw new Error("Attempt to auto-create Quantity.comparator");
320      else if (Configuration.doAutoCreate())
321        this.comparator = new Enumeration<QuantityComparator>(new QuantityComparatorEnumFactory()); // bb
322    return this.comparator;
323  }
324
325  public boolean hasComparatorElement() {
326    return this.comparator != null && !this.comparator.isEmpty();
327  }
328
329  public boolean hasComparator() {
330    return this.comparator != null && !this.comparator.isEmpty();
331  }
332
333  /**
334   * @param value {@link #comparator} (How the value should be understood and
335   *              represented - whether the actual value is greater or less than
336   *              the stated value due to measurement issues; e.g. if the
337   *              comparator is "<" , then the real value is < stated value.).
338   *              This is the underlying object with id, value and extensions. The
339   *              accessor "getComparator" gives direct access to the value
340   */
341  public Quantity setComparatorElement(Enumeration<QuantityComparator> value) {
342    this.comparator = value;
343    return this;
344  }
345
346  /**
347   * @return How the value should be understood and represented - whether the
348   *         actual value is greater or less than the stated value due to
349   *         measurement issues; e.g. if the comparator is "<" , then the real
350   *         value is < stated value.
351   */
352  public QuantityComparator getComparator() {
353    return this.comparator == null ? null : this.comparator.getValue();
354  }
355
356  /**
357   * @param value How the value should be understood and represented - whether the
358   *              actual value is greater or less than the stated value due to
359   *              measurement issues; e.g. if the comparator is "<" , then the
360   *              real value is < stated value.
361   */
362  public Quantity setComparator(QuantityComparator value) {
363    if (value == null)
364      this.comparator = null;
365    else {
366      if (this.comparator == null)
367        this.comparator = new Enumeration<QuantityComparator>(new QuantityComparatorEnumFactory());
368      this.comparator.setValue(value);
369    }
370    return this;
371  }
372
373  /**
374   * @return {@link #unit} (A human-readable form of the unit.). This is the
375   *         underlying object with id, value and extensions. The accessor
376   *         "getUnit" gives direct access to the value
377   */
378  public StringType getUnitElement() {
379    if (this.unit == null)
380      if (Configuration.errorOnAutoCreate())
381        throw new Error("Attempt to auto-create Quantity.unit");
382      else if (Configuration.doAutoCreate())
383        this.unit = new StringType(); // bb
384    return this.unit;
385  }
386
387  public boolean hasUnitElement() {
388    return this.unit != null && !this.unit.isEmpty();
389  }
390
391  public boolean hasUnit() {
392    return this.unit != null && !this.unit.isEmpty();
393  }
394
395  /**
396   * @param value {@link #unit} (A human-readable form of the unit.). This is the
397   *              underlying object with id, value and extensions. The accessor
398   *              "getUnit" gives direct access to the value
399   */
400  public Quantity setUnitElement(StringType value) {
401    this.unit = value;
402    return this;
403  }
404
405  /**
406   * @return A human-readable form of the unit.
407   */
408  public String getUnit() {
409    return this.unit == null ? null : this.unit.getValue();
410  }
411
412  /**
413   * @param value A human-readable form of the unit.
414   */
415  public Quantity setUnit(String value) {
416    if (Utilities.noString(value))
417      this.unit = null;
418    else {
419      if (this.unit == null)
420        this.unit = new StringType();
421      this.unit.setValue(value);
422    }
423    return this;
424  }
425
426  /**
427   * @return {@link #system} (The identification of the system that provides the
428   *         coded form of the unit.). This is the underlying object with id,
429   *         value and extensions. The accessor "getSystem" gives direct access to
430   *         the value
431   */
432  public UriType getSystemElement() {
433    if (this.system == null)
434      if (Configuration.errorOnAutoCreate())
435        throw new Error("Attempt to auto-create Quantity.system");
436      else if (Configuration.doAutoCreate())
437        this.system = new UriType(); // bb
438    return this.system;
439  }
440
441  public boolean hasSystemElement() {
442    return this.system != null && !this.system.isEmpty();
443  }
444
445  public boolean hasSystem() {
446    return this.system != null && !this.system.isEmpty();
447  }
448
449  /**
450   * @param value {@link #system} (The identification of the system that provides
451   *              the coded form of the unit.). This is the underlying object with
452   *              id, value and extensions. The accessor "getSystem" gives direct
453   *              access to the value
454   */
455  public Quantity setSystemElement(UriType value) {
456    this.system = value;
457    return this;
458  }
459
460  /**
461   * @return The identification of the system that provides the coded form of the
462   *         unit.
463   */
464  public String getSystem() {
465    return this.system == null ? null : this.system.getValue();
466  }
467
468  /**
469   * @param value The identification of the system that provides the coded form of
470   *              the unit.
471   */
472  public Quantity setSystem(String value) {
473    if (Utilities.noString(value))
474      this.system = null;
475    else {
476      if (this.system == null)
477        this.system = new UriType();
478      this.system.setValue(value);
479    }
480    return this;
481  }
482
483  /**
484   * @return {@link #code} (A computer processable form of the unit in some unit
485   *         representation system.). This is the underlying object with id, value
486   *         and extensions. The accessor "getCode" gives direct access to the
487   *         value
488   */
489  public CodeType getCodeElement() {
490    if (this.code == null)
491      if (Configuration.errorOnAutoCreate())
492        throw new Error("Attempt to auto-create Quantity.code");
493      else if (Configuration.doAutoCreate())
494        this.code = new CodeType(); // bb
495    return this.code;
496  }
497
498  public boolean hasCodeElement() {
499    return this.code != null && !this.code.isEmpty();
500  }
501
502  public boolean hasCode() {
503    return this.code != null && !this.code.isEmpty();
504  }
505
506  /**
507   * @param value {@link #code} (A computer processable form of the unit in some
508   *              unit representation system.). This is the underlying object with
509   *              id, value and extensions. The accessor "getCode" gives direct
510   *              access to the value
511   */
512  public Quantity setCodeElement(CodeType value) {
513    this.code = value;
514    return this;
515  }
516
517  /**
518   * @return A computer processable form of the unit in some unit representation
519   *         system.
520   */
521  public String getCode() {
522    return this.code == null ? null : this.code.getValue();
523  }
524
525  /**
526   * @param value A computer processable form of the unit in some unit
527   *              representation system.
528   */
529  public Quantity setCode(String value) {
530    if (Utilities.noString(value))
531      this.code = null;
532    else {
533      if (this.code == null)
534        this.code = new CodeType();
535      this.code.setValue(value);
536    }
537    return this;
538  }
539
540  protected void listChildren(List<Property> childrenList) {
541    super.listChildren(childrenList);
542    childrenList.add(new Property("value", "decimal",
543        "The value of the measured amount. The value includes an implicit precision in the presentation of the value.",
544        0, java.lang.Integer.MAX_VALUE, value));
545    childrenList.add(new Property("comparator", "code",
546        "How the value should be understood and represented - whether the actual value is greater or less than the stated value due to measurement issues; e.g. if the comparator is \"<\" , then the real value is < stated value.",
547        0, java.lang.Integer.MAX_VALUE, comparator));
548    childrenList.add(
549        new Property("unit", "string", "A human-readable form of the unit.", 0, java.lang.Integer.MAX_VALUE, unit));
550    childrenList
551        .add(new Property("system", "uri", "The identification of the system that provides the coded form of the unit.",
552            0, java.lang.Integer.MAX_VALUE, system));
553    childrenList
554        .add(new Property("code", "code", "A computer processable form of the unit in some unit representation system.",
555            0, java.lang.Integer.MAX_VALUE, code));
556  }
557
558  @Override
559  public void setProperty(String name, Base value) throws FHIRException {
560    if (name.equals("value"))
561      this.value = castToDecimal(value); // DecimalType
562    else if (name.equals("comparator"))
563      this.comparator = new QuantityComparatorEnumFactory().fromType(value); // Enumeration<QuantityComparator>
564    else if (name.equals("unit"))
565      this.unit = castToString(value); // StringType
566    else if (name.equals("system"))
567      this.system = castToUri(value); // UriType
568    else if (name.equals("code"))
569      this.code = castToCode(value); // CodeType
570    else
571      super.setProperty(name, value);
572  }
573
574  @Override
575  public Base addChild(String name) throws FHIRException {
576    if (name.equals("value")) {
577      throw new FHIRException("Cannot call addChild on a singleton property Quantity.value");
578    } else if (name.equals("comparator")) {
579      throw new FHIRException("Cannot call addChild on a singleton property Quantity.comparator");
580    } else if (name.equals("unit")) {
581      throw new FHIRException("Cannot call addChild on a singleton property Quantity.unit");
582    } else if (name.equals("system")) {
583      throw new FHIRException("Cannot call addChild on a singleton property Quantity.system");
584    } else if (name.equals("code")) {
585      throw new FHIRException("Cannot call addChild on a singleton property Quantity.code");
586    } else
587      return super.addChild(name);
588  }
589
590  public String fhirType() {
591    return "Quantity";
592
593  }
594
595  public Quantity copy() {
596    Quantity dst = new Quantity();
597    copyValues(dst);
598    dst.value = value == null ? null : value.copy();
599    dst.comparator = comparator == null ? null : comparator.copy();
600    dst.unit = unit == null ? null : unit.copy();
601    dst.system = system == null ? null : system.copy();
602    dst.code = code == null ? null : code.copy();
603    return dst;
604  }
605
606  protected Quantity typedCopy() {
607    return copy();
608  }
609
610  @Override
611  public boolean equalsDeep(Base other) {
612    if (!super.equalsDeep(other))
613      return false;
614    if (!(other instanceof Quantity))
615      return false;
616    Quantity o = (Quantity) other;
617    return compareDeep(value, o.value, true) && compareDeep(comparator, o.comparator, true)
618        && compareDeep(unit, o.unit, true) && compareDeep(system, o.system, true) && compareDeep(code, o.code, true);
619  }
620
621  @Override
622  public boolean equalsShallow(Base other) {
623    if (!super.equalsShallow(other))
624      return false;
625    if (!(other instanceof Quantity))
626      return false;
627    Quantity o = (Quantity) other;
628    return compareValues(value, o.value, true) && compareValues(comparator, o.comparator, true)
629        && compareValues(unit, o.unit, true) && compareValues(system, o.system, true)
630        && compareValues(code, o.code, true);
631  }
632
633  public boolean isEmpty() {
634    return super.isEmpty() && (value == null || value.isEmpty()) && (comparator == null || comparator.isEmpty())
635        && (unit == null || unit.isEmpty()) && (system == null || system.isEmpty()) && (code == null || code.isEmpty());
636  }
637
638}