5

In Xtend, is it possible to break in a loop or have a check to discontinue the loop?

«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»

My output is:

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"

But what I want is:

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"

How can my desired output be implemented? I heard you can call another method or something but I have no idea how to do that, could someone show me some code for this problem? Thank you :)

Charles Henry
  • 353
  • 3
  • 16

1 Answers1

0

you could use a set to store the entries you already visited. As an example, consider the following program:

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»''')
}

It outputs:

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"
Danilo Pianini
  • 966
  • 8
  • 19