velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Shinobu Kawai <>
Subject Re: Defining iterator for #foreach
Date Mon, 18 Oct 2004 23:37:14 GMT
Hi Barbara,

Nice to know that Velocity is working with Java 5.  ;)

> I prefer immutable classes because they are threadsafe, can't be
> changed by the template designer, and follow Joshua Bloch's advice in
> the book Effective Java (an excellent read, BTW).
I haven't read the book, so I don't know what's said in it, BUT you
can make it thread safe by Collections.synchronized*.  Say you have a
List called list.  You can get what you need by
  List listForTemplate =
Okay, so it's a bit annoying.  ;)

> A wrapper class (call it MapList<K,V>) for Vector<SimpleMap<K,V>> is
> lot handier that writing Vector<SimpleMap<K,V>> when I use this
> particular formulation a lot and pass it to the template designers.
Ah, you have a List of Map's.  That makes it harder.

> It is also easier for my template designers to use MapList<K,V> than
> trying to understand the intricacies of Generics in JAVA when the
> specification gets complicated.
Actually, I don't think the template designers even care about
Generics.  All they need to know is, "What are the public methods I
can use?".  I put a sample at the end of this message.  The Velocity
template will look the same, using Generics or not.

>  I can also have methods that are more
> straightforward and return objects that are automatically cast to the
> proper class.  VERY handy.
Now that's a good reason.  Readability for the template designers is
something that needs good care of.

> Even for commonly used, less complicated collections, using a wrapper
> is sometimes nice.  For example, I have a wrapper for HashMap<String,
> String> that is immutable and comes in handy for storage as a web
> session attribute.  It's the only way I can retrieve something that
> acts like HashMap<String,String> from a session attribute, since you
> can't cast to a class-with-Generics in Java 1.5.
Didn't know that.  Can you give an example of when you can't cast it? 
I tried it, but the compiler gave me no errors (only "Unsafe type
operation" warnings), and running it gave me no ClassCastException's.

>  In my case, it
> avoids a lot of warning:unchecked compilation messages in my web
> applications.
Or is this what you are talking about?

> Probably more than you wanted to know.  Despite writing extra classes
> (but not many) and a lot of frustration having to sort out what
> Generics can and cannot do, in the end I think they are a BIG plus for
> developers who can adapt.
Of course, I'll bet such developers can adapt to Generics as well.  ;)

Well, here's the sample for Generics and Velocity:
-- Java code
VelocityContext context = new VelocityContext();
List<Map<String, String>> list = new ArrayList<Map<String, String>>();

Map<String, String> map1 = new HashMap<String, String>();
map1.put("id", "1");
map1.put("name", "Barbara Baughman");

Map<String, String> map2 = new HashMap<String, String>();
map2.put("id", "2");
map2.put("name", "Shinobu Kawai");


-- Velocity Template
#foreach($map in $maplist)

-- Output
1:Barbara Baughman
2:Shinobu Kawai

Nothing changes for the Template even if you remove the Generics.  :)

Best regards,
-- Shinobu Kawai

Shinobu Kawai <>

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message