
001package ca.uhn.fhir.util; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 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 ca.uhn.fhir.i18n.Msg; 024import ca.uhn.fhir.model.api.ICompositeElement; 025import ca.uhn.fhir.model.api.IElement; 026import org.hl7.fhir.instance.model.api.IBase; 027 028import java.util.ArrayList; 029import java.util.List; 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 public static boolean isEmpty(List<? extends IBase> theElements) { 080 if (theElements == null) { 081 return true; 082 } 083 for (int i = 0; i < theElements.size(); i++) { 084 IBase next; 085 try { 086 next = theElements.get(i); 087 } catch (ClassCastException e) { 088 List<?> elements = theElements; 089 String s = "Found instance of " + elements.get(i).getClass() + " - Did you set a field value to the incorrect type? Expected " + IBase.class.getName(); 090 throw new ClassCastException(Msg.code(1748) + s); 091 } 092 if (next != null && !next.isEmpty()) { 093 return false; 094 } 095 } 096 return true; 097 } 098 099 /** 100 * Note that this method does not work on HL7.org structures 101 */ 102 public static <T extends IElement> List<T> allPopulatedChildElements(Class<T> theType, Object... theElements) { 103 ArrayList<T> retVal = new ArrayList<T>(); 104 for (Object next : theElements) { 105 if (next == null) { 106 continue; 107 }else if (next instanceof IElement) { 108 addElement(retVal, (IElement) next, theType); 109 } else if (next instanceof List) { 110 for (Object nextElement : ((List<?>)next)) { 111 if (!(nextElement instanceof IBase)) { 112 throw new IllegalArgumentException(Msg.code(1749) + "Found element of "+nextElement.getClass()); 113 } 114 addElement(retVal, (IElement) nextElement, theType); 115 } 116 } else { 117 throw new IllegalArgumentException(Msg.code(1750) + "Found element of "+next.getClass()); 118 } 119 120 } 121 return retVal; 122 } 123 124 //@SuppressWarnings("unchecked") 125 private static <T extends IElement> void addElement(ArrayList<T> retVal, IElement next, Class<T> theType) { 126 if (theType != null && theType.isAssignableFrom(next.getClass())) { 127 retVal.add(theType.cast(next)); 128 } 129 if (next instanceof ICompositeElement) { 130 ICompositeElement iCompositeElement = (ICompositeElement) next; 131 //TODO: Use of a deprecated method should be resolved. 132 retVal.addAll(iCompositeElement.getAllPopulatedChildElementsOfType(theType)); 133 } 134 } 135 136}