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.Collections;
25  import java.util.List;
26  
27  import org.apache.commons.lang3.Validate;
28  import org.hl7.fhir.instance.model.api.IBaseDatatype;
29  
30  import ca.uhn.fhir.model.api.annotation.Child;
31  import ca.uhn.fhir.model.api.annotation.Description;
32  
33  public abstract class BaseElement implements /*IElement, */ISupportsUndeclaredExtensions {
34  
35  	private static final long serialVersionUID = -3092659584634499332L;
36  	private List<String> myFormatCommentsPost;
37  	private List<String> myFormatCommentsPre;
38  	
39     @Child(name = "extension", type = {ExtensionDt.class}, order=0, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=false)
40     @Description(shortDefinition="Additional Content defined by implementations", formalDefinition="May be used to represent additional information that is not part of the basic definition of the resource. In order to make the use of extensions safe and manageable, there is a strict set of governance  applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension." )
41  	private List<ExtensionDt> myUndeclaredExtensions;
42  	
43     /**
44      * May be used to represent additional information that is not part of the basic definition of the resource, and that modifies the understanding of the element that contains it. Usually modifier elements provide negation or qualification. In order to make the use of extensions safe and manageable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. Applications processing a resource are required to check for modifier extensions.
45      */
46     @Child(name = "modifierExtension", type = {ExtensionDt.class}, order=1, min=0, max=Child.MAX_UNLIMITED, modifier=true, summary=false)
47     @Description(shortDefinition="Extensions that cannot be ignored", formalDefinition="May be used to represent additional information that is not part of the basic definition of the resource, and that modifies the understanding of the element that contains it. Usually modifier elements provide negation or qualification. In order to make the use of extensions safe and manageable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. Applications processing a resource are required to check for modifier extensions." )
48  	private List<ExtensionDt> myUndeclaredModifierExtensions;
49  
50  	@Override
51  	public ExtensionDt addUndeclaredExtension(boolean theIsModifier, String theUrl) {
52  		Validate.notEmpty(theUrl, "URL must be populated");
53  
54  		ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl);
55  		if (theIsModifier) {
56  			getUndeclaredModifierExtensions();
57  			myUndeclaredModifierExtensions.add(retVal);
58  		} else {
59  			getUndeclaredExtensions();
60  			myUndeclaredExtensions.add(retVal);
61  		}
62  		return retVal;
63  	}
64  
65  	@Override
66  	public ExtensionDt addUndeclaredExtension(boolean theIsModifier, String theUrl, IBaseDatatype theValue) {
67  		Validate.notEmpty(theUrl, "URL must be populated");
68  		Validate.notNull(theValue, "Value must not be null");
69  		ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl, theValue);
70  		if (theIsModifier) {
71  			getUndeclaredModifierExtensions();
72  			myUndeclaredModifierExtensions.add(retVal);
73  		} else {
74  			getUndeclaredExtensions();
75  			myUndeclaredExtensions.add(retVal);
76  		}
77  		return retVal;
78  	}
79  
80  	@Override
81  	public void addUndeclaredExtension(ExtensionDt theExtension) {
82  		Validate.notNull(theExtension, "Extension can not be null");
83  		if (theExtension.isModifier()) {
84  			getUndeclaredModifierExtensions();
85  			myUndeclaredModifierExtensions.add(theExtension);
86  		} else {
87  			getUndeclaredExtensions();
88  			myUndeclaredExtensions.add(theExtension);
89  		}
90  	}
91  
92  	@Override
93  	public List<ExtensionDt> getAllUndeclaredExtensions() {
94  		ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>();
95  		if (myUndeclaredExtensions != null) {
96  			retVal.addAll(myUndeclaredExtensions);
97  		}
98  		if (myUndeclaredModifierExtensions != null) {
99  			retVal.addAll(myUndeclaredModifierExtensions);
100 		}
101 		return Collections.unmodifiableList(retVal);
102 	}
103 
104 	@Override
105 	public List<String> getFormatCommentsPost() {
106 		if (myFormatCommentsPost == null)
107 			myFormatCommentsPost = new ArrayList<String>();
108 		return myFormatCommentsPost;
109 	}
110 
111 	@Override
112 	public List<String> getFormatCommentsPre() {
113 		if (myFormatCommentsPre == null)
114 			myFormatCommentsPre = new ArrayList<String>();
115 		return myFormatCommentsPre;
116 	}
117 
118 	@Override
119 	public List<ExtensionDt> getUndeclaredExtensions() {
120 		if (myUndeclaredExtensions == null) {
121 			myUndeclaredExtensions = new ArrayList<ExtensionDt>();
122 		}
123 		return (myUndeclaredExtensions);
124 	}
125 
126 	@Override
127 	public List<ExtensionDt> getUndeclaredExtensionsByUrl(String theUrl) {
128 		org.apache.commons.lang3.Validate.notNull(theUrl, "URL can not be null");
129 		ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>();
130 		for (ExtensionDt next : getAllUndeclaredExtensions()) {
131 			if (theUrl.equals(next.getUrlAsString())) {
132 				retVal.add(next);
133 			}
134 		}
135 		return Collections.unmodifiableList(retVal);
136 	}
137 
138 	@Override
139 	public List<ExtensionDt> getUndeclaredModifierExtensions() {
140 		if (myUndeclaredModifierExtensions == null) {
141 			myUndeclaredModifierExtensions = new ArrayList<ExtensionDt>();
142 		}
143 		return (myUndeclaredModifierExtensions);
144 	}
145 
146 	@Override
147 	public boolean hasFormatComment() {
148 		return (myFormatCommentsPre != null && !myFormatCommentsPre.isEmpty()) || (myFormatCommentsPost != null && !myFormatCommentsPost.isEmpty());
149 	}
150 
151 	/**
152 	 * Intended to be called by extending classes {@link #isEmpty()} implementations, returns <code>true</code> if all
153 	 * content in this superclass instance is empty per the semantics of {@link #isEmpty()}.
154 	 */
155 	protected boolean isBaseEmpty() {
156 		if (myUndeclaredExtensions != null) {
157 			for (ExtensionDt next : myUndeclaredExtensions) {
158 				if (next == null) {
159 					continue;
160 				}
161 				if (!next.isEmpty()) {
162 					return false;
163 				}
164 			}
165 		}
166 		if (myUndeclaredModifierExtensions != null) {
167 			for (ExtensionDt next : myUndeclaredModifierExtensions) {
168 				if (next == null) {
169 					continue;
170 				}
171 				if (!next.isEmpty()) {
172 					return false;
173 				}
174 			}
175 		}
176 		return true;
177 	}
178 
179 }