traverseDescendants
Function
Common
fun DelegatableNode.traverseDescendants(
key: Any?,
block: (TraversableNode) -> TraverseDescendantsAction,
)
Conditionally executes block
for each descendant with a matching key
.
Note 1: For nodes that do not have the same key, it will continue to execute the block
for
descendants below that non-matching node (where there may be a node that matches).
Note 2: The parameter block
's return value TraverseDescendantsAction
will determine the next
step in the traversal.
Common
fun <T> T.traverseDescendants(block: (T) -> TraverseDescendantsAction) where T : TraversableNode
Conditionally executes block
for each descendant of the same class.
Note 1: For nodes that do not have the same key, it will continue to execute the block
for the
descendants below that non-matching node (where there may be a node that matches).
Note 2: The parameter block
's return value TraverseDescendantsAction
will determine the next
step in the traversal.
Code Examples
traverseDescendantsWithKeyDemo
/**
* Simplified example of traverseDescendants with a key. For a full featured sample, look below at
* [TraverseModifierDemo].
*/
fun traverseDescendantsWithKeyDemo() {
val customTraversableModifierNode = CustomTraversableModifierNode()
with(customTraversableModifierNode) {
traverseDescendants(traverseKey) {
if (it is CustomTraversableModifierNode) {
it.doSomethingWithDescendant()
}
// [traverseDescendants()] actually has three options:
// - ContinueTraversal
// - SkipSubtreeAndContinueTraversal - rarely used
// - CancelTraversal
// They are pretty self explanatory. Usually, you just want to continue or cancel the
// search. In some rare cases, you might want to skip the subtree but continue searching
// the tree.
TraverseDescendantsAction.ContinueTraversal
}
}
}
traverseDescendantsDemo
/**
* Simplified example of traverseDescendants. For a full featured sample, look below at
* [TraverseModifierDemo].
*/
fun traverseDescendantsDemo() {
val customTraversableModifierNode = CustomTraversableModifierNode()
with(customTraversableModifierNode) {
traverseDescendants {
// Because I use the existing key of the class, I can guarantee 'it' will be of the same
// type as the class, so I can call my functions directly.
it.doSomethingWithDescendant()
// [traverseDescendants()] actually has three options:
// - ContinueTraversal
// - SkipSubtreeAndContinueTraversal - rarely used
// - CancelTraversal
// They are pretty self explanatory. Usually, you just want to continue or cancel the
// search. In some rare cases, you might want to skip the subtree but continue searching
// the tree.
TraverseDescendantsAction.ContinueTraversal
}
}
}