001package org.hl7.fhir.r4.model;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032import java.util.List;
033
034import org.apache.commons.lang3.NotImplementedException;
035import org.hl7.fhir.exceptions.FHIRException;
036import org.hl7.fhir.utilities.Utilities;
037
038/**
039 * See http://www.healthintersections.com.au/?p=1941
040 * 
041 * @author Grahame
042 *
043 */
044public class Comparison {
045
046  public class MatchProfile {
047
048  }
049
050  public static boolean matches(String c1, String c2, MatchProfile profile) {
051    if (Utilities.noString(c1) || Utilities.noString(c2))
052      return false;
053    c1 = Utilities.normalize(c1);
054    c2 = Utilities.normalize(c2);
055    return c1.equals(c2);
056  }
057
058  public static <T extends Enum<?>> boolean matches(Enumeration<T> e1, Enumeration<T> e2, MatchProfile profile) {
059    if (e1 == null || e2 == null)
060      return false;
061    return e1.getValue().equals(e2.getValue());
062  }
063
064  public static boolean matches(CodeableConcept c1, CodeableConcept c2, MatchProfile profile) throws FHIRException {
065    if (profile != null)
066      throw new NotImplementedException("Not Implemented Yet");
067
068    if (c1.getCoding().isEmpty() && c2.getCoding().isEmpty()) {
069      return matches(c1.getText(), c2.getText(), null);
070    } else {
071      // in the absence of specific guidance, we just require that all codes match
072      boolean ok = true;
073      for (Coding c : c1.getCoding()) {
074        ok = ok && inList(c2.getCoding(), c, null);
075      }
076      for (Coding c : c2.getCoding()) {
077        ok = ok && inList(c1.getCoding(), c, null);
078      }
079      return ok;
080    }
081  }
082
083  public static void merge(CodeableConcept dst, CodeableConcept src) {
084    if (dst.getTextElement() == null && src.getTextElement() != null)
085      dst.setTextElement(src.getTextElement());
086  }
087
088  public static boolean inList(List<Coding> list, Coding c, MatchProfile profile) {
089    for (Coding item : list) {
090      if (matches(item, c, profile))
091        return true;
092    }
093    return false;
094  }
095
096  public static boolean matches(Coding c1, Coding c2, MatchProfile profile) {
097    if (profile != null)
098      throw new NotImplementedException("Not Implemented Yet");
099
100    // in the absence of a profile, we ignore version
101    return matches(c1.getSystem(), c2.getSystem(), null) && matches(c1.getCode(), c2.getCode(), null);
102  }
103
104  public static boolean matches(Identifier i1, Identifier i2, MatchProfile profile) {
105    if (profile != null)
106      throw new NotImplementedException("Not Implemented Yet");
107
108    // in the absence of a profile, we ignore version
109    return matches(i1.getSystem(), i2.getSystem(), null) && matches(i1.getValue(), i2.getValue(), null);
110  }
111
112  public static void merge(Identifier dst, Identifier src) {
113    if (dst.getUseElement() == null && src.getUseElement() != null)
114      dst.setUseElement(src.getUseElement());
115    if (dst.getType() == null && src.getType() != null)
116      dst.setType(src.getType());
117    if (dst.getPeriod() == null && src.getPeriod() != null)
118      dst.setPeriod(src.getPeriod());
119    if (dst.getAssigner() == null && src.getAssigner() != null)
120      dst.setAssigner(src.getAssigner());
121  }
122
123  public static boolean matches(ContactPoint c1, ContactPoint c2, Object profile) {
124    if (profile != null)
125      throw new NotImplementedException("Not Implemented Yet");
126
127    // in the absence of a profile, we insist on system
128    return matches(c1.getSystemElement(), c2.getSystemElement(), null) && matches(c1.getValue(), c2.getValue(), null);
129  }
130
131  public static void merge(ContactPoint dst, ContactPoint src) {
132    if (dst.getUseElement() == null && src.getUseElement() != null)
133      dst.setUseElement(src.getUseElement());
134    if (dst.getPeriod() == null && src.getPeriod() != null)
135      dst.setPeriod(src.getPeriod());
136  }
137
138}