001package ca.uhn.fhir.util; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2019 University Health Network 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import java.util.ArrayList; 024import java.util.List; 025 026import org.hl7.fhir.instance.model.api.IBase; 027 028import ca.uhn.fhir.model.api.ICompositeElement; 029import ca.uhn.fhir.model.api.IElement; 030 031public class ElementUtil { 032 033 @SuppressWarnings("unchecked") 034 public static boolean isEmpty(Object... theElements) { 035 if (theElements == null) { 036 return true; 037 } 038 for (int i = 0; i < theElements.length; i++) { 039 Object next = theElements[i]; 040 if (next instanceof List) { 041 if (!isEmpty((List<? extends IBase>) next)) { 042 return false; 043 } 044 } else if (next instanceof String && (!((String)next).isEmpty())) { 045 return false; 046 } else if (next != null && !((IBase) next).isEmpty()) { 047 return false; 048 } 049 } 050 return true; 051 } 052 053 public static boolean isEmpty(IBase... theElements) { 054 if (theElements == null) { 055 return true; 056 } 057 for (int i = 0; i < theElements.length; i++) { 058 IBase next = theElements[i]; 059 if (next != null && !next.isEmpty()) { 060 return false; 061 } 062 } 063 return true; 064 } 065 066 public static boolean isEmpty(IElement... theElements) { 067 if (theElements == null) { 068 return true; 069 } 070 for (int i = 0; i < theElements.length; i++) { 071 IBase next = theElements[i]; 072 if (next != null && !next.isEmpty()) { 073 return false; 074 } 075 } 076 return true; 077 } 078 079 /* 080 public static <T> void validateAllElementsAreOfTypeOrThrowClassCastExceptionForModelSetter(List<T> theList, Class<T> theType) { 081 if (theList == null) { 082 return; 083 } 084 for (T next : theList) { 085 if (next != null && theType.isAssignableFrom(next.getClass()) == false) { 086 StringBuilder b = new StringBuilder(); 087 b.append("Failed to set invalid value, found element in list of type "); 088 b.append(next.getClass().getSimpleName()); 089 b.append(" but expected "); 090 b.append(theType.getName()); 091 throw new ClassCastException(b.toString()); 092 } 093 } 094 } 095 */ 096 097 public static boolean isEmpty(List<? extends IBase> theElements) { 098 if (theElements == null) { 099 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}