現在のアプリケーションでは、ツリーをたどって特定のデバイス (および子デバイス) 上のすべてのオペレーターをキャプチャする必要があります。デバイスには、特定のオペレーターを含む子デバイスを含めることができます。
Groovy で再帰を使用するのは初めてなので、正しいことを行っているかどうか疑問に思っています..? 物事を行うためのより良い方法を学ぶのに役立つ指針はありますか?
def listOperators(device) {
// list with all operator id's
def results = []
// closure to traverse down the tree
def getAllOperators = { aDevice->
if(aDevice) {
aDevice.operators.each { it ->
results << it.id
}
}
if (aDevice?.children) {
aDevice.children.each { child ->
results << owner.call(child)
}
}
}
// call the closure with the given device
getAllOperators(device)
// return list with unique results
return results.unique()
}