View Javadoc
1   package ca.uhn.fhir.model.api;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 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  
28  import ca.uhn.fhir.model.api.annotation.Child;
29  import ca.uhn.fhir.model.api.annotation.DatatypeDef;
30  import ca.uhn.fhir.model.primitive.StringDt;
31  
32  @DatatypeDef(name="Extension") 
33  public class ExtensionDt extends BaseIdentifiableElement implements ICompositeDatatype {
34  
35  	private boolean myModifier;
36  	
37  	@Child(name="url", type=StringDt.class, order=0, min=1, max=1)	
38  	private StringDt myUrl;
39  
40  	@Child(name="value", type=IDatatype.class, order=1, min=0, max=1)	
41  	private IElement myValue;
42  	
43  	public ExtensionDt() {
44  	}
45  
46  	public ExtensionDt(boolean theIsModifier) {
47  		myModifier = theIsModifier;
48  	}
49  
50  	public ExtensionDt(boolean theIsModifier, String theUrl) {
51  		Validate.notEmpty(theUrl, "URL must be populated");
52  
53  		myModifier = theIsModifier;
54  		myUrl = new StringDt(theUrl);
55  	}
56  
57  	public ExtensionDt(boolean theIsModifier, String theUrl, IDatatype theValue) {
58  		Validate.notEmpty(theUrl, "URL must be populated");
59  		Validate.notNull(theValue, "Value must not be null");
60  
61  		myModifier = theIsModifier;
62  		myUrl = new StringDt(theUrl);
63  		myValue=theValue;
64  	}
65  
66  	public StringDt getUrl() {
67  		if (myUrl==null) {
68  			myUrl=new StringDt();
69  		}
70  		return myUrl;
71  	}
72  
73  	public String getUrlAsString() {
74  		return getUrl().getValue();
75  	}
76  
77  	/**
78  	 * Returns the value of this extension, if one exists.
79  	 * <p>
80  	 * 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
81  	 * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions.
82  	 * </p>
83  	 */
84  	public IElement getValue() {
85  		return myValue;
86  	}
87  
88  	/**
89  	 * 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
90  	 * be a primitive.
91  	 * <p>
92  	 * 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
93  	 * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions.
94  	 * </p>
95  	 * 
96  	 * @throws ClassCastException
97  	 *             If the value of this extension is not a primitive datatype
98  	 */
99  	public IPrimitiveDatatype<?> getValueAsPrimitive() {
100 		if (!(getValue() instanceof IPrimitiveDatatype)) {
101 			throw new ClassCastException("Extension with URL["+myUrl+"] can not be cast to primitive type, type is: "+ getClass().getCanonicalName());
102 		}
103 		return (IPrimitiveDatatype<?>) getValue();
104 	}
105 	
106 	@Override
107 	public boolean isEmpty() {
108 		return super.isBaseEmpty() && (myValue == null || myValue.isEmpty());
109 	}
110 
111 	public boolean isModifier() {
112 		return myModifier;
113 	}
114 
115 	public void setModifier(boolean theModifier) {
116 		myModifier = theModifier;
117 	}
118 
119 	public ExtensionDt setUrl(String theUrl) {
120 		myUrl = new StringDt(theUrl);
121 		return this;
122 	}
123 
124 	public ExtensionDt setUrl(StringDt theUrl) {
125 		myUrl = theUrl;
126 		return this;
127 	}
128 
129 	public void setValue(IElement theValue) {
130 		myValue = theValue;
131 	}
132 
133 	@Override
134 	public <T extends IElement> List<T> getAllPopulatedChildElementsOfType(Class<T> theType) {
135 		return new ArrayList<T>();
136 	}
137 
138 }