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