001package org.hl7.fhir.r5.comparison;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.hl7.fhir.r5.comparison.ResourceComparer.MessageCounts;
007import org.hl7.fhir.r5.model.StructureDefinition;
008import org.hl7.fhir.utilities.validation.ValidationMessage;
009import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
010
011public class StructuralMatch<T> {
012
013  private String name; // this is used in some contexts to carry name when T is a pretty abstract type
014  private T left;
015  private T right;
016  private List<ValidationMessage> messages = new ArrayList<>();
017  private List<StructuralMatch<T>> children = new ArrayList<>();
018 
019  public StructuralMatch() {
020    // root, just a place holder...
021  }
022 
023  public StructuralMatch(T left, T right) {
024    super();
025    this.left = left;
026    this.right = right;
027  }
028 
029  public StructuralMatch(T left, T right, ValidationMessage msg) {
030    super();
031    this.left = left;
032    this.right = right;
033    if (msg != null) {
034      this.messages.add(msg);
035    }
036  }
037 
038  public StructuralMatch(ValidationMessage msg, T right) {
039    super();
040    this.messages.add(msg);
041    this.right = right;
042  }
043
044  public StructuralMatch(T left, ValidationMessage msg) {
045    super();
046    this.left = left;
047    this.messages.add(msg);
048  }
049   
050  public T getLeft() {
051    return left;
052  }
053  public T getRight() {
054    return right;
055  }
056
057  public List<StructuralMatch<T>> getChildren() {
058    return children;
059  }
060
061  /**
062   * return left if it exists, or return right (which might be null)
063   *
064   * This is used when accessing whatever makes the items common
065   *
066   * @return
067   */
068  public T either() {
069    return left != null ? left : right;
070  }
071
072  public boolean hasLeft() {
073    return left != null;
074  }
075 
076  public boolean hasRight() {
077    return right != null;
078  }
079
080  public List<ValidationMessage> getMessages() {
081    return messages;
082  }
083
084  public boolean hasErrors() {
085    for (ValidationMessage vm : messages) {
086      if (vm.getLevel() == IssueSeverity.ERROR) {
087        return true;
088      }
089    }
090    return false;
091  }
092
093  public void countMessages(MessageCounts cnts) {
094    for (ValidationMessage vm : getMessages()) {
095      if (vm.getLevel() == IssueSeverity.ERROR) {
096        cnts.error();
097      } else if (vm.getLevel() == IssueSeverity.WARNING) {
098        cnts.warning();
099      } else if (vm.getLevel() == IssueSeverity.INFORMATION) {
100        cnts.hint();
101      } 
102    }
103    for (StructuralMatch<T> c : children) {
104      c.countMessages(cnts);
105    }
106  }
107
108  public String getName() {
109    return name;
110  }
111
112  public StructuralMatch<T> setName(String name) {
113    this.name = name;
114    return this;
115  }
116
117
118  
119}