RED-6009: Document Tree Structure

* remove instanceof as much as possible
This commit is contained in:
Kilian Schuettler 2023-05-16 14:38:09 +02:00
parent 926d507583
commit f8963feac1
3 changed files with 15 additions and 5 deletions

View File

@ -73,7 +73,7 @@ public class Document implements GenericSemanticNode {
@Override @Override
public Headline getHeadline() { public Headline getHeadline() {
return streamChildrenOfType(NodeType.HEADLINE).map(node -> (Headline) node).findFirst().orElseThrow(() -> new NotFoundException("No Headlines found in this document!")); return streamAllSubNodesOfType(NodeType.HEADLINE).map(node -> (Headline) node).findFirst().orElseThrow(() -> new NotFoundException("No Headlines found in this document!"));
} }
@ -85,7 +85,7 @@ public class Document implements GenericSemanticNode {
public Stream<Image> streamAllImages() { public Stream<Image> streamAllImages() {
return streamAllSubNodes().filter(node -> node instanceof Image).map(node -> (Image) node); return streamAllSubNodesOfType(NodeType.IMAGE).map(node -> (Image) node);
} }

View File

@ -55,8 +55,7 @@ public class Section implements GenericSemanticNode {
public Headline getHeadline() { public Headline getHeadline() {
return streamChildren()// return streamChildrenOfType(NodeType.HEADLINE)//
.filter(node -> node instanceof Headline)//
.map(node -> (Headline) node)// .map(node -> (Headline) node)//
.findFirst()// .findFirst()//
.orElseGet(() -> getParent().getHeadline()); .orElseGet(() -> getParent().getHeadline());

View File

@ -323,7 +323,7 @@ public interface SemanticNode {
/** /**
* Streams all children located directly underneath this node in the DocumentTree. * Streams all children located directly underneath this node in the DocumentTree of the provided type.
* *
* @return Stream of all children * @return Stream of all children
*/ */
@ -344,6 +344,17 @@ public interface SemanticNode {
} }
/**
* Recursively streams all SemanticNodes of the provided type located underneath this node in the DocumentTree in order.
*
* @return Stream of all SubNodes
*/
default Stream<SemanticNode> streamAllSubNodesOfType(NodeType nodeType) {
return getDocumentTree().streamAllSubEntriesInOrder(getTreeId()).filter(entry -> entry.getType().equals(nodeType)).map(DocumentTree.Entry::getNode);
}
/** /**
* The Boundary is the start and end string offsets in the reading order of the document. * The Boundary is the start and end string offsets in the reading order of the document.
* *