-
Notifications
You must be signed in to change notification settings - Fork 20
/
depth-first-execute.kt
55 lines (49 loc) · 2.48 KB
/
depth-first-execute.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import com.intuit.commons.traverser.TraverseContext
import com.intuit.commons.traverser.TraverseVisitor
import com.intuit.commons.traverser.Traverser
fun main() {
// populate the company according example at the top of this discussion
val c = Company("Company")
.businessEntity(BusinessEntity("BU-Pacific")
.team(Team("PD=Product Development")
.member(Member("Sylvester Moonstone"))
.member(Member("John Smith"))
.member(Member("Lucy Gold")))
.team(Team("SL=Sales Department")
.member(Member("Nick Citrine"))
.member(Member("Kleo Ruby")))
.team(Team("MG=Management")
.member(Member("Anna Peridot"))))
// create depth-first traverser and execute it
val foundKleoRuby = Traverser
.depthFirst(Node::children)
.traverse(c, false, object : TraverseVisitor<Node, TraverseContext<Node>> {
override fun enter(context: TraverseContext<Node>): Traverser.Action {
val node = context.thisNode()
println("enter: " + node?.name)
return if (node.name == "Kleo Ruby") {
context.setResult(true)
// request to abort the loop
println("Found. Quitting.")
Traverser.Action.QUIT
} else {
// allow advancing to the children of this node if any
Traverser.Action.CONTINUE
}
}
override fun leave(context: TraverseContext<Node>): Traverser.Action {
val node = context.thisNode()
println("leave: " + node.name)
// in post-process CONTINUE and SKIP are treated equally
// to allow advancing to the next discovered nodes, but
// do not discover children of this node anymore
return Traverser.Action.CONTINUE
}
override fun onBackRef(context: TraverseContext<Node>): Traverser.Action {
val node = context.thisNode()
println("onBackRef: " + node.name + ". What happened?")
return Traverser.Action.CONTINUE
}
})
println("foundKleoRuby: $foundKleoRuby")
}