commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Janek Bogucki <...@studylink.com>
Subject Re: [collections] collections size
Date Fri, 19 Mar 2004 23:21:24 GMT
On Fri, 2004-03-19 at 19:26, matthew.hawthorne wrote:
> I understand the underlying idea behind your suggestion, but I don't 
> quite get the use case.  Why create conditional llogic to get the size 
> of an arbitrary list, when you'd then have to implement the same 
> conditional logic to get the items out of the list?
> 
> If your goal is to treat all lists in a similar way, I think you'd be 
> better of doing something like:
> 
> public static Collection toCollection(Object list) {
>    if(list instanceof Collection)
>      return list
>    else if (list instanceof Map)
>      return ((Map)list).entrySet()
>    else
>      return Arrays.asList(list)
> }

While I agree with the idea of using a Collection view of the object
when you want to do Collection things on the object, there is a
precedent for this sort of approach in o.a.c.collections in the form of
CollectionUtils.get(Object, int). This could be used to get the items
out of the 'list' although performance would suffer in some cases.

Edited JavaDoc:

     Returns the index-th value in object.

     Supports:

     Map
     List 
     Object Array 
     Collection 
     Iterator or Enumeration

     public static Object get(Object object, int index)

-Janek

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message