[stringtemplate-interest] bug in first function or just a Java5 issue?
John Snyders
jjsnyders at rcn.com
Fri Oct 20 20:28:39 PDT 2006
> -----Original Message-----
> From: stringtemplate-interest-bounces at antlr.org
> [mailto:stringtemplate-interest-bounces at antlr.org]On Behalf Of Terence
> Parr
> Sent: Friday, October 20, 2006 4:56 PM
> To: StringTemplate
> Subject: Re: [stringtemplate-interest] bug in first function or just a
> Java5 issue?
>
>
>
> On Oct 19, 2006, at 11:22 PM, John Snyders wrote:
>
> > It seems to me that the first() function has a subtle bug. This is
> > the way it currently is:
> >
> > /** Return the first attribute if multiple valued or the attribute
> > * itself if single-valued. Used in <names:first()>
> > */
> > public Object first(Object attribute) {
> > if ( attribute==null ) {
> > return null;
> > }
> > Object f = attribute;
> > attribute = convertAnythingIteratableToIterator(attribute);
> > if ( attribute instanceof Iterator ) {
> > Iterator it = (Iterator)attribute;
> > if ( it.hasNext() ) {
> > f = it.next();
> > }
> > }
> > return f;
> > }
> > According to the comment if the attribute is multi valued then first
> > () will return the first value.
> > If it is not multi valued it will just return the attribute. What
> > it doesn't say is what happens when the
> > attribute is multi valued but is empty.
>
> Hi John.
>
> Is there such a thing? How can it have multiple values if empty? I
> think what you are saying is that you passed in an object that is a
> list or something but it has no values.
Yes anything iterable or a collection or ArrayList etc can be empty but it
is still a collection (I mean collection in the loose sense so as to include
iterable).
I interpret "multiple valued" as meaning can be iterated over regardless of
how many things it actually has in it right now.
> If that is the case, it
> should still decide that it is iteratable and then get a null first
> entry.
This is exactly what I think once it is determined that it can be iterataed
over it should never go back and treat it like a scalar value. But it does
as you can see if the if branch if has next is not taken then it returns
the original attribute not the first (which would be null because the
collection is empty) item of the iterator.
I made a change to first and last I can post them if there is any interest.
> what exactly are you passing in?
public class MyCollectionLikeThing implements Iterable
Thanks,
-John
> Ter
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
More information about the stringtemplate-interest
mailing list