View Javadoc
1   package ca.uhn.fhir.model.api;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.Validate;
27  import org.apache.commons.lang3.builder.ToStringBuilder;
28  import org.apache.commons.lang3.builder.ToStringStyle;
29  import org.hl7.fhir.instance.model.api.IBaseDatatype;
30  import org.hl7.fhir.instance.model.api.IBaseExtension;
31  
32  import ca.uhn.fhir.model.api.annotation.Child;
33  import ca.uhn.fhir.model.api.annotation.DatatypeDef;
34  import ca.uhn.fhir.model.primitive.StringDt;
35  
36  @DatatypeDef(name = "Extension")
37  public class ExtensionDt extends BaseIdentifiableElement implements ICompositeDatatype, IBaseExtension<ExtensionDt, IDatatype> {
38  
39  	private static final long serialVersionUID = 6399491332783085935L;
40  
41  	private boolean myModifier;
42  	
43  	@Child(name="url", type=StringDt.class, order=0, min=1, max=1)	
44  	private StringDt myUrl;
45  
46  	@Child(name = "value", type = IDatatype.class, order = 1, min = 0, max = 1)
47  	private IBaseDatatype myValue;
48  	
49  	public ExtensionDt() {
50  	}
51  
52  	public ExtensionDt(boolean theIsModifier) {
53  		myModifier = theIsModifier;
54  	}
55  
56  	public ExtensionDt(boolean theIsModifier, String theUrl) {
57  		Validate.notEmpty(theUrl, "URL must be populated");
58  
59  		myModifier = theIsModifier;
60  		myUrl = new StringDt(theUrl);
61  	}
62  
63  	public ExtensionDt(boolean theIsModifier, String theUrl, IBaseDatatype theValue) {
64  		Validate.notEmpty(theUrl, "URL must be populated");
65  		Validate.notNull(theValue, "Value must not be null");
66  
67  		myModifier = theIsModifier;
68  		myUrl = new StringDt(theUrl);
69  		myValue=theValue;
70  	}
71  
72  	/**
73  	 * Returns the URL for this extension.
74  	 * <p>
75  	 * Note that before HAPI 0.9 this method returned a {@link StringDt} but as of
76  	 * HAPI 0.9 this method returns a plain string. This was changed because it does not make sense to use a StringDt here
77  	 * since the URL itself can not contain extensions and it was therefore misleading.
78  	 * </p>
79  	 */
80  	@Override
81  	public String getUrl() {
82  		return myUrl != null ? myUrl.getValue() : null;
83  	}
84  
85  	/**
86  	 * Retained for backward compatibility
87  	 *
88  	 * @see ExtensionDt#getUrl()
89  	 */
90  	public String getUrlAsString() {
91  		return getUrl();
92  	}
93  
94  	/**
95  	 * Returns the value of this extension, if one exists.
96  	 * <p>
97  	 * 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
98  	 * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions.
99  	 * </p>
100 	 */
101 	@Override
102 	public IBaseDatatype getValue() {
103 		return myValue;
104 	}
105 
106 	/**
107 	 * 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
108 	 * be a primitive.
109 	 * <p>
110 	 * 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
111 	 * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions.
112 	 * </p>
113 	 * 
114 	 * @throws ClassCastException
115 	 *             If the value of this extension is not a primitive datatype
116 	 */
117 	public IPrimitiveDatatype<?> getValueAsPrimitive() {
118 		if (!(getValue() instanceof IPrimitiveDatatype)) {
119 			throw new ClassCastException("Extension with URL["+myUrl+"] can not be cast to primitive type, type is: "+ getClass().getCanonicalName());
120 		}
121 		return (IPrimitiveDatatype<?>) getValue();
122 	}
123 	
124 	@Override
125 	public boolean isEmpty() {
126 		return super.isBaseEmpty() && (myValue == null || myValue.isEmpty());
127 	}
128 
129 	public boolean isModifier() {
130 		return myModifier;
131 	}
132 
133 	public void setModifier(boolean theModifier) {
134 		myModifier = theModifier;
135 	}
136 
137 	@Override
138 	public ExtensionDt setUrl(String theUrl) {
139 		myUrl = theUrl != null ? new StringDt(theUrl) : myUrl;
140 		return this;
141 	}
142 
143 	public ExtensionDt setUrl(StringDt theUrl) {
144 		myUrl = theUrl;
145 		return this;
146 	}
147 
148 	@Override
149 	public ExtensionDt setValue(IBaseDatatype theValue) {
150 		myValue = theValue;
151 		return this;
152 	}
153 
154 	@Override
155 	@Deprecated //override deprecated method
156 	public <T extends IElement> List<T> getAllPopulatedChildElementsOfType(Class<T> theType) {
157 		return new ArrayList<T>();
158 	}
159 
160 	@Override
161 	public List<ExtensionDt> getExtension() {
162 		return getAllUndeclaredExtensions();
163 	}
164 
165 	@Override
166 	public String toString() {
167 		ToStringBuilder retVal = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
168 		retVal.append("url", getUrl());
169 		retVal.append("value", getValue());
170 		return retVal.build();
171 	}
172 	
173 	
174 
175 }