diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/DataTypeVisitor.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/DataTypeVisitor.java new file mode 100644 index 000000000..dbd9c4f01 --- /dev/null +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/DataTypeVisitor.java @@ -0,0 +1,69 @@ +package org.hl7.fhir.r4b.utils; + +import org.hl7.fhir.r4b.model.Base; +import org.hl7.fhir.r4b.model.DataType; +import org.hl7.fhir.r4b.model.Property; +import org.hl7.fhir.r4b.model.Resource; +import org.hl7.fhir.utilities.Utilities; + +public class DataTypeVisitor { + + public interface IDatatypeVisitor { + Class classT(); + boolean visit(String path, T node); + } + + private boolean anyFalse; + private boolean anyTrue; + private int nodeCount; + private int selectedCount; + + public void visit(Resource resource, IDatatypeVisitor visitor) { + visitNode(resource.fhirType(), resource, visitor); + } + + @SuppressWarnings("unchecked") + private void visitNode(String path, Base node, IDatatypeVisitor visitor) { + nodeCount++; + if (node instanceof DataType && visitor.classT().isInstance(node)) { + selectedCount++; + boolean ok = visitor.visit(path, (T) node); + if (ok) { + anyTrue = true; + } else { + anyFalse = true; + } + } + for (Property p : node.children()) { + if (p.isList()) { + int i = 0; + for (Base b : p.getValues()) { + visitNode(path+"."+p.getName()+"["+i+"]", b, visitor); + i++; + } + } else { + for (Base b : p.getValues()) { + visitNode(path+"."+p.getName(), b, visitor); + } + } + } + } + + public boolean isAnyFalse() { + return anyFalse; + } + + public boolean isAnyTrue() { + return anyTrue; + } + + public int getNodeCount() { + return nodeCount; + } + + public int getSelectedCount() { + return selectedCount; + } + + +} diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/Extension.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/Extension.java index 9508dd5db..62807a278 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/Extension.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/Extension.java @@ -68,6 +68,11 @@ public class Extension extends BaseExtension implements IBaseExtension { + Class classT(); + boolean visit(String path, T node); + } + + private boolean anyFalse; + private boolean anyTrue; + private int nodeCount; + private int selectedCount; + + public void visit(Resource resource, IDatatypeVisitor visitor) { + visitNode(resource.fhirType(), resource, visitor); + } + + @SuppressWarnings("unchecked") + private void visitNode(String path, Base node, IDatatypeVisitor visitor) { + nodeCount++; + if (node instanceof DataType && visitor.classT().isInstance(node)) { + selectedCount++; + boolean ok = visitor.visit(path, (T) node); + if (ok) { + anyTrue = true; + } else { + anyFalse = true; + } + } + for (Property p : node.children()) { + if (p.isList()) { + int i = 0; + for (Base b : p.getValues()) { + visitNode(path+"."+p.getName()+"["+i+"]", b, visitor); + i++; + } + } else { + for (Base b : p.getValues()) { + visitNode(path+"."+p.getName(), b, visitor); + } + } + } + } + + public boolean isAnyFalse() { + return anyFalse; + } + + public boolean isAnyTrue() { + return anyTrue; + } + + public int getNodeCount() { + return nodeCount; + } + + public int getSelectedCount() { + return selectedCount; + } + + +}