001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 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        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 = theElements.get(i);
085                        if (next != null && !next.isEmpty()) {
086                                return false;
087                        }
088                }
089                return true;
090        }
091
092        /**
093         * Note that this method does not work on HL7.org structures
094         */
095        public static <T extends IElement> List<T> allPopulatedChildElements(Class<T> theType, Object... theElements) {
096                ArrayList<T> retVal = new ArrayList<T>();
097                for (Object next : theElements) {
098                        if (next == null) {
099                                continue;
100                        }else if (next instanceof IElement) {
101                                addElement(retVal, (IElement) next, theType);
102                        } else if (next instanceof List) {
103                                for (Object nextElement : ((List<?>)next)) {
104                                        if (!(nextElement instanceof IBase)) {
105                                                throw new IllegalArgumentException("Found element of "+nextElement.getClass());
106                                        }
107                                        addElement(retVal, (IElement) nextElement, theType);
108                                }
109                        } else {
110                                throw new IllegalArgumentException("Found element of "+next.getClass());
111                        }
112                        
113                }
114                return retVal;
115        }
116
117        @SuppressWarnings("unchecked")
118        private static <T extends IElement> void addElement(ArrayList<T> retVal, IElement next, Class<T> theType) {
119                if (theType == null|| theType.isAssignableFrom(next.getClass())) {
120                        retVal.add((T) next);
121                }
122                if (next instanceof ICompositeElement) {
123                        ICompositeElement iCompositeElement = (ICompositeElement) next;
124                        retVal.addAll(iCompositeElement.getAllPopulatedChildElementsOfType(theType));
125                }
126        }
127        
128}