Re: "addition" of lists

Sergiu Dumitriu <[email protected]> Fri, 24 May 2013 22:11:04 -0400
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <[email protected]>
On 05/24/2013 06:10 PM, [email protected] wrote:
>> ________________________________________
>> From: Sergiu Dumitriu [[email protected]]
>> Sent: 24 May 2013 20:44
>> To: [email protected]
>> Subject: Re: "addition" of lists
>>
>> On 05/24/2013 02:37 PM, [email protected] wrote:
>>> Hey Sergiu! A big pleasure to meet you here! Thanks for your answer. Please, see below...
>>>
>>>> ________________________________________
>>>> From: Sergiu Dumitriu [[email protected]]
>>>> Sent: 24 May 2013 19:54
>>>> To: [email protected]
>>>> Subject: Re: "addition" of lists
>>>>
>>>> On 05/24/2013 01:50 PM, [email protected] wrote:
>>>>> Hi!
>>>>>
>>>>> Probably I'll use the wrong semantic, but I'll try to explain the very simple result I want
>>>>> to get with Velocity: given two lists of results, let's me call them $list1 and $list2,
>>>>> I want to get a new list, let's call it $listTotal, including all item in $list1 and $list2.
>>>>>
>>>>> Please, how could I do that? What is the correct expression I must it in Velocity's World for his operation?
>>>>>
>>>>> Thank you so much for your help!
>>>>>
>>>>> Ricardo
>>>>>
>>>>
>>>> Hey Ricardo,
>>>>
>>>> If those are indeed lists, they should support all the methods that
>>>> Java's List class provides [1], including addAll [2], so this should work:
>>>>
>>>> #set ($listTotal = [])
>>>> #set ($discard = $listTotal.addAll($list1))
>>>> #set ($discard = $listTotal.addAll($list2))
>>>>
>>>
>>> It worked! They are indeed lists :-) But I've not a clear idea yet about what is list,
>>> what an array, what a collection, what any sort of collection... Please, could you
>>> provide me a good reference to keep trying to >>understand what I feel like a
>>> must to leverage the power of Velocity and, of course, the nice team XWiki/Velocity?
>>>
>>> These two lists come from a for you well known lines of code. Something like this...
>>>
>>> #set ($xwqlquery = "where doc.translation = 0 and doc.creationDate < '2013-01-01'
>>> and doc.parent like '%$doc.title%' and doc.fullName NOT IN ($nicestr0)
>>> order by doc.object(XWiki.XWikiUsers).last_name")
>>> #set ($results = $services.query.xwql($xwqlquery).execute())
>>>
>>> Please, is it possible to check $results if is it a list? For instance, I guessed it is a list because I can apply size() to it. Is this a right guess?
>>
>> You can always check the actual class of a variable with:
>>
>> $var.class
>>
>> This will print the classname, which will correspond to a real Java
>> class (most likely an ArrayList).
>>
> Yeap! An ArrayList.
> 
>> You can then look up that class in your favorite Java IDE and look at
>> its hierarchy: which interfaces does it implement (List, Set or Map),
>> what methods does it have, etc.
>>
> 
> And, please, how could I know what kind of objects are in the ArrayList? I guess strings is an option; objects another one....

Well, not from the list itself, but you can check the .class of each
object in the list like you did for the list:

$list.get(0).class
(of course, if there is at least one element in the list)

>>
>> One exception is that arrays (like String[]) are treated as lists by
>> Velocity, so they will also support all the List methods, even though in
>> Java they don't.
>>
> 
> I think I read about this several times but never managed to understand. I keep learning a lot!

Well, whenever printing the class of a thing shows something like "class
[L...", then you can use any of the methods that java.util.List has on
that object. Velocity does all the magic.

>>> Thank you so much for your help!
>>>
>>>>
>>>> [1] http://docs.oracle.com/javase/1.5.0/docs/api/java/util/List.html
>>>> [2] http://docs.oracle.com/javase/1.5.0/docs/api/java/util/List.html#addAll%28java.util.Collection%29
>>>
>>> Thanks!
>>
>>