While-like structure in Velocity?

2013-05-15 Thread Christopher Schultz
All,

I've searched the archives for while capabilities in Velocity, and
mostly the answers have been things like that's beyond the scope of
Velocity... you want a real programming language for that.

I'd like to do something that naturally would use a while loop, but
perhaps someone can offer an alternative suggestion.

I need to traverse a queue, but that queue needs to be able to modify
itself during the processing. Basically, something like this:

while (!queue.empty) {
  // do something with the head element
  // possible enqueue some more items at the back
}

#foreach would be the way to do it if the queue didn't have to be
modified, but I'll get an exception if I try to traverse an iterator
over the queue and then modify it mid-flight.

Right now, I have this written as a JSP but I'd rather have it as a
Velocity template.

My only thought is to create an ugly class that implements Iterator and
wraps the queue but is much less strict than a standard iterator. Any
suggestions?

-chris



signature.asc
Description: OpenPGP digital signature


Re: While-like structure in Velocity?

2013-05-15 Thread Claude Brisson
 #foreach would be the way to do it if the queue didn't have to be
 modified, but I'll get an exception if I try to traverse an iterator
 over the queue and then modify it mid-flight.

Even if the queue is materialized by a linked list rather than by an
array? #foreach over a linked list is the way to go, I think.


  Claude

-
To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org
For additional commands, e-mail: user-h...@velocity.apache.org



Re: While-like structure in Velocity?

2013-05-15 Thread Sergiu Dumitriu
On 05/15/2013 12:11 PM, Christopher Schultz wrote:
 All,
 
 I've searched the archives for while capabilities in Velocity, and
 mostly the answers have been things like that's beyond the scope of
 Velocity... you want a real programming language for that.
 
 I'd like to do something that naturally would use a while loop, but
 perhaps someone can offer an alternative suggestion.
 
 I need to traverse a queue, but that queue needs to be able to modify
 itself during the processing. Basically, something like this:
 
 while (!queue.empty) {
   // do something with the head element
   // possible enqueue some more items at the back
 }
 
 #foreach would be the way to do it if the queue didn't have to be
 modified, but I'll get an exception if I try to traverse an iterator
 over the queue and then modify it mid-flight.
 
 Right now, I have this written as a JSP but I'd rather have it as a
 Velocity template.
 
 My only thought is to create an ugly class that implements Iterator and
 wraps the queue but is much less strict than a standard iterator. Any
 suggestions?
 
 -chris
 

While not a very clean solution, you can use a very large foreach and
manually work with the queue inside the loop, and #break($foreach) once
the queue is empty:

#foreach($i in [0..10])
  #if ($queue.isEmpty())
#break($foreach)
  #end
  #set ($item = $queue.pop())
  ## Do stuff with $item
  #if (should add more things)
#set ($discard = $queue.add(new things))
  #end
#end

-- 
Sergiu Dumitriu
http://purl.org/net/sergiu

-
To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org
For additional commands, e-mail: user-h...@velocity.apache.org