001package org.hl7.fhir.r4.model;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r4
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
024import java.util.ArrayList;
025import java.util.List;
026
027/**
028 * A child element or property defined by the FHIR specification
029 * This class is defined as a helper class when iterating the 
030 * children of an element in a generic fashion
031 * 
032 * At present, iteration is only based on the specification, but 
033 * this may be changed to allow profile based expression at a
034 * later date
035 * 
036 * note: there's no point in creating one of these classes outside this package
037 */
038public class Property {
039
040        /**
041         * The name of the property as found in the FHIR specification
042         */
043        private String name;
044        
045        /**
046         * The type of the property as specified in the FHIR specification (e.g. type|type|Reference(Name|Name)
047         */
048        private String typeCode;
049        
050        /**
051         * The formal definition of the element given in the FHIR specification
052         */
053        private String definition;
054        
055        /**
056         * The minimum allowed cardinality - 0 or 1 when based on the specification
057         */
058        private int minCardinality;
059        
060        /** 
061         * The maximum allowed cardinality - 1 or MAX_INT when based on the specification
062         */
063        private int maxCardinality;
064        
065        /**
066         * The actual elements that exist on this instance
067         */
068        private List<Base> values = new ArrayList<Base>();
069
070        /**
071         * For run time, if/once a property is hooked up to it's definition
072         */
073        private StructureDefinition structure; 
074
075        /**
076         * Internal constructor
077         */
078        public Property(String name, String typeCode, String definition, int minCardinality, int maxCardinality, Base value) {
079          super();
080          this.name = name;
081          this.typeCode = typeCode;
082          this.definition = definition;
083          this.minCardinality = minCardinality;
084          this.maxCardinality = maxCardinality;
085          if (value != null)
086            this.values.add(value);
087  }
088
089        /**
090         * Internal constructor
091         */
092        public Property(String name, String typeCode, String definition, int minCardinality, int maxCardinality, List<? extends Base> values) {
093          super();
094          this.name = name;
095          this.typeCode = typeCode;
096          this.definition = definition;
097          this.minCardinality = minCardinality;
098          this.maxCardinality = maxCardinality;
099          if (values != null)
100            this.values.addAll(values);
101  }
102
103        /**
104         * @return The name of this property in the FHIR Specification
105         */
106        public String getName() {
107                return name;
108        }
109
110        /**
111         * @return The stated type in the FHIR specification
112         */
113        public String getTypeCode() {
114                return typeCode;
115        }
116
117        /** 
118         * @return The definition of this element in the FHIR spec
119         */
120        public String getDefinition() {
121                return definition;
122        }
123
124        /**
125         * @return the minimum cardinality for this element 
126         */
127        public int getMinCardinality() {
128                return minCardinality;
129        }
130
131        /**
132         * @return the maximum cardinality for this element 
133         */
134        public int getMaxCardinality() {
135                return maxCardinality;
136        }
137
138        /**
139         * @return the actual values - will only be 1 unless maximum cardinality == MAX_INT
140         */
141        public List<Base> getValues() {
142                return values;
143        }
144
145  public boolean hasValues() {
146    for (Base e : getValues())
147      if (e != null)
148        return true;
149    return false;
150  }
151
152  public StructureDefinition getStructure() {
153    return structure;
154  }
155
156  public void setStructure(StructureDefinition structure) {
157    this.structure = structure;
158  }
159
160  public boolean isList() {
161    return maxCardinality > 1;
162  }
163
164
165  public String toString() {
166    return name; 
167  }
168        
169}