You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Geir Magnusson Jr." <ge...@optonline.net> on 2001/01/24 17:00:39 UTC

NOTICE : ObjectArray now is an ArrayList

As noted previously (Dec 29th, 2000), it was proposed that we change the
return of an object array ( ["a","b","c"]) from Object[] to something
else, that something else was proposed to be an ArrayList.

I made the change this morning, because Kasper Nielsen seemed to want to
use an ObjectArray in a more complicated manner than just in a
#foreach().

So, this is a warning : if you have objects that you are passing the
ObjectArray to, and are expecting an Object[] (like the Velocity testbed
was....), fix it.

Or if there is a big problem, we can back this out, but we should just
try and put this to bed for good. The IntegerRange returns an ArrayList
(  [1..5] ), so it's good to make them match.

To summarise :

Like the IntegerRange, the ObjectArray in VTL is instantiated as an
ArrayList :

#set($foo = ["a","b",c"]) -> $foo will point to an ArrayList

And for passing directly to an object in the context :

$provider.concat( [ "<", $foo , ">" ] )

concat() should be something like :
   
  public String concat( List o)

Note also that you get your own ArrayList when you #set() or just pass
one to a method, so there should be no fear of inter-thread problems. 
Muck with it to your hearts content.

geir


-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity

Re: NOTICE : ObjectArray now is an ArrayList

Posted by Daniel Rall <dl...@collab.net>.
"Geir Magnusson Jr." <ge...@optonline.net> writes:

> Daniel Rall wrote:
> > 
> > "Geir Magnusson Jr." <ge...@optonline.net> writes:
> > 
> > > Note also that you get your own ArrayList when you #set() or just pass
> > > one to a method, so there should be no fear of inter-thread problems.
> > > Muck with it to your hearts content.
> > 
> > But remember that ArrayList itself is not synchronized!  :)
> 
> I don't understand why this matters, so if I am missing something,
> please, lets get this straightened out.
> 
> When I said above that one should have no fear of inter-thread problems,
> I meant that when you have a template that has 
> 
>   #set( $foo = ["a","b","c"] )
> 
> each thread (say a request to a servlet) merging that template will get
> a new ArrayList placed in that thread's context.  Therefore, each thread
> of execution can do what it wishes with that ArrayList w/o fear of
> affecting any other thread. (I think... :\ )
> 
> Since 'the rule' is that the context must not be used/shared
> simultaneously across threads, it doesn't matter that ArrayList isn't
> synchronized. ( Aside from being a horrible idea anyway, one of the
> reasons for 'the rule' is that the VelocityContext default context
> implementation uses a HashMap, which isn't synchronized :)

Java objects already in the context have the possibility of themselves
being multithreaded.  I'm not by any means recommending this, I'm just
saying that it's possible.  Use of an ArrayList in such a situation
would lead to Big Trouble.  However, one should avoid such a design in
the first place...I just wanted to post a reminder.
-- 

Daniel Rall <dl...@collab.net>

Re: NOTICE : ObjectArray now is an ArrayList

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Daniel Rall wrote:
> 
> "Geir Magnusson Jr." <ge...@optonline.net> writes:
> 
> > Note also that you get your own ArrayList when you #set() or just pass
> > one to a method, so there should be no fear of inter-thread problems.
> > Muck with it to your hearts content.
> 
> But remember that ArrayList itself is not synchronized!  :)

I don't understand why this matters, so if I am missing something,
please, lets get this straightened out.

When I said above that one should have no fear of inter-thread problems,
I meant that when you have a template that has 

  #set( $foo = ["a","b","c"] )

each thread (say a request to a servlet) merging that template will get
a new ArrayList placed in that thread's context.  Therefore, each thread
of execution can do what it wishes with that ArrayList w/o fear of
affecting any other thread. (I think... :\ )

Since 'the rule' is that the context must not be used/shared
simultaneously across threads, it doesn't matter that ArrayList isn't
synchronized. ( Aside from being a horrible idea anyway, one of the
reasons for 'the rule' is that the VelocityContext default context
implementation uses a HashMap, which isn't synchronized :)

Anyway, if there is something I am missing here...

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity

Re: NOTICE : ObjectArray now is an ArrayList

Posted by Daniel Rall <dl...@collab.net>.
"Geir Magnusson Jr." <ge...@optonline.net> writes:

> Note also that you get your own ArrayList when you #set() or just pass
> one to a method, so there should be no fear of inter-thread problems. 
> Muck with it to your hearts content.

But remember that ArrayList itself is not synchronized!  :)
-- 

Daniel Rall <dl...@collab.net>

Re: NOTICE : ObjectArray now is an ArrayList

Posted by Daniel Rall <dl...@collab.net>.
"Geir Magnusson Jr." <ge...@optonline.net> writes:

> Note also that you get your own ArrayList when you #set() or just pass
> one to a method, so there should be no fear of inter-thread problems. 
> Muck with it to your hearts content.

But remember that ArrayList itself is not synchronized!  :)
-- 

Daniel Rall <dl...@collab.net>