You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Maarten Boekhold <bo...@gmx.com> on 2016/03/29 12:25:24 UTC

Check if List is of specific size and elements of specific type

Hi,

Is there a quick and easy way to do something like:

assert listVariable == [int, int]

eg, the list is of size 2 and each element is an int?

Maarten

Re: Check if List is of specific size and elements of specific type

Posted by "Søren Berg Glasius (GR8Conf EU)" <sb...@gr8conf.org>.
Hi Maarten,

You could be close with this.

def listVariable = [1,2]
assert listVariable*.getClass() == [int, int]

but your assert will have to be

assert listVariable*.getClass() == [Integer, Integer]

since ints are actually the object type Integer

Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team

GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius 
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem

From: Maarten Boekhold <bo...@gmx.com>
Reply: users@groovy.apache.org <us...@groovy.apache.org>
Date: March 29, 2016 at 12:25:42
To: users@groovy.incubator.apache.org <us...@groovy.incubator.apache.org>
Subject:  Check if List is of specific size and elements of specific type  

Hi,  

Is there a quick and easy way to do something like:  

assert listVariable == [int, int]  

eg, the list is of size 2 and each element is an int?  

Maarten  

Re: Check if List is of specific size and elements of specific type

Posted by Maarten Boekhold <bo...@gmx.com>.
Him

Thanks all for the response, this works fine. Only draw-back is that 
IntelliJ gives a warning about incompatible types. Almost as if it 
doesn't understand the spread operator...

Maarten

On 2016-03-29 14:29, Marcos Carceles wrote:
> Would this work?
>
> listVariable*.class == [Integer, Integer]
>
> On 29 March 2016 at 12:25, Maarten Boekhold <boekhold@gmx.com 
> <ma...@gmx.com>> wrote:
>
>     Hi,
>
>     Is there a quick and easy way to do something like:
>
>     assert listVariable == [int, int]
>
>     eg, the list is of size 2 and each element is an int?
>
>     Maarten
>
>


Re: Check if List is of specific size and elements of specific type

Posted by Cédric Champeau <ce...@gmail.com>.
I can't resist a more verbose, but more functional way:

[1,2].identity { assert size() == 2 } .every { it instanceof Integer }

2016-03-29 14:07 GMT+02:00 Søren Berg Glasius (GR8Conf EU) <
sbglasius@gr8conf.org>:

> @dinko yeah, that's why I choose to do my example with
> listVariable*.getClass()
>
> Best regards,
> Søren Berg Glasius
> GR8Conf Europe organizing team
>
> GR8Conf ApS
> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
> --- GR8Conf - Dedicated to the Groovy Ecosystem
>
> From: Dinko Srkoč <di...@gmail.com> <di...@gmail.com>
> Reply: users@groovy.apache.org <us...@groovy.apache.org>
> <us...@groovy.apache.org>
> Date: March 29, 2016 at 14:04:50
> To: users@groovy.apache.org <us...@groovy.apache.org>
> <us...@groovy.apache.org>
> Subject:  Re: Check if List is of specific size and elements of specific
> type
>
> On 29 March 2016 at 12:29, Marcos Carceles <ma...@gmail.com>
> wrote:
> > Would this work?
> >
> > listVariable*.class == [Integer, Integer]
>
> It would in this particular example, but this may be dangerous for
> some other cases. Try e.g. this:
>
> [1, [:], [class: 1]]*.class
>
> Cheers,
> Dinko
>
> >
> > On 29 March 2016 at 12:25, Maarten Boekhold <bo...@gmx.com> wrote:
> >>
> >> Hi,
> >>
> >> Is there a quick and easy way to do something like:
> >>
> >> assert listVariable == [int, int]
> >>
> >> eg, the list is of size 2 and each element is an int?
> >>
> >> Maarten
> >
> >
>
>

Re: Check if List is of specific size and elements of specific type

Posted by Dinko Srkoč <di...@gmail.com>.
On 29 March 2016 at 14:07, Søren Berg Glasius (GR8Conf EU)
<sb...@gr8conf.org> wrote:
> @dinko yeah, that's why I choose to do my example with
> listVariable*.getClass()

