View Javadoc
1   package ca.uhn.fhir.util;
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.hl7.fhir.instance.model.api.IBase;
27  
28  import ca.uhn.fhir.model.api.ICompositeElement;
29  import ca.uhn.fhir.model.api.IElement;
30  
31  public class ElementUtil {
32  
33  	@SuppressWarnings("unchecked")
34  	public static boolean isEmpty(Object... theElements) {
35  		if (theElements == null) {
36  			return true;
37  		}
38  		for (int i = 0; i < theElements.length; i++) {
39  			Object next = theElements[i];
40  			if (next instanceof List) {
41  				if (!isEmpty((List<? extends IBase>) next)) {
42  					return false;
43  				}
44  			} else if (next instanceof String && (!((String)next).isEmpty())) {
45  				return false;
46  			} else if (next != null && !((IBase) next).isEmpty()) {
47  				return false;
48  			}
49  		}
50  		return true;
51  	}
52  
53  	public static boolean isEmpty(IBase... theElements) {
54  		if (theElements == null) {
55  			return true;
56  		}
57  		for (int i = 0; i < theElements.length; i++) {
58  			IBase next = theElements[i];
59  			if (next != null && !next.isEmpty()) {
60  				return false;
61  			}
62  		}
63  		return true;
64  	}
65  
66  	public static boolean isEmpty(IElement... theElements) {
67  		if (theElements == null) {
68  			return true;
69  		}
70  		for (int i = 0; i < theElements.length; i++) {
71  			IBase next = theElements[i];
72  			if (next != null && !next.isEmpty()) {
73  				return false;
74  			}
75  		}
76  		return true;
77  	}
78  
79  	/*
80  	public static <T> void validateAllElementsAreOfTypeOrThrowClassCastExceptionForModelSetter(List<T> theList, Class<T> theType) {
81  		if (theList == null) {
82  			return;
83  		}
84  		for (T next : theList) {
85  			if (next != null && theType.isAssignableFrom(next.getClass()) == false) {
86  				StringBuilder b = new StringBuilder();
87  				b.append("Failed to set invalid value, found element in list of type ");
88  				b.append(next.getClass().getSimpleName());
89  				b.append(" but expected ");
90  				b.append(theType.getName());
91  				throw new ClassCastException(b.toString());
92  			}
93  		}
94  	}
95  	*/
96  	
97  	public static boolean isEmpty(List<? extends IBase> theElements) {
98  		if (theElements == null) {
99  			return true;
100 		}
101 		for (int i = 0; i < theElements.size(); i++) {
102 			IBase next;
103 			try {
104 				next = theElements.get(i);
105 			} catch (ClassCastException e) {
106 				List<?> elements = theElements;
107 				String s = "Found instance of " + elements.get(i).getClass() + " - Did you set a field value to the incorrect type? Expected " + IBase.class.getName();
108 				throw new ClassCastException(s);
109 			}
110 			if (next != null && !next.isEmpty()) {
111 				return false;
112 			}
113 		}
114 		return true;
115 	}
116 
117 	/**
118 	 * Note that this method does not work on HL7.org structures
119 	 */
120 	public static <T extends IElement> List<T> allPopulatedChildElements(Class<T> theType, Object... theElements) {
121 		ArrayList<T> retVal = new ArrayList<T>();
122 		for (Object next : theElements) {
123 			if (next == null) {
124 				continue;
125 			}else if (next instanceof IElement) {
126 				addElement(retVal, (IElement) next, theType);
127 			} else if (next instanceof List) {
128 				for (Object nextElement : ((List<?>)next)) {
129 					if (!(nextElement instanceof IBase)) {
130 						throw new IllegalArgumentException("Found element of "+nextElement.getClass());
131 					}
132 					addElement(retVal, (IElement) nextElement, theType);
133 				}
134 			} else {
135 				throw new IllegalArgumentException("Found element of "+next.getClass());
136 			}
137 			
138 		}
139 		return retVal;
140 	}
141 
142 	//@SuppressWarnings("unchecked")
143 	private static <T extends IElement> void addElement(ArrayList<T> retVal, IElement next, Class<T> theType) {
144 		//FIXME There seems to be an error on theType == null => if (theType != null|| theType.isAssignableFrom
145 		if (theType == null|| theType.isAssignableFrom(next.getClass())) {
146 			retVal.add(theType.cast(next));
147 		}
148 		if (next instanceof ICompositeElement) {
149 			ICompositeElement iCompositeElement = (ICompositeElement) next;
150 			//TODO: Use of a deprecated method should be resolved.
151 			retVal.addAll(iCompositeElement.getAllPopulatedChildElementsOfType(theType));
152 		}
153 	}
154 	
155 }