001package org.hl7.fhir.r5.utils.structuremap;
002
003import org.hl7.fhir.r5.elementmodel.Property;
004import org.hl7.fhir.r5.fhirpath.TypeDetails;
005import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
006
007import java.util.ArrayList;
008import java.util.List;
009
010public class VariablesForProfiling {
011  private final StructureMapUtilities structureMapUtilities;
012  private List<VariableForProfiling> list = new ArrayList<VariableForProfiling>();
013  private boolean optional;
014  private boolean repeating;
015
016  public VariablesForProfiling(StructureMapUtilities structureMapUtilities, boolean optional, boolean repeating) {
017    this.structureMapUtilities = structureMapUtilities;
018    this.optional = optional;
019    this.repeating = repeating;
020  }
021
022  public void add(VariableMode mode, String name, String path, Property property, TypeDetails types) {
023    add(mode, name, new PropertyWithType(path, property, null, types));
024  }
025
026  public void add(VariableMode mode, String name, String path, Property baseProperty, Property profileProperty, TypeDetails types) {
027    add(mode, name, new PropertyWithType(path, baseProperty, profileProperty, types));
028  }
029
030  public void add(VariableMode mode, String name, PropertyWithType property) {
031    VariableForProfiling vv = null;
032    for (VariableForProfiling v : list)
033      if ((v.getMode() == mode) && v.getName().equals(name))
034        vv = v;
035    if (vv != null)
036      list.remove(vv);
037    list.add(new VariableForProfiling(mode, name, property));
038  }
039
040  public VariablesForProfiling copy(boolean optional, boolean repeating) {
041    VariablesForProfiling result = new VariablesForProfiling(structureMapUtilities, optional, repeating);
042    result.list.addAll(list);
043    return result;
044  }
045
046  public VariablesForProfiling copy() {
047    VariablesForProfiling result = new VariablesForProfiling(structureMapUtilities, optional, repeating);
048    result.list.addAll(list);
049    return result;
050  }
051
052  public VariableForProfiling get(VariableMode mode, String name) {
053    if (mode == null) {
054      for (VariableForProfiling v : list)
055        if ((v.getMode() == VariableMode.OUTPUT) && v.getName().equals(name))
056          return v;
057      for (VariableForProfiling v : list)
058        if ((v.getMode() == VariableMode.INPUT) && v.getName().equals(name))
059          return v;
060    }
061    for (VariableForProfiling v : list)
062      if ((v.getMode() == mode) && v.getName().equals(name))
063        return v;
064    return null;
065  }
066
067  public String summary() {
068    CommaSeparatedStringBuilder s = new CommaSeparatedStringBuilder();
069    CommaSeparatedStringBuilder t = new CommaSeparatedStringBuilder();
070    for (VariableForProfiling v : list)
071      if (v.getMode() == VariableMode.INPUT)
072        s.append(v.summary());
073      else
074        t.append(v.summary());
075    return "source variables [" + s.toString() + "], target variables [" + t.toString() + "]";
076  }
077}