Melzer, Steven wrote:
so i wrote the following:
        public static int size(Object list) throws IllegalArgumentException {
                int i = 0;
                if (list instanceof Collection) {
                        i = ((Collection)list).size();
                } else if (list instanceof Map) {
                        i = ((Map)list).size();
                } else {
                        i = Array.getLength(list);
                }
                return i;

thought it would be nice to include this functionality in an upcoming commons collection release.


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)
}


And then doing:


list = getListFromSomewhere()
XXX.toCollection(list).size()


I think this is a better solution, although I have the feeling that I'm missing something even more obvious here.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to