001package ca.uhn.fhir.mdm.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Master Data Management
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 org.hl7.fhir.instance.model.api.IPrimitiveType;
024
025import java.lang.reflect.Field;
026import java.util.function.BiPredicate;
027
028public class PrimitiveTypeEqualsPredicate implements BiPredicate {
029
030        @Override
031        public boolean test(Object theBase1, Object theBase2) {
032                if (theBase1 == null) {
033                        return theBase2 == null;
034                }
035                if (theBase2 == null) {
036                        return false;
037                }
038                if (!theBase1.getClass().equals(theBase2.getClass())) {
039                        return false;
040                }
041
042                for (Field f : theBase1.getClass().getDeclaredFields()) {
043                        Class<?> fieldClass = f.getType();
044
045                        if (!IPrimitiveType.class.isAssignableFrom(fieldClass)) {
046                                continue;
047                        }
048
049                        IPrimitiveType<?> val1, val2;
050
051                        f.setAccessible(true);
052                        try {
053                                val1 = (IPrimitiveType<?>) f.get(theBase1);
054                                val2 = (IPrimitiveType<?>) f.get(theBase2);
055                        } catch (Exception e) {
056                                // swallow
057                                continue;
058                        }
059
060                        if (val1 == null && val2 == null) {
061                                continue;
062                        }
063
064                        if (val1 == null || val2 == null) {
065                                return false;
066                        }
067
068                        Object actualVal1 = val1.getValue();
069                        Object actualVal2 = val2.getValue();
070
071                        if (actualVal1 == null && actualVal2 == null) {
072                                continue;
073                        }
074                        if (actualVal1 == null) {
075                                return false;
076                        }
077                        if (!actualVal1.equals(actualVal2)) {
078                                return false;
079                        }
080                }
081
082                return true;
083        }
084}