[stringtemplate-interest] bug in first function or just a Java 5 issue?

Terence Parr parrt at cs.usfca.edu
Fri Oct 20 13:56:02 PDT 2006


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.  If that is the case, it  
should still decide that it is iteratable and then get a null first  
entry. what exactly are you passing in?

Ter


More information about the stringtemplate-interest mailing list