All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.regnosys.rosetta.generator.python.util.Util.xtend Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
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 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}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy