001package org.hl7.fhir.dstu2.model;
002
003/*-
004 * #%L
005 * org.hl7.fhir.dstu2
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
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
023
024import java.util.List;
025
026import org.apache.commons.lang3.NotImplementedException;
027import org.hl7.fhir.exceptions.FHIRException;
028import org.hl7.fhir.utilities.Utilities;
029
030/**
031 * See http://www.healthintersections.com.au/?p=1941
032 * 
033 * @author Grahame
034 *
035 */
036public class Comparison {
037
038        public class MatchProfile {
039
040  }
041
042        public static boolean matches(String c1, String c2, MatchProfile profile)  {
043          if (Utilities.noString(c1) || Utilities.noString(c2))
044                return false;
045          c1 = Utilities.normalize(c1);
046          c2 = Utilities.normalize(c2);
047          return c1.equals(c2);
048        }
049
050        public static <T extends Enum<?>> boolean matches(Enumeration<T> e1, Enumeration<T> e2, MatchProfile profile)  {
051          if (e1 == null || e2 == null)
052                return false;
053          return e1.getValue().equals(e2.getValue());
054        }
055
056        public static boolean matches(CodeableConcept c1, CodeableConcept c2, MatchProfile profile) throws FHIRException {
057          if (profile != null) 
058                throw new NotImplementedException("Not Implemented Yet");
059          
060          if (c1.getCoding().isEmpty() && c2.getCoding().isEmpty()) {
061                return matches(c1.getText(), c2.getText(), null);
062          } else {
063                // in the absence of specific guidance, we just require that all codes match
064                boolean ok = true;
065                for (Coding c : c1.getCoding()) {
066                        ok = ok && inList(c2.getCoding(), c, null);
067                }
068                for (Coding c : c2.getCoding()) {
069                        ok = ok && inList(c1.getCoding(), c, null);
070                }
071                return ok;
072          }
073  }
074
075        public static void merge(CodeableConcept dst, CodeableConcept src) {
076                if (dst.getTextElement() == null && src.getTextElement() != null)
077                        dst.setTextElement(src.getTextElement());  
078  }
079
080        
081        public static boolean inList(List<Coding> list, Coding c, MatchProfile profile) {
082          for (Coding item : list) {
083                if (matches(item, c, profile))
084                        return true;
085          }
086          return false;
087  }
088
089        public static boolean matches(Coding c1, Coding c2, MatchProfile profile) {
090          if (profile != null) 
091                throw new NotImplementedException("Not Implemented Yet");
092                
093          // in the absence of a profile, we ignore version
094          return matches(c1.getSystem(), c2.getSystem(), null) && matches(c1.getCode(), c2.getCode(), null);
095  }
096
097        public static boolean matches(Identifier i1, Identifier i2, MatchProfile profile) {
098          if (profile != null) 
099                throw new NotImplementedException("Not Implemented Yet");
100                
101          // in the absence of a profile, we ignore version
102          return matches(i1.getSystem(), i2.getSystem(), null) && matches(i1.getValue(), i2.getValue(), null);
103  }
104
105        public static void merge(Identifier dst, Identifier src) {
106                if (dst.getUseElement() == null && src.getUseElement() != null)
107                        dst.setUseElement(src.getUseElement());  
108                if (dst.getType() == null && src.getType() != null)
109                        dst.setType(src.getType());  
110                if (dst.getPeriod() == null && src.getPeriod() != null)
111                        dst.setPeriod(src.getPeriod());  
112                if (dst.getAssigner() == null && src.getAssigner() != null)
113                        dst.setAssigner(src.getAssigner());  
114  }
115
116        public static boolean matches(ContactPoint c1, ContactPoint c2, Object profile) {
117          if (profile != null) 
118                throw new NotImplementedException("Not Implemented Yet");
119                
120          // in the absence of a profile, we insist on system
121          return matches(c1.getSystemElement(), c2.getSystemElement(), null) && matches(c1.getValue(), c2.getValue(), null);
122  }
123
124        public static void merge(ContactPoint dst, ContactPoint src) {
125                if (dst.getUseElement() == null && src.getUseElement() != null)
126                        dst.setUseElement(src.getUseElement());  
127                if (dst.getPeriod() == null && src.getPeriod() != null)
128                        dst.setPeriod(src.getPeriod());  
129  }
130
131}