首页 > 解决方案 > 在简单的速度报告中实现递归

问题描述

我正在生成速度报告。我目前遍历一组文档 (ID),对于其中的每一个,我都可以获得一个关系列表。

我想做的是为每个这些 ID 调用相同的函数以查看是否有任何进一步的关系:

我考虑了一个while循环,但后来发现不支持。

## Loop through the selection of documents
#foreach( $vDoc in $documentList )

## for each document obtain a list of all upostream relationships
#foreach($h1 in $relDao.getUpstreamDocumentIds($vDoc.document.id))

## Need recursion in here....
## need to keep getting the upstream IDs until the size is zero and then return that ID

#end
#end

标签: javarecursionapache-velocity

解决方案


这是一种实现速度递归的方法。我们可以使用宏,定义一个宏并递归地使用它和一个基本情况

片段相同。根据要求进行修改。

## ***Call the macro for the first time***
#reportMacro($documentList) 

## ***This is the definition of the macro***
#macro(reportMacro $documentList)
{    
    ## Loop through the selection of documents
    #foreach( $vDoc in $documentList )

        ## for each document obtain a list of all upostream relationships
        #foreach($h1 in $relDao.getUpstreamDocumentIds($vDoc.document.id))
            #if("$h1" != ""  && ${h1.size()} > 0)
                ## ***Recursively call the macro ***
                #reportMacro($h1)
            #else
                this marks end of recursion do some operations here if needed ... 
            #end
        #end
    #end
}
#end 

推荐阅读