001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.model.api;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.model.api.annotation.Child;
024import ca.uhn.fhir.model.api.annotation.DatatypeDef;
025import ca.uhn.fhir.model.primitive.StringDt;
026import org.apache.commons.lang3.Validate;
027import org.apache.commons.lang3.builder.ToStringBuilder;
028import org.apache.commons.lang3.builder.ToStringStyle;
029import org.hl7.fhir.instance.model.api.IBaseDatatype;
030import org.hl7.fhir.instance.model.api.IBaseExtension;
031
032import java.util.ArrayList;
033import java.util.List;
034
035@DatatypeDef(name = "Extension")
036public class ExtensionDt extends BaseIdentifiableElement implements ICompositeDatatype, IBaseExtension<ExtensionDt, IDatatype> {
037
038        private static final long serialVersionUID = 6399491332783085935L;
039
040        private boolean myModifier;
041        
042        @Child(name="url", type=StringDt.class, order=0, min=1, max=1)  
043        private StringDt myUrl;
044
045        @Child(name = "value", type = IDatatype.class, order = 1, min = 0, max = 1)
046        private IBaseDatatype myValue;
047        
048        public ExtensionDt() {
049        }
050
051        public ExtensionDt(boolean theIsModifier) {
052                myModifier = theIsModifier;
053        }
054
055        public ExtensionDt(boolean theIsModifier, String theUrl) {
056                Validate.notEmpty(theUrl, "URL must be populated");
057
058                myModifier = theIsModifier;
059                myUrl = new StringDt(theUrl);
060        }
061
062        public ExtensionDt(boolean theIsModifier, String theUrl, IBaseDatatype theValue) {
063                Validate.notEmpty(theUrl, "URL must be populated");
064                Validate.notNull(theValue, "Value must not be null");
065
066                myModifier = theIsModifier;
067                myUrl = new StringDt(theUrl);
068                myValue=theValue;
069        }
070
071        /**
072         * Returns the URL for this extension.
073         * <p>
074         * Note that before HAPI 0.9 this method returned a {@link StringDt} but as of
075         * HAPI 0.9 this method returns a plain string. This was changed because it does not make sense to use a StringDt here
076         * since the URL itself can not contain extensions and it was therefore misleading.
077         * </p>
078         */
079        @Override
080        public String getUrl() {
081                return myUrl != null ? myUrl.getValue() : null;
082        }
083
084        /**
085         * Retained for backward compatibility
086         *
087         * @see ExtensionDt#getUrl()
088         */
089        public String getUrlAsString() {
090                return getUrl();
091        }
092
093        /**
094         * Returns the value of this extension, if one exists.
095         * <p>
096         * 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
097         * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions.
098         * </p>
099         */
100        @Override
101        public IBaseDatatype getValue() {
102                return myValue;
103        }
104
105        /**
106         * 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
107         * be a primitive.
108         * <p>
109         * 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
110         * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions.
111         * </p>
112         * 
113         * @throws ClassCastException
114         *             If the value of this extension is not a primitive datatype
115         */
116        public IPrimitiveDatatype<?> getValueAsPrimitive() {
117                if (!(getValue() instanceof IPrimitiveDatatype)) {
118                        throw new ClassCastException(Msg.code(1887) + "Extension with URL["+myUrl+"] can not be cast to primitive type, type is: "+ getClass().getCanonicalName());
119                }
120                return (IPrimitiveDatatype<?>) getValue();
121        }
122        
123        @Override
124        public boolean isEmpty() {
125                return super.isBaseEmpty() && (myValue == null || myValue.isEmpty());
126        }
127
128        public boolean isModifier() {
129                return myModifier;
130        }
131
132        public void setModifier(boolean theModifier) {
133                myModifier = theModifier;
134        }
135
136        @Override
137        public ExtensionDt setUrl(String theUrl) {
138                myUrl = theUrl != null ? new StringDt(theUrl) : myUrl;
139                return this;
140        }
141
142        public ExtensionDt setUrl(StringDt theUrl) {
143                myUrl = theUrl;
144                return this;
145        }
146
147        @Override
148        public ExtensionDt setValue(IBaseDatatype theValue) {
149                myValue = theValue;
150                return this;
151        }
152
153        @Override
154        @Deprecated //override deprecated method
155        public <T extends IElement> List<T> getAllPopulatedChildElementsOfType(Class<T> theType) {
156                return new ArrayList<T>();
157        }
158
159        @Override
160        public List<ExtensionDt> getExtension() {
161                return getAllUndeclaredExtensions();
162        }
163
164        @Override
165        public String toString() {
166                ToStringBuilder retVal = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
167                retVal.append("url", getUrl());
168                retVal.append("value", getValue());
169                return retVal.build();
170        }
171        
172        
173
174}