[stringtemplate-interest] handling of arrays (John Snyders)

Nate misc at n4te.com
Tue Jan 30 11:54:50 PST 2007


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
>   



More information about the stringtemplate-interest mailing list