Xtend循环中断

时间:2013-03-03 20:12:50

标签: loops code-generation break xtend

在Xtend中,是否可以在循环中断或者检查以停止循环?

«FOR e:d.entitys»
    «FOR a:e.attributes»
        «IF a.eClass.name.contentEquals('Something')»
            «e.name» "This output should be output one for each Entity e"
        «ENDIF»
    «ENDFOR»
«ENDFOR»

我的输出是:

Entity 1 "This output should be output one for each Entity e"
Entity 1 "This output should be output one for each Entity e"
Entity 1 "This output should be output one for each Entity e"
Entity 2 "This output should be output one for each Entity e"
Entity 4 "This output should be output one for each Entity e"
Entity 4 "This output should be output one for each Entity e"

但我想要的是:

Entity 1 "This output should be output one for each Entity e"
Entity 2 "This output should be output one for each Entity e"
Entity 4 "This output should be output one for each Entity e"

如何实现我想要的输出?我听说你可以调用另一种方法或者其他东西,但我不知道怎么做,有人能给我看一些这个问题的代码吗? 谢谢:))

1 个答案:

答案 0 :(得分:0)

您可以使用一套来存储您已访问过的条目。例如,请考虑以下程序:

def static void main(String... args) {
    val list = #['my', 'possibly', 'possibly', 'duplicated', 'duplicated', 'duplicated', 'entities']
    val visited = new LinkedHashSet
    println(
    '''«FOR a:list»
        «IF visited.add(a)»
            «a» "This output should be output one for each Entity e"
        «ENDIF»
    «ENDFOR»''')
}

输出:

my "This output should be output one for each Entity e"
possibly "This output should be output one for each Entity e"
duplicated "This output should be output one for each Entity e"
entities "This output should be output one for each Entity e"