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.List;
025
026import org.apache.commons.lang3.Validate;
027import org.hl7.fhir.instance.model.api.IBaseDatatype;
028import org.hl7.fhir.instance.model.api.IBaseExtension;
029
030import ca.uhn.fhir.model.api.annotation.Child;
031import ca.uhn.fhir.model.api.annotation.DatatypeDef;
032import ca.uhn.fhir.model.primitive.StringDt;
033
034@DatatypeDef(name = "Extension")
035public class ExtensionDt extends BaseIdentifiableElement implements ICompositeDatatype, IBaseExtension<ExtensionDt, IDatatype> {
036
037        private boolean myModifier;
038        
039        @Child(name="url", type=StringDt.class, order=0, min=1, max=1)  
040        private StringDt myUrl;
041
042        @Child(name = "value", type = IDatatype.class, order = 1, min = 0, max = 1)
043        private IBaseDatatype myValue;
044        
045        public ExtensionDt() {
046        }
047
048        public ExtensionDt(boolean theIsModifier) {
049                myModifier = theIsModifier;
050        }
051
052        public ExtensionDt(boolean theIsModifier, String theUrl) {
053                Validate.notEmpty(theUrl, "URL must be populated");
054
055                myModifier = theIsModifier;
056                myUrl = new StringDt(theUrl);
057        }
058
059        public ExtensionDt(boolean theIsModifier, String theUrl, IBaseDatatype theValue) {
060                Validate.notEmpty(theUrl, "URL must be populated");
061                Validate.notNull(theValue, "Value must not be null");
062
063                myModifier = theIsModifier;
064                myUrl = new StringDt(theUrl);
065                myValue=theValue;
066        }
067
068        /**
069         * Returns the URL for this extension.
070         * <p>
071         * Note that before HAPI 0.9 this method returned a {@link StringDt} but as of
072         * HAPI 0.9 this method returns a plain string. This was changed because it does not make sense to use a StringDt here
073         * since the URL itself can not contain extensions and it was therefore misleading.
074         * </p>
075         */
076        public String getUrl() {
077                return myUrl != null ? myUrl.getValue() : null;
078        }
079
080        /**
081         * Retained for backward compatibility
082         *
083         * @see ExtensionDt#getUrl()
084         */
085        public String getUrlAsString() {
086                return getUrl();
087        }
088
089        /**
090         * Returns the value of this extension, if one exists.
091         * <p>
092         * Note that if this extension contains extensions (instead of a datatype) then <b>this method will return null</b>. In that case, you must use {@link #getUndeclaredExtensions()} and
093         * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions.
094         * </p>
095         */
096        public IBaseDatatype getValue() {
097                return myValue;
098        }
099
100        /**
101         * Returns the value of this extension, casted to a primitive datatype. This is a convenience method which should only be called if you are sure that the value for this particular extension will
102         * be a primitive.
103         * <p>
104         * Note that if this extension contains extensions (instead of a datatype) then <b>this method will return null</b>. In that case, you must use {@link #getUndeclaredExtensions()} and
105         * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions.
106         * </p>
107         * 
108         * @throws ClassCastException
109         *             If the value of this extension is not a primitive datatype
110         */
111        public IPrimitiveDatatype<?> getValueAsPrimitive() {
112                if (!(getValue() instanceof IPrimitiveDatatype)) {
113                        throw new ClassCastException("Extension with URL["+myUrl+"] can not be cast to primitive type, type is: "+ getClass().getCanonicalName());
114                }
115                return (IPrimitiveDatatype<?>) getValue();
116        }
117        
118        @Override
119        public boolean isEmpty() {
120                return super.isBaseEmpty() && (myValue == null || myValue.isEmpty());
121        }
122
123        public boolean isModifier() {
124                return myModifier;
125        }
126
127        public void setModifier(boolean theModifier) {
128                myModifier = theModifier;
129        }
130
131        public ExtensionDt setUrl(String theUrl) {
132                myUrl = theUrl != null ? new StringDt(theUrl) : myUrl;
133                return this;
134        }
135
136        public ExtensionDt setUrl(StringDt theUrl) {
137                myUrl = theUrl;
138                return this;
139        }
140
141        public ExtensionDt setValue(IBaseDatatype theValue) {
142                myValue = theValue;
143                return this;
144        }
145
146        @Override
147        public <T extends IElement> List<T> getAllPopulatedChildElementsOfType(Class<T> theType) {
148                return new ArrayList<T>();
149        }
150
151        @Override
152        public List<ExtensionDt> getExtension() {
153                return getAllUndeclaredExtensions();
154        }
155
156}