com.regnosys.rosetta.generator.python.util.Util.xtend Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of python Show documentation
Show all versions of python Show documentation
Generators to create python for rosetta
package com.regnosys.rosetta.generator.python.util
import java.util.function.Function
import java.util.Iterator
import java.util.NoSuchElementException
import com.regnosys.rosetta.rosetta.RosettaType
import com.regnosys.rosetta.rosetta.RosettaModel
class Util {
static def Iterable distinct(Iterable parentIterable) {
return new DistinctByIterator(parentIterable, [it])
}
static def Iterable distinctBy(Iterable parentIterable, Function extractFunction) {
return new DistinctByIterator(parentIterable, extractFunction)
}
static def boolean exists(Iterable super T> iter, Class clazz) {
!iter.filter(clazz).empty
}
static def String getNamespace (RosettaModel rm) {
return rm.getName.split("\\.").get(0)
}
private static class DistinctByIterator implements Iterable{
val Iterable iterable;
val Function extractFunction;
new(Iterable iterable, Function extractFunction) {
this.iterable = iterable
this.extractFunction = extractFunction
}
override iterator() {
val parentIterator = iterable.iterator
return new Iterator() {
val read = newHashSet
var T readNext;
override hasNext() {
if (readNext!==null) return true;
while (true){
if (!parentIterator.hasNext) return false
readNext = parentIterator.next
val compareVal = extractFunction.apply(readNext)
if (!read.contains(compareVal)) {
read.add(compareVal);
return true;
}
}
}
override next() {
if (hasNext) {
val result = readNext
readNext = null
return result
}
else {
throw new NoSuchElementException("read past end of iterator")
}
}
}
}
}
static def String fullname(RosettaType clazz) '''«clazz.model.name».«clazz.name»'''
static def String packageName(RosettaType clazz) {clazz.model.name}
}