AabinGunz
AabinGunz

Reputation: 12347

Check if a list's value contains in a string

I have a list def vrs=["6.0","6.1","6.1.0"] (versions) and I get a map in a function with this value def test=[version:6.1 HotFix11].

How can I check whether test's version value matches with the list vrs?

I tried this vrs.each{ver-> println test.version.contains(ver)} but it gives Exception in thread "main" java.lang.StackOverflowError

Update

Turns out, there was something wrong with my code. I tried the test case in a small Groovy script and it works.

Here is the full code:

private Map params
private def root

private def nineBelow

XmlHandler(String xml)
{
    nineBelow=["6.0","6.1","6.1.0"]

    params=[:]
    root=new XmlParser().parseText(xml)
}
def getParams()
{
    if(root.product.version.size()>0)
    {
        params.version=root.product.version.text()
    }

    /*nineBelow.each {
        println params.version  //even this throws java.lang.StackOverflowError
        //println "$it , ${params.version}"
        //println ver.getClass()+", "+params.version.getClass()
         }*/

    println nineBelow.each{ver-> println params.version.contains(ver)}

    /*I need to check whether `params.version` matches with `nineBelow` list, so i'll check for condition here*/

    params
}

Another class which calls getParams()

static main(args) {

    String fileContents = new File('E://Projects/agent6.1.xml').text
    XmlHandler xm=new XmlHandler(fileContents)  

    def params=xm.getParams()
    println params
}

Update

Even println nineBelow.each { println params.version} gives me Exception in thread "main" java.lang.StackOverflowError

More Update

It worked only after the below code

def ver=params.version
    println nineBelow.each { println ver.contains(it) }

What is the problem here?

Upvotes: 3

Views: 13012

Answers (2)

tim_yates
tim_yates

Reputation: 171074

Do you have an example that fails with some example xml? I ask because this (which you say at the top of your question crashes) actually works:

def vrs=["6.0","6.1","6.1.0"]
def test=[ version:'6.1 HotFix11' ]

vrs.each { ver-> println test.version.contains( ver ) }

and prints:

false
true
false

But I cannot find a problem with your other code, as I don't know what 'E://Projects/agent6.1.xml' contains...

Upvotes: 3

jalopaba
jalopaba

Reputation: 8119

I guess your code is as follows:

class XmlHandler {
    private Map params
    private def root

    private def nineBelow

    XmlHandler(String xml)
    {
        nineBelow=["6.0","6.1","6.1.0"]

        params=[:]
        root=new XmlParser().parseText(xml)
    }

    def getParams()
    {
        if(root.product.version.size()>0)
        {
            params.version=root.product.version.text()
        }

        nineBelow.each {
            println params.version  //even this throws java.lang.StackOverflowError
           //println "$it , ${params.version}"
           //println ver.getClass()+", "+params.version.getClass()
        }
   }
}

def doc = """
    <root>
        <product>
            <version>1.0</version>
        <version>2.0</version>
        <version>3.0</version>
        <version>4.0</version>
        <version>5.0</version>
    </product>
</root>
"""

XmlHandler handler = new XmlHandler(doc)
handler.getParams()

Therefore, in println params.version you are actually invoking getParams().version thereby entering in an endless recursive call.

Check Groovy @ symbol before fields. As tim_yates says there, ¿bug or feature?

Upvotes: 1

Related Questions