[stringtemplate-interest] handling of arrays

Nate misc at n4te.com
Thu Feb 1 12:18:32 PST 2007


It does work, I was wrong. In fact, you don't even need a cast...

String[] strings = new String[] {"abc", "def", "ghi"};
Object[] stringsHiddenByObjectArray = strings;

What I was thinking of was going the other way...

Object[] objects = new Object[] {"jkl", "mno", "pqr"};
String[] thisDoesntWork = (String[])objects;

-Nate


Harry Karadimas wrote:
> Sorry about the lengthy post, but I checked and found that the cast to 
> (Object[])
> actually works !
>
> Below is a "small" demo  class that demonstrates various uses.
> To make it short, what works is :
>   public static Object convertArrayToList(Object value)
>   {
>     if (value==null || !value.getClass().isArray()) return value;
>     return Arrays.asList((Object[])value); 
>   }
>
> --------
>
> package stringtemplate_test;
>
> import java.util.ArrayList;
> import java.util.Arrays;
> import java.util.Collection;
> import java.util.Iterator;
> import java.util.List;
>
> /**
>  * "Lots of Lists", to test various Array to List conversion strategies
>  * @author KARADIMAS
>  */
> public class Lol
> {
>   static Object[] myArray = {"A", "B", "C"};
>   static List myList = new ArrayList();
>   static Object[][][] threeLevels = {
>       {
>         {"1.1.1", "1.1.2", "1.1.3"},
>         {"1.2.1", "1.2.2"}
>       },
>       {
>         {"2.1.1"},
>         {"2.2.1", "2.2.2"}
>       },
>       {
>         {"3.1.1", "3.1.2"},
>         {"3.2.1"}
>       },
>       {
>         {myList}
>       },
>   };
>   static int[] myInts = {1,2,3};
>  
>   public static void main(String[] args)
>   {
>     //further init of myList
>     myList.add("X");
>     myList.add(myArray);
>     myList.add("Z");
>     System.out.println("Original list :");
>     printList(threeLevels);
>     System.out.println("convertArrayToList1() :");
>     Object r = convertArrayToList1(threeLevels);
>     printList((List) r);
>     System.out.println("convertArrayToList2() :");
>     r = convertArrayToList2(threeLevels);
>     printList((List) r);
>     System.out.println("convertAllForStUsage() :");
>     r = convertAllAtOnce(threeLevels);
>     printList((List) r);
>     r = convertArrayToList1(myInts);
>     printList(r);
>   }
>  
>   /**
>    * simplest, works ok if called before each use, maybe the best 
> approach if
>    * used in lazy (= just in time) execution.
>    * Note here that Object[][]...[] is an instance of Object[] (and of
>    * Object, incidentally)
>    */
>   public static Object convertArrayToList1(Object value)
>   {
>     if (value==null || !value.getClass().isArray()) return value;
>     return Arrays.asList((Object[])value); 
>   }
>  
>   /**
>    * handles sub-arrays recursively at once, but what about arrays 
> inside lists?
>    */
>   public static Object convertArrayToList2(Object value)
>   {
>     if (value==null || !value.getClass().isArray()) return value;
>     Object[] oa1 = (Object[]) value;
>     Object[] oa2 = new Object[oa1.length];
>     System.arraycopy(oa1, 0, oa2, 0, oa1.length);
>     for (int i = 0; i < oa2.length; i++) oa2[i] = 
> convertArrayToList2(oa2[i]);   
>     return Arrays.asList(oa2); 
>   }
>
>   /**
>    * More complex method.
>    * Manages both lists and arrays, but what about arrays inside maps, for
>    * example ? And also, why convert now arrays that might never be used ?
>    */
>   public static Object convertAllAtOnce(Object value)
>   {
>     if ( !isListable(value) ) return value;
>     ArrayList res = new ArrayList(); //we always return a new list
>     if (value instanceof List) {
>       //process the list
>       List vList = (List) value;
>       for (Iterator iter = vList.iterator(); iter.hasNext();) {
>         res.add(convertAllAtOnce(iter.next()));               
>       }
>       return res;
>     }
>     //it's an array, process its elements
>     Object[] oa = (Object[]) value;
>     for (int i = 0; i < oa.length; i++) 
> res.add(convertAllAtOnce(oa[i]));       
>     return res;
>   }
>
>   /** helper function to clarify code */
>   private static final boolean isListable(Object o)
>   {
>     return (o instanceof List) || (o != null && o.getClass().isArray());
>   }
>  
>   public static void printList(Object v) { printList("", v); }
>  
>   public static void printList(String indent, Object v) {
>     if (!isListable(v)) {
>       System.out.println(indent+"v="+v);
>       return;
>     }
>     if (v instanceof List) {
>       System.out.println(indent+"(");
>       List lst = (List) v;
>       for (Iterator iter = lst.iterator(); iter.hasNext();) {
>         Object o = (Object) iter.next();
>         printList(indent+"  ", o);
>       }
>       System.out.println(indent+")");
>       return;
>     }
>     Object[] oa = (Object[]) v;
>     System.out.println(indent+"[//array");
>     for (int i = 0; i < oa.length; i++) printList(indent+"  ", oa[i]);
>     System.out.println(indent+"]//array");
>   }
>    
> }
>
>                                                        Harry Karadimas
> /______________________________________________________________________
> Dr Harry Karadimas  Medecin Ingenieur resp. Recherche et Developpement
> Departement d'Information Hospitalier
> CHU Henri Mondor 51, av. du Mal de Lattre de Tassigny   94010  CRETEIL
> tel : (00 33 1) 49 81 21 79                fax : (00 33 1) 49 81 27 08
> secr.: (00 33 1) 49 81 23 82 m.el.:harry.karadimas at hmn.ap-hop-paris.fr
> /
>
>
> stringtemplate-interest-request at antlr.org a écrit :
>> Send stringtemplate-interest mailing list submissions to
>> 	stringtemplate-interest at antlr.org
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>> 	http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
>> or, via email, send a message with subject or body 'help' to
>> 	stringtemplate-interest-request at antlr.org
>>
>> You can reach the person managing the list at
>> 	stringtemplate-interest-owner at antlr.org
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of stringtemplate-interest digest..."
>>
>>
>> Today's Topics:
>>
>>    1. Re: handling of arrays (John Snyders) (Terence Parr)
>>    2. Re: handling of arrays (John Snyders) (Terence Parr)
>>    3. Re: Porting StringTemplate (Kay Roepke)
>>
>>
>> ----------------------------------------------------------------------
>>
>> Message: 1
>> Date: Tue, 30 Jan 2007 12:14:43 -0800
>> From: Terence Parr <parrt at cs.usfca.edu>
>> Subject: Re: [stringtemplate-interest] handling of arrays (John
>> 	Snyders)
>> To: Nate <misc at n4te.com>
>> Cc: StringTemplate <stringtemplate-interest at antlr.org>
>> Message-ID: <E02F2DC0-7AE7-4B2B-A69A-49E19AD2CCE8 at cs.usfca.edu>
>> Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed
>>
>> crap.  Ok, back to arrayiterator then.
>> Ter
>> On Jan 30, 2007, at 11:54 AM, Nate wrote:
>>
>>   
>>> Unless you are using Java 5+, you'd have to use System.arrayCopy to  
>>> copy the array to an Object array.
>>>
>>> -Nate
>>>
>>>
>>> Terence Parr wrote:
>>>     
>>>> Awesome!  Heh, it's exactly what we want I think.
>>>>
>>>> Wait, class cast issue:
>>>>
>>>> 	public static Object convertArrayToList(Object value) {
>>>> 		if ( value==null ) {
>>>> 			return null;
>>>> 		}
>>>> 		if ( value.getClass().isArray() ) {
>>>> 			return Arrays.asList((Object[])value);  // CAST ISSUE!
>>>> 		}
>>>> 		return value;
>>>> 	}
>>>>
>>>> won't work as value is not Object[] most of the time...but how do  
>>>> you  get it to compile w/o the cast?
>>>>
>>>> Ter
>>>>
>>>> On Jan 30, 2007, at 9:13 AM, Harry Karadimas wrote:
>>>>
>>>>
>>>>       
>>>>> Hi, I don't know if that relates to the previous post, but java   
>>>>> *does* have something
>>>>> to encapsulate an array into a list, and that is the "asList"   
>>>>> method of the java.util.Arrays
>>>>> class.
>>>>>
>>>>> Object[] myArray = ...
>>>>> List myList = Arrays.asList(myArray);
>>>>>
>>>>> Harry Karadimas   
>>>>> ____________________________________________________________________ 
>>>>> __  Dr Harry Karadimas Medecin Ingenieur resp. Recherche et   
>>>>> Developpement Departement d'Information Hospitalier CHU Henri   
>>>>> Mondor 51, av. du Mal de Lattre de Tassigny 94010 CRETEIL tel :  
>>>>> (00  33 1) 49 81 21 79 fax : (00 33 1) 49 81 27 08 secr.: (00 33  
>>>>> 1) 49  81 23 82 m.el.:harry.karadimas at hmn.ap-hop-paris.fr
>>>>>
>>>>>
>>>>> stringtemplate-interest-request at antlr.org a ?crit :
>>>>>
>>>>>         
>>>>>> Send stringtemplate-interest mailing list submissions to   
>>>>>> stringtemplate-interest at antlr.org To subscribe or unsubscribe  
>>>>>> via  the World Wide Web, visit http://www.antlr.org:8080/ 
>>>>>> mailman/ listinfo/stringtemplate-interest or, via email, send a  
>>>>>> message  with subject or body 'help' to stringtemplate-interest-  
>>>>>> request at antlr.org You can reach the person managing the list at   
>>>>>> stringtemplate-interest-owner at antlr.org When replying, please  
>>>>>> edit  your Subject line so it is more specific than "Re:  
>>>>>> Contents of  stringtemplate-interest digest..." Today's Topics:  
>>>>>> 1. Re: handling  of arrays (John Snyders) 2. Re: inconsistency  
>>>>>> with length function  (Oliver Flege)   
>>>>>> ------------------------------------------------------------------- 
>>>>>> -- - Message: 1 Date: Sun, 28 Jan 2007 21:44:26 -0500 From:  
>>>>>> "John  Snyders" <jjsnyders at rcn.com> Subject: Re:  
>>>>>> [stringtemplate- interest] handling of arrays To: "Terence  
>>>>>> Parr"  <parrt at cs.usfca.edu>,	"StringTemplate" <stringtemplate-  
>>>>>> interest at antlr.org> Message-ID:   
>>>>>> <PMEOJKDLMMHOHNEAMPBGGEJMCAAA.jjsnyders at rcn.com> Content-Type:   
>>>>>> text/plain;	charset="US-ASCII"
>>>>>>
>>>>>>           
>>>>>>> -----Original Message----- From: stringtemplate-interest-  
>>>>>>> bounces at antlr.org [mailto:stringtemplate-interest-  
>>>>>>> bounces at antlr.org]On Behalf Of Terence Parr Sent: Saturday,   
>>>>>>> January 27, 2007 6:38 PM To: StringTemplate Subject: Re:   
>>>>>>> [stringtemplate-interest] handling of arrays On Jan 16, 2007,  
>>>>>>> at  9:40 PM, John Snyders wrote:
>>>>>>>
>>>>>>>             
>>>>>>>> I noticed in ASTExpr.java the method convertArrayToList.
>>>>>>>>
>>>>>>>>               
>>>>>>> it is. just done to make things consistent...else have to  
>>>>>>> check  for arrays everyone. gross, eh?
>>>>>>>
>>>>>>>             
>>>>>>>> It seems to me that this is wasteful. I have not looked at it   
>>>>>>>> too deep but why not handle arrays like other collections and   
>>>>>>>> wrap them in an ArrayIterator inside   
>>>>>>>> convertAnythingIteratableToIterator.
>>>>>>>>
>>>>>>>>               
>>>>>>> Interesting...ArrayIterator, eh? Does Sun have a standard one?  
>>>>>>> I  don't see it. Do you mean I should create one real quick?
>>>>>>>
>>>>>>>             
>>>>>> There isn't one in the Java SDK (as far as I know) but jakarta   
>>>>>> commons has one http://jakarta.apache.org/commons/collections/ 
>>>>>> api- release/org/apache/commons /collections/iterators/  
>>>>>> ArrayIterator.html ST probably shouldn't rely on this so you  
>>>>>> can  create your own. It is straight forward. I created one  
>>>>>> before I  found the one in commons collections. I can give it to  
>>>>>> you if you  like (assuming I can find it). But again I didn't  
>>>>>> think this  through fully.
>>>>>>
>>>>>>           
>>>>>>> Ter _______________________________________________   
>>>>>>> stringtemplate-interest mailing list stringtemplate-  
>>>>>>> interest at antlr.org http://www.antlr.org:8080/mailman/listinfo/  
>>>>>>> stringtemplate-interest
>>>>>>>
>>>>>>>             
>>>>>> ------------------------------ Message: 2 Date: Mon, 29 Jan  
>>>>>> 2007  10:25:24 +0100 From: Oliver Flege <o.flege at market- 
>>>>>> maker.de>  Subject: Re: [stringtemplate-interest] inconsistency  
>>>>>> with length  function To: StringTemplate <stringtemplate- 
>>>>>> interest at antlr.org>  Message-ID: <45BDBD84.4040707 at market- 
>>>>>> maker.de> Content-Type: text/ plain; charset=ISO-8859-1 Hi,  
>>>>>> Terence Parr wrote:
>>>>>>
>>>>>>           
>>>>>>>> I think that the length method in ASTExpr.java should be  
>>>>>>>> changed  as follows: From: } else if (attribute instanceof  
>>>>>>>> List) { i =  ((List)attribute).size(); } To: } else if  
>>>>>>>> (attribute instanceof  Collection) { i = ((List)attribute).size 
>>>>>>>> (); }
>>>>>>>>
>>>>>>>>               
>>>>>>> Howdy! OK, yep, good fix.
>>>>>>>
>>>>>>>             
>>>>>> even better would be } else if (attribute instanceof  
>>>>>> Collection)  { i = ((Collection)attribute).size(); } :) Cheers,  
>>>>>> Oliver  ------------------------------   
>>>>>> _______________________________________________ stringtemplate-  
>>>>>> interest mailing list stringtemplate-interest at antlr.org http://  
>>>>>> www.antlr.org:8080/mailman/listinfo/stringtemplate-interest End  
>>>>>> of  stringtemplate-interest Digest, Vol 22, Issue 14   
>>>>>> *******************************************************
>>>>>>
>>>>>>           
>>>>> <harry.karadimas.vcf>
>>>>> _______________________________________________
>>>>> 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
>>>>
>>>>       
>>
>>
>>
>> ------------------------------
>>
>> Message: 2
>> Date: Tue, 30 Jan 2007 13:13:24 -0800
>> From: Terence Parr <parrt at cs.usfca.edu>
>> Subject: Re: [stringtemplate-interest] handling of arrays (John
>> 	Snyders)
>> To: StringTemplate <stringtemplate-interest at antlr.org>
>> Message-ID: <5DE29967-5C4B-4700-8D51-EDB9CF147D51 at cs.usfca.edu>
>> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
>>
>> Ok, i made an array wrapper and iterator.  had to do both so a list  
>> is not purely an iterator, which has sideeffects.
>>
>> 	/** 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() ) {
>> 			return new ArrayWrappedInList(value);
>> 		}
>> 		return value;
>> 	}
>>
>> ArrayWrappedInList creates ArrayIterator.  These are two new classes.
>>
>> Pushed to depot.
>>
>> Ter
>>
>> On Jan 30, 2007, at 9:13 AM, Harry Karadimas wrote:
>>
>>   
>>> Hi, I don't know if that relates to the previous post, but java  
>>> *does* have something
>>> to encapsulate an array into a list, and that is the "asList"  
>>> method of the java.util.Arrays
>>> class.
>>>
>>> Object[] myArray = ...
>>> List myList = Arrays.asList(myArray);
>>>
>>> Harry Karadimas ____
>>>     
>>
>>
>> ------------------------------
>>
>> Message: 3
>> Date: Wed, 31 Jan 2007 02:21:11 +0100
>> From: Kay Roepke <kroepke at classdump.org>
>> Subject: Re: [stringtemplate-interest] Porting StringTemplate
>> To: Shane Witbeck <shane at digitalsanctum.com>
>> Cc: StringTemplate <stringtemplate-interest at antlr.org>
>> Message-ID: <6789A417-4627-450E-A443-BF753E93862F at classdump.org>
>> Content-Type: text/plain; charset=windows-1252; delsp=yes;
>> 	format=flowed
>>
>>
>> On Jan 28, 2007, at 1:04 AM, Shane Witbeck wrote:
>>
>>   
>>> It seems like porting to ECMAScript would be better strategically
>>> since ActionScript is an extension of it and the amount of work out
>>> there being done in JavaScript (AJAX, etc.).
>>>
>>> Shane
>>>
>>> On 1/27/07, Terence Parr <parrt at cs.usfca.edu> wrote:
>>>     
>>>> This would be very involved and a huge project.  ANTLR doesn't
>>>> generate ActionScript so you'd need to parse everything by hand.
>>>>
>>>>       
>>
>> Porting StringTemplate isn't exactly easy, esp. since it uses ANTLR  
>> v2. My attempts
>> to do an Obj-C port haven't been exactly successful, mainly because  
>> ANTLR v3 threw
>> up parsing ST :(
>>
>> I don't think that parsing ST by hand is a viable option, since it is  
>> a bit involved
>> at times. I certainly wouldn't attempt it unless I was desperate...
>> In the long run, though, v3 should be able to handle ST easily and  
>> that would make
>> it a lot easier (even if you'd need to write a new v3 target).
>>
>> As to the ECMAScript target, I'm not sure whether the current engines  
>> are really fit to
>> do recursive descent parsing in. All the engines I have seen have  
>> some real issues in
>> regard to memory, ie. tend to be slow molasses (but I admit it would  
>> a cool thing to have
>> now and then).
>>
>> my 0.02?,
>>
>> -k
>>
>>   
> _______________________________________________
> 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