Yeah, I know. :-)

One needs to get burned once or twice in order to learn (one == me,
but I imagine I'm not alone ;-) ).

- Dinko

>
> Best regards,
> Søren Berg Glasius
> GR8Conf Europe organizing team
>
> GR8Conf ApS
> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
> --- GR8Conf - Dedicated to the Groovy Ecosystem
>
> From: Dinko Srkoč <di...@gmail.com>
> Reply: users@groovy.apache.org <us...@groovy.apache.org>
> Date: March 29, 2016 at 14:04:50
> To: users@groovy.apache.org <us...@groovy.apache.org>
> Subject:  Re: Check if List is of specific size and elements of specific
> type
>
> On 29 March 2016 at 12:29, Marcos Carceles <ma...@gmail.com>
> wrote:
>> Would this work?
>>
>> listVariable*.class == [Integer, Integer]
>
> It would in this particular example, but this may be dangerous for
> some other cases. Try e.g. this:
>
> [1, [:], [class: 1]]*.class
>
> Cheers,
> Dinko
>
>>
>> On 29 March 2016 at 12:25, Maarten Boekhold <bo...@gmx.com> wrote:
>>>
>>> Hi,
>>>
>>> Is there a quick and easy way to do something like:
>>>
>>> assert listVariable == [int, int]
>>>
>>> eg, the list is of size 2 and each element is an int?
>>>
>>> Maarten
>>
>>

Re: Check if List is of specific size and elements of specific type

Posted by "Søren Berg Glasius (GR8Conf EU)" <sb...@gr8conf.org>.
@dinko yeah, that's why I choose to do my example with listVariable*.getClass() 

Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team

GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius 
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem

From: Dinko Srkoč <di...@gmail.com>
Reply: users@groovy.apache.org <us...@groovy.apache.org>
Date: March 29, 2016 at 14:04:50
To: users@groovy.apache.org <us...@groovy.apache.org>
Subject:  Re: Check if List is of specific size and elements of specific type  

On 29 March 2016 at 12:29, Marcos Carceles <ma...@gmail.com> wrote:  
> Would this work?  
>  
> listVariable*.class == [Integer, Integer]  

It would in this particular example, but this may be dangerous for  
some other cases. Try e.g. this:  

[1, [:], [class: 1]]*.class  

Cheers,  
Dinko  

>  
> On 29 March 2016 at 12:25, Maarten Boekhold <bo...@gmx.com> wrote:  
>>  
>> Hi,  
>>  
>> Is there a quick and easy way to do something like:  
>>  
>> assert listVariable == [int, int]  
>>  
>> eg, the list is of size 2 and each element is an int?  
>>  
>> Maarten  
>  
>  

Re: Check if List is of specific size and elements of specific type

Posted by Dinko Srkoč <di...@gmail.com>.
On 29 March 2016 at 12:29, Marcos Carceles <ma...@gmail.com> wrote:
> Would this work?
>
> listVariable*.class == [Integer, Integer]

It would in this particular example, but this may be dangerous for
some other cases. Try e.g. this:

  [1, [:], [class: 1]]*.class

Cheers,
Dinko

>
> On 29 March 2016 at 12:25, Maarten Boekhold <bo...@gmx.com> wrote:
>>
>> Hi,
>>
>> Is there a quick and easy way to do something like:
>>
>> assert listVariable == [int, int]
>>
>> eg, the list is of size 2 and each element is an int?
>>
>> Maarten
>
>

Re: Check if List is of specific size and elements of specific type

Posted by Marcos Carceles <ma...@gmail.com>.
Would this work?

listVariable*.class == [Integer, Integer]

On 29 March 2016 at 12:25, Maarten Boekhold <bo...@gmx.com> wrote:

> Hi,
>
> Is there a quick and easy way to do something like:
>
> assert listVariable == [int, int]
>
> eg, the list is of size 2 and each element is an int?
>
> Maarten
>