[stringtemplate-interest] Ok, I'm stumped

Terence Parr parrt at cs.usfca.edu
Mon Aug 6 12:51:52 PDT 2007


Ah. yep, i have a new one:

	/** Do a standard conversion of array attributes to a List.  Wrap the
	 *  array instead of copying like old version.  Make an
	 *  ArrayWrappedInList that knows to create an ArrayIterator.
	 */
	public static Object convertArrayToList(Object value) {
		if ( value==null ) {
			return null;
		}
		if ( value.getClass().isArray() ) {
			if ( value.getClass().getComponentType().isPrimitive() ) {
				return new ArrayWrappedInList(value);
			}
			return Arrays.asList((Object[])value);
		}
		return value;
	}

which uses my new iterator:

package org.antlr.stringtemplate.language;

import java.util.ArrayList;
import java.util.Iterator;
import java.lang.reflect.Array;

/** Turn an array into a List; subclass ArrayList for easy  
development, but
*  it really doesn't use super stuff for anything.  Ensure we create
*  ArrayIterator for this List.
*/
public class ArrayWrappedInList extends ArrayList {
	protected Object array = null;
	/** Arrays are fixed size; precompute. */
	protected int n;

	public ArrayWrappedInList(Object array) {
		this.array = array;
		n = Array.getLength(array);
	}

	public Object get(int i) {
		return Array.get(array, i);
	}

	public int size() {
		return n;
	}

	public boolean isEmpty() {
		return n==0;
	}

	public Object[] toArray() {
		return (Object[])array;
	}

	public Iterator iterator() {
		return new ArrayIterator(array);
	}
}

Can I send you a tarball to try?

Ter

On Aug 6, 2007, at 12:19 PM, Barnes, Jeff wrote:

> My version src of
> org.antlr.stringtemplate.language.ASTExpr.convertArrayToList(Object)
> starts off like
>
>                 if ( value==null ) {
>                         return null;
>                 }
>                 /*
>                 if ( !(value instanceof StringTemplate) ) {
>                         System.out.println("convert "+value+" to
> array?");
>                 }
>                 */
>                 /*
>                 if ( !arraysConvertibleToList.contains 
> (value.getClass())
> &&
>                          !(value instanceof Object[]) )
>                 {
>                         return value;
>                 }
>                 */
>                 if ( value instanceof Object[] ) {
>                         Object[] list = (Object[])value;
>                         List v = new
> StringTemplate.STAttributeList(list.le
> ngth);
>                         for (int i = 0; i < list.length; i++) {
>                                 Object elem = list[i];
>                                 v.add(elem);
>                         }
>                         value = v;
>                 }
>                 else if ( value instanceof int[] ) {
>                         int[] list = (int[])value;
>                         List v = new
> StringTemplate.STAttributeList(list.le
> ngth);
>                         for (int i = 0; i < list.length; i++) {
>                                 int elem = list[i];
>                                 v.add(new Integer(elem));
>                         }
>                         value = v;
>                 }
>                 else if ( value instanceof long[] ) {
>                         long[] list = (long[])value;
>                         List v = new
> StringTemplate.STAttributeList(list.le
> ngth);
>                         for (int i = 0; i < list.length; i++) {
>                                 long elem = list[i];
>                                 v.add(new Long(elem));
>                         }
>                         value = v;
>                 }
>                 else if ( value instanceof float[] ) {
>                         float[] list = (float[])value;
>                         List v = new
> StringTemplate.STAttributeList(list.le
> ngth);
>                         for (int i = 0; i < list.length; i++) {
>                                 float elem = list[i];
>                                 v.add(new Float(elem));
>
>
> ...
>
> There is no check for Boolean array.
>
> The latest and greatest is a much shorter method:
>
>         public static Object convertArrayToList(Object value) {
>
> 	                if ( value==null ) {
>  	                        return null;
>  	                }
>
> 	                if ( value.getClass().isArray() ) {
>
> 	                        if (
> value.getClass().getComponentType().isPrimitive() ) {
>                                 return new ArrayWrappedInList(value);
>
> 	                        }
> 	                        return Arrays.asList((Object[])value);
> 	                }
>                 return value;
>
>         }
>
> Hope this helps.
>
> Jeff
>
> -----Original Message-----
> From: stringtemplate-interest-bounces at antlr.org
> [mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of  
> Barnes,
> Jeff
> Sent: Monday, August 06, 2007 3:02 PM
> To: StringTemplate
> Subject: Re: [stringtemplate-interest] Ok, I'm stumped
>
> I just downloaded from stringtemplate.org and get the same results.  
> When
> did you push last?
>
> Jeff
>
> -----Original Message-----
> From: stringtemplate-interest-bounces at antlr.org
> [mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of  
> Barnes,
> Jeff
> Sent: Monday, August 06, 2007 2:52 PM
> To: StringTemplate
> Subject: Re: [stringtemplate-interest] Ok, I'm stumped
>
> My distro is about a month old...
>
> Terence
> [Z at 1ffbd68 I'm True
> Parr
>  I'm False
>
>      public static void main(String[] args)
>      {
>          Form f = new Form();
>          f.names = new String[] { "Terence", "Parr" };
>          f.values = new boolean[] { false, true };
>
>           StringTemplate st = new StringTemplate(
>               "$form.names,form.values:{ name, value |\n" +
>               "$name$\n" +
>               "$value$ " +
>               "$if(value)$I'm True$else$I'm False$endif$" +
>               "};separator=\"\n\"$");
>           st.setAttribute("form", f);
>           System.out.println(st.toString());
>      }
>
> -----Original Message-----
> From: stringtemplate-interest-bounces at antlr.org
> [mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of  
> Terence
> Parr
> Sent: Monday, August 06, 2007 2:47 PM
> To: StringTemplate
> Subject: Re: [stringtemplate-interest] Ok, I'm stumped
>
>
> On Aug 6, 2007, at 11:37 AM, Barnes, Jeff wrote:
>
>> No it doesn't work either.
>
> wow!
>
> Hmm...maybe I fixed something.  I get
>
> /tmp $ java Test
> Terence
> false I'm False
> Parr
> true I'm True
>
>       public static void main(String[] args)
>       {
>           Form f = new Form();
>           f.names = new String[] { "Terence", "Parr" };
>           f.values = new boolean[] { false, true };
>
>           StringTemplate st = new StringTemplate(
>               "$form.names,form.values:{ name, value |\n" +
>               "$name$\n" +
>               "$value$ " +
>               "$if(value)$I'm True$else$I'm False$endif$" +
>               "};separator=\"\n\"$");
>           st.setAttribute("form", f);
>           System.out.println(st.toString());
>       }
>
> which seems right given false followed by true.
>
> Ter
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
> _______________________________________________
> 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