commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Shekhar Gulati <shekhargulat...@gmail.com>
Subject [lang] : Question on Usage of Generics
Date Fri, 06 Aug 2010 20:54:54 GMT
Hello,

I have started playing with latest beta release of Commons-Lang 3.0  and I
have question on usage of generics in Commons-lang

For example, there is a method called toMap in ArrayUtils which could have
been easily generified but isn't

In Commons-Lang 3.0

public static Map<Object, Object> toMap(Object[] array) {
        if (array == null) {
            return null;
        }
        final Map<Object, Object> map = new HashMap<Object, Object>((int)
(array.length * 1.5));
        for (int i = 0; i < array.length; i++) {
            Object object = array[i];
            if (object instanceof Map.Entry<?, ?>) {
                Map.Entry<?,?> entry = (Map.Entry<?,?>) object;
                map.put(entry.getKey(), entry.getValue());
            } else if (object instanceof Object[]) {
                Object[] entry = (Object[]) object;
                if (entry.length < 2) {
                    throw new IllegalArgumentException("Array element " + i
+ ", '"
                        + object
                        + "', has a length less than 2");
                }
                map.put(entry[0], entry[1]);
            } else {
                throw new IllegalArgumentException("Array element " + i + ",
'"
                        + object
                        + "', is neither of type Map.Entry nor an Array");
            }
        }
        return map;
    }



Fully Generified version of toMap method

public static <T> Map<T, T> toMap(T[][] array) {
if (array == null) {
return null;
}
final Map<T, T> map = new HashMap<T, T>((int) (array.length * 1.5));
for (int i = 0; i < array.length; i++) {
T[] t = array[i];
if (t[i] instanceof Map.Entry) {
@SuppressWarnings("unchecked")
Map.Entry<T, T> entry = (Map.Entry<T, T>) t[i];
map.put(entry.getKey(), entry.getValue());
} else if (t instanceof Object[]) {
T[] entry = t;
if (entry.length < 2) {
throw new IllegalArgumentException("Array element " + i
+ ", '" + t + "', has a length less than 2");
}
map.put(entry[0], entry[1]);
} else {
throw new IllegalArgumentException("Array element " + i + ", '"
+ t + "', is neither of type Map.Entry nor an Array");
}
}
return map;
}

I just thought to ask the Commons-Lang team.. Are there any reason why some
methods are not generified?




-- 
Thanks
Shekhar
09873937317

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message