001package ca.uhn.fhir.model.api;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 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
030public abstract class BaseElement implements IElement, ISupportsUndeclaredExtensions {
031
032        private List<ExtensionDt> myUndeclaredExtensions;
033        private List<ExtensionDt> myUndeclaredModifierExtensions;
034
035        @Override
036        public ExtensionDt addUndeclaredExtension(boolean theIsModifier, String theUrl, IBaseDatatype theValue) {
037                Validate.notEmpty(theUrl, "URL must be populated");
038                Validate.notNull(theValue, "Value must not be null");
039                ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl, theValue);
040                if (theIsModifier) {
041                        getUndeclaredModifierExtensions();
042                        myUndeclaredModifierExtensions.add(retVal);
043                } else {
044                        getUndeclaredExtensions();
045                        myUndeclaredExtensions.add(retVal);
046                }
047                return retVal;
048        }
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 void addUndeclaredExtension(ExtensionDt theExtension) {
067                Validate.notNull(theExtension, "Extension can not be null");
068                if (theExtension.isModifier()) {
069                        getUndeclaredModifierExtensions();
070                        myUndeclaredModifierExtensions.add(theExtension);
071                } else {
072                        getUndeclaredExtensions();
073                        myUndeclaredExtensions.add(theExtension);
074                }
075        }
076
077        @Override
078        public List<ExtensionDt> getAllUndeclaredExtensions() {
079                ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>();
080                if (myUndeclaredExtensions != null) {
081                        retVal.addAll(myUndeclaredExtensions);
082                }
083                if (myUndeclaredModifierExtensions != null) {
084                        retVal.addAll(myUndeclaredModifierExtensions);
085                }
086                return Collections.unmodifiableList(retVal);
087        }
088
089        @Override
090        public List<ExtensionDt> getUndeclaredExtensions() {
091                if (myUndeclaredExtensions == null) {
092                        myUndeclaredExtensions = new ArrayList<ExtensionDt>();
093                }
094                return (myUndeclaredExtensions);
095        }
096
097        @Override
098        public List<ExtensionDt> getUndeclaredExtensionsByUrl(String theUrl) {
099                org.apache.commons.lang3.Validate.notNull(theUrl, "URL can not be null");
100                ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>();
101                for (ExtensionDt next : getAllUndeclaredExtensions()) {
102                        if (theUrl.equals(next.getUrlAsString())) {
103                                retVal.add(next);
104                        }
105                }
106                return Collections.unmodifiableList(retVal);
107        }
108
109        @Override
110        public List<ExtensionDt> getUndeclaredModifierExtensions() {
111                if (myUndeclaredModifierExtensions == null) {
112                        myUndeclaredModifierExtensions = new ArrayList<ExtensionDt>();
113                }
114                return (myUndeclaredModifierExtensions);
115        }
116
117        /**
118         * Intended to be called by extending classes {@link #isEmpty()} implementations, returns <code>true</code> if all content in this superclass instance is empty per the semantics of
119         * {@link #isEmpty()}.
120         */
121        protected boolean isBaseEmpty() {
122                if (myUndeclaredExtensions != null) {
123                        for (ExtensionDt next : myUndeclaredExtensions) {
124                                if (!next.isEmpty()) {
125                                        return false;
126                                }
127                        }
128                }
129                if (myUndeclaredModifierExtensions != null) {
130                        for (ExtensionDt next : myUndeclaredModifierExtensions) {
131                                if (!next.isEmpty()) {
132                                        return false;
133                                }
134                        }
135                }
136                return true;
137        }
138
139}