001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.util;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.model.api.ICompositeElement;
024import ca.uhn.fhir.model.api.IElement;
025import org.hl7.fhir.instance.model.api.IBase;
026
027import java.util.ArrayList;
028import java.util.List;
029
030public class ElementUtil {
031
032        @SuppressWarnings("unchecked")
033        public static boolean isEmpty(Object... theElements) {
034                if (theElements == null) {
035                        return true;
036                }
037                for (int i = 0; i < theElements.length; i++) {
038                        Object next = theElements[i];
039                        if (next instanceof List) {
040                                if (!isEmpty((List<? extends IBase>) next)) {
041                                        return false;
042                                }
043                        } else if (next instanceof String && (!((String)next).isEmpty())) {
044                                return false;
045                        } else if (next != null && !((IBase) next).isEmpty()) {
046                                return false;
047                        }
048                }
049                return true;
050        }
051
052        public static boolean isEmpty(IBase... theElements) {
053                if (theElements == null) {
054                        return true;
055                }
056                for (int i = 0; i < theElements.length; i++) {
057                        IBase next = theElements[i];
058                        if (next != null && !next.isEmpty()) {
059                                return false;
060                        }
061                }
062                return true;
063        }
064
065        public static boolean isEmpty(IElement... theElements) {
066                if (theElements == null) {
067                        return true;
068                }
069                for (int i = 0; i < theElements.length; i++) {
070                        IBase next = theElements[i];
071                        if (next != null && !next.isEmpty()) {
072                                return false;
073                        }
074                }
075                return true;
076        }
077
078        public static boolean isEmpty(List<? extends IBase> theElements) {
079                if (theElements == null) {
080                        return true;
081                }
082                for (int i = 0; i < theElements.size(); i++) {
083                        IBase next;
084                        try {
085                                next = theElements.get(i);
086                        } catch (ClassCastException e) {
087                                List<?> elements = theElements;
088                                String s = "Found instance of " + elements.get(i).getClass() + " - Did you set a field value to the incorrect type? Expected " + IBase.class.getName();
089                                throw new ClassCastException(Msg.code(1748) + s);
090                        }
091                        if (next != null && !next.isEmpty()) {
092                                return false;
093                        }
094                }
095                return true;
096        }
097
098        /**
099         * Note that this method does not work on HL7.org structures
100         */
101        public static <T extends IElement> List<T> allPopulatedChildElements(Class<T> theType, Object... theElements) {
102                ArrayList<T> retVal = new ArrayList<T>();
103                for (Object next : theElements) {
104                        if (next == null) {
105                                continue;
106                        }else if (next instanceof IElement) {
107                                addElement(retVal, (IElement) next, theType);
108                        } else if (next instanceof List) {
109                                for (Object nextElement : ((List<?>)next)) {
110                                        if (!(nextElement instanceof IBase)) {
111                                                throw new IllegalArgumentException(Msg.code(1749) + "Found element of "+nextElement.getClass());
112                                        }
113                                        addElement(retVal, (IElement) nextElement, theType);
114                                }
115                        } else {
116                                throw new IllegalArgumentException(Msg.code(1750) + "Found element of "+next.getClass());
117                        }
118                        
119                }
120                return retVal;
121        }
122
123        //@SuppressWarnings("unchecked")
124        private static <T extends IElement> void addElement(ArrayList<T> retVal, IElement next, Class<T> theType) {
125                if (theType != null && theType.isAssignableFrom(next.getClass())) {
126                        retVal.add(theType.cast(next));
127                }
128                if (next instanceof ICompositeElement) {
129                        ICompositeElement iCompositeElement = (ICompositeElement) next;
130                        //TODO: Use of a deprecated method should be resolved.
131                        retVal.addAll(iCompositeElement.getAllPopulatedChildElementsOfType(theType));
132                }
133        }
134        
135}