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.Collections;
25  import java.util.List;
26  
27  import org.apache.commons.lang3.Validate;
28  
29  public abstract class BaseElement implements IElement, ISupportsUndeclaredExtensions {
30  
31  	private List<ExtensionDt> myUndeclaredExtensions;
32  	private List<ExtensionDt> myUndeclaredModifierExtensions;
33  
34  	@Override
35  	public ExtensionDt addUndeclaredExtension(boolean theIsModifier, String theUrl, IDatatype theValue) {
36  		Validate.notEmpty(theUrl, "URL must be populated");
37  		Validate.notNull(theValue, "Value must not be null");
38  		ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl, theValue);
39  		if (theIsModifier) {
40  			getUndeclaredModifierExtensions();
41  			myUndeclaredModifierExtensions.add(retVal);
42  		} else {
43  			getUndeclaredExtensions();
44  			myUndeclaredExtensions.add(retVal);
45  		}
46  		return retVal;
47  	}
48  
49  	@Override
50  	public ExtensionDt addUndeclaredExtension(boolean theIsModifier, String theUrl) {
51  		Validate.notEmpty(theUrl, "URL must be populated");
52  
53  		ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl);
54  		if (theIsModifier) {
55  			getUndeclaredModifierExtensions();
56  			myUndeclaredModifierExtensions.add(retVal);
57  		} else {
58  			getUndeclaredExtensions();
59  			myUndeclaredExtensions.add(retVal);
60  		}
61  		return retVal;
62  	}
63  
64  	@Override
65  	public void addUndeclaredExtension(ExtensionDt theExtension) {
66  		Validate.notNull(theExtension, "Extension can not be null");
67  		if (theExtension.isModifier()) {
68  			getUndeclaredModifierExtensions();
69  			myUndeclaredModifierExtensions.add(theExtension);
70  		} else {
71  			getUndeclaredExtensions();
72  			myUndeclaredExtensions.add(theExtension);
73  		}
74  	}
75  
76  	@Override
77  	public List<ExtensionDt> getAllUndeclaredExtensions() {
78  		ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>();
79  		if (myUndeclaredExtensions != null) {
80  			retVal.addAll(myUndeclaredExtensions);
81  		}
82  		if (myUndeclaredModifierExtensions != null) {
83  			retVal.addAll(myUndeclaredModifierExtensions);
84  		}
85  		return Collections.unmodifiableList(retVal);
86  	}
87  
88  	@Override
89  	public List<ExtensionDt> getUndeclaredExtensions() {
90  		if (myUndeclaredExtensions == null) {
91  			myUndeclaredExtensions = new ArrayList<ExtensionDt>();
92  		}
93  		return Collections.unmodifiableList(myUndeclaredExtensions);
94  	}
95  
96  	@Override
97  	public List<ExtensionDt> getUndeclaredExtensionsByUrl(String theUrl) {
98  		org.apache.commons.lang3.Validate.notNull(theUrl, "URL can not be null");
99  		ArrayList<ExtensionDt> retVal = new ArrayList<ExtensionDt>();
100 		for (ExtensionDt next : getAllUndeclaredExtensions()) {
101 			if (theUrl.equals(next.getUrlAsString())) {
102 				retVal.add(next);
103 			}
104 		}
105 		return Collections.unmodifiableList(retVal);
106 	}
107 
108 	@Override
109 	public List<ExtensionDt> getUndeclaredModifierExtensions() {
110 		if (myUndeclaredModifierExtensions == null) {
111 			myUndeclaredModifierExtensions = new ArrayList<ExtensionDt>();
112 		}
113 		return Collections.unmodifiableList(myUndeclaredModifierExtensions);
114 	}
115 
116 	/**
117 	 * Intended to be called by extending classes {@link #isEmpty()} implementations, returns <code>true</code> if all content in this superclass instance is empty per the semantics of
118 	 * {@link #isEmpty()}.
119 	 */
120 	protected boolean isBaseEmpty() {
121 		if (myUndeclaredExtensions != null) {
122 			for (ExtensionDt next : myUndeclaredExtensions) {
123 				if (!next.isEmpty()) {
124 					return false;
125 				}
126 			}
127 		}
128 		if (myUndeclaredModifierExtensions != null) {
129 			for (ExtensionDt next : myUndeclaredModifierExtensions) {
130 				if (!next.isEmpty()) {
131 					return false;
132 				}
133 			}
134 		}
135 		return true;
136 	}
137 
138 }