001package ca.uhn.fhir.model.api;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
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
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026
027import org.apache.commons.lang3.Validate;
028import org.hl7.fhir.instance.model.api.IBaseDatatype;
029
030import ca.uhn.fhir.model.api.annotation.Child;
031import ca.uhn.fhir.model.api.annotation.Description;
032
033public abstract class BaseElement implements /*IElement, */ISupportsUndeclaredExtensions {
034
035        private static final long serialVersionUID = -3092659584634499332L;
036        private List<String> myFormatCommentsPost;
037        private List<String> myFormatCommentsPre;
038        
039   @Child(name = "extension", type = {ExtensionDt.class}, order=0, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=false)
040   @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 resource. 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." )
041        private List<ExtensionDt> myUndeclaredExtensions;
042        
043   /**
044    * May be used to represent additional information that is not part of the basic definition of the resource, and that modifies the understanding of the element that contains it. Usually modifier elements provide negation or qualification. 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. Applications processing a resource are required to check for modifier extensions.
045    */
046   @Child(name = "modifierExtension", type = {ExtensionDt.class}, order=1, min=0, max=Child.MAX_UNLIMITED, modifier=true, summary=false)
047   @Description(shortDefinition="Extensions that cannot be ignored", formalDefinition="May be used to represent additional information that is not part of the basic definition of the resource, and that modifies the understanding of the element that contains it. Usually modifier elements provide negation or qualification. 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. Applications processing a resource are required to check for modifier extensions." )
048        private List<ExtensionDt> myUndeclaredModifierExtensions;
049
050        @Override
051        public ExtensionDt addUndeclaredExtension(boolean theIsModifier, String theUrl) {
052                Validate.notEmpty(theUrl, "URL must be populated");
053
054                ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl);
055                if (theIsModifier) {
056                        getUndeclaredModifierExtensions();
057                        myUndeclaredModifierExtensions.add(retVal);
058                } else {
059                        getUndeclaredExtensions();
060                        myUndeclaredExtensions.add(retVal);
061                }
062                return retVal;
063        }
064
065        @Override
066        public ExtensionDt addUndeclaredExtension(boolean theIsModifier, String theUrl, IBaseDatatype theValue) {
067                Validate.notEmpty(theUrl, "URL must be populated");
068                Validate.notNull(theValue, "Value must not be null");
069                ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl, theValue);
070                if (theIsModifier) {
071                        getUndeclaredModifierExtensions();
072                        myUndeclaredModifierExtensions.add(retVal);
073                } else {
074                        getUndeclaredExtensions();
075                        myUndeclaredExtensions.add(retVal);
076                }
077                return retVal;
078        }
079
080        @Override
081        public void addUndeclaredExtension(ExtensionDt theExtension) {
082                Validate.notNull(theExtension, "Extension can not be null");
083                if (theExtension.isModifier()) {
084                        getUndeclaredModifierExtensions();
085                        myUndeclaredModifierExtensions.add(theExtension);
086                } else {
087                        getUndeclaredExtensions();
088                        myUndeclaredExtensions.add(theExtension);
089                }
090        }
091
092        @Override
093        public List<ExtensionDt> getAllUndeclaredExtensions() {
094                ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>();
095                if (myUndeclaredExtensions != null) {
096                        retVal.addAll(myUndeclaredExtensions);
097                }
098                if (myUndeclaredModifierExtensions != null) {
099                        retVal.addAll(myUndeclaredModifierExtensions);
100                }
101                return Collections.unmodifiableList(retVal);
102        }
103
104        @Override
105        public List<String> getFormatCommentsPost() {
106                if (myFormatCommentsPost == null)
107                        myFormatCommentsPost = new ArrayList<String>();
108                return myFormatCommentsPost;
109        }
110
111        @Override
112        public List<String> getFormatCommentsPre() {
113                if (myFormatCommentsPre == null)
114                        myFormatCommentsPre = new ArrayList<String>();
115                return myFormatCommentsPre;
116        }
117
118        @Override
119        public List<ExtensionDt> getUndeclaredExtensions() {
120                if (myUndeclaredExtensions == null) {
121                        myUndeclaredExtensions = new ArrayList<ExtensionDt>();
122                }
123                return (myUndeclaredExtensions);
124        }
125
126        @Override
127        public List<ExtensionDt> getUndeclaredExtensionsByUrl(String theUrl) {
128                org.apache.commons.lang3.Validate.notNull(theUrl, "URL can not be null");
129                ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>();
130                for (ExtensionDt next : getAllUndeclaredExtensions()) {
131                        if (theUrl.equals(next.getUrlAsString())) {
132                                retVal.add(next);
133                        }
134                }
135                return Collections.unmodifiableList(retVal);
136        }
137
138        @Override
139        public List<ExtensionDt> getUndeclaredModifierExtensions() {
140                if (myUndeclaredModifierExtensions == null) {
141                        myUndeclaredModifierExtensions = new ArrayList<ExtensionDt>();
142                }
143                return (myUndeclaredModifierExtensions);
144        }
145
146        @Override
147        public boolean hasFormatComment() {
148                return (myFormatCommentsPre != null && !myFormatCommentsPre.isEmpty()) || (myFormatCommentsPost != null && !myFormatCommentsPost.isEmpty());
149        }
150
151        /**
152         * Intended to be called by extending classes {@link #isEmpty()} implementations, returns <code>true</code> if all
153         * content in this superclass instance is empty per the semantics of {@link #isEmpty()}.
154         */
155        protected boolean isBaseEmpty() {
156                if (myUndeclaredExtensions != null) {
157                        for (ExtensionDt next : myUndeclaredExtensions) {
158                                if (next == null) {
159                                        continue;
160                                }
161                                if (!next.isEmpty()) {
162                                        return false;
163                                }
164                        }
165                }
166                if (myUndeclaredModifierExtensions != null) {
167                        for (ExtensionDt next : myUndeclaredModifierExtensions) {
168                                if (next == null) {
169                                        continue;
170                                }
171                                if (!next.isEmpty()) {
172                                        return false;
173                                }
174                        }
175                }
176                return true;
177        }
178
179}