com.regnosys.rosetta.translate.GeneratorUtil.xtend Maven / Gradle / Ivy
package com.regnosys.rosetta.translate
import com.regnosys.rosetta.rosetta.RosettaExternalSynonymSource
import com.regnosys.rosetta.rosetta.RosettaSynonymSource
import java.util.Collection
import java.util.List
class GeneratorUtil {
def static List synonymSourceNames(Collection sources) {
return synonymSources(sources).map[name]
}
def static List externalSynonymSources(Collection sources) {
return synonymSources(sources).filter(RosettaExternalSynonymSource).toList
}
private def static synonymSources(Collection sources) {
val collected = newArrayList
sources.forEach[traverse(collected)]
return collected.toList
}
/**
* Recursively follow super synonym sources to build a hierarchy from bottom to top.
*/
private def static void traverse(RosettaSynonymSource source, List collected) {
if (source !== null) {
collected.addLast(source);
source.superSources.forEach[traverse(collected)]
}
}
/**
* Adds element to list. If list already contains element, remove and add as the last element.
*/
private def static void addLast(List list, RosettaSynonymSource source) {
if (list.contains(source))
list.remove(source)
list.add(source)
}
/**
* Only external synonyms allow inheritance.
*/
private def static getSuperSources(RosettaSynonymSource source) {
if (source instanceof RosettaExternalSynonymSource) {
return source.superSources
}
return emptyList
}
}