
001package org.hl7.fhir.r5.utils; 002 003import org.hl7.fhir.r5.model.Base; 004import org.hl7.fhir.r5.model.DataType; 005import org.hl7.fhir.r5.model.Property; 006import org.hl7.fhir.r5.model.Resource; 007import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage; 008 009@MarkedToMoveToAdjunctPackage 010public class DataTypeVisitor { 011 012 public interface IDatatypeVisitor<T extends DataType> { 013 Class<T> classT(); 014 boolean visit(String path, T node); 015 } 016 017 private boolean anyFalse; 018 private boolean anyTrue; 019 private int nodeCount; 020 private int selectedCount; 021 022 public <T extends DataType> void visit(Resource resource, IDatatypeVisitor<T> visitor) { 023 visitNode(resource.fhirType(), resource, visitor); 024 } 025 026 @SuppressWarnings("unchecked") 027 private <T extends DataType> void visitNode(String path, Base node, IDatatypeVisitor<T> visitor) { 028 nodeCount++; 029 if (node instanceof DataType && visitor.classT().isInstance(node)) { 030 selectedCount++; 031 boolean ok = visitor.visit(path, (T) node); 032 if (ok) { 033 anyTrue = true; 034 } else { 035 anyFalse = true; 036 } 037 } 038 for (Property p : node.children()) { 039 if (p.isList()) { 040 int i = 0; 041 for (Base b : p.getValues()) { 042 visitNode(path+"."+p.getName()+"["+i+"]", b, visitor); 043 i++; 044 } 045 } else { 046 for (Base b : p.getValues()) { 047 visitNode(path+"."+p.getName(), b, visitor); 048 } 049 } 050 } 051 } 052 053 public boolean isAnyFalse() { 054 return anyFalse; 055 } 056 057 public boolean isAnyTrue() { 058 return anyTrue; 059 } 060 061 public int getNodeCount() { 062 return nodeCount; 063 } 064 065 public int getSelectedCount() { 066 return selectedCount; 067 } 068 069 070}