commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: svn commit: r983137 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java
Date Sun, 08 Aug 2010 17:25:31 GMT
On 6 August 2010 23:44,  <jcarman@apache.org> wrote:
> Author: jcarman
> Date: Fri Aug  6 22:44:38 2010
> New Revision: 983137
>
> URL: http://svn.apache.org/viewvc?rev=983137&view=rev
> Log:
> Generifying toMap() method (adding in possibility for type inference on return type).
>
> Modified:
>    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java
>
> Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java
> URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java?rev=983137&r1=983136&r2=983137&view=diff
> ==============================================================================
> --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java
(original)
> +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java
Fri Aug  6 22:44:38 2010
> @@ -16,6 +16,7 @@
>  */
>  package org.apache.commons.lang3;
>
> +import java.awt.Color;

???

>  import java.lang.reflect.Array;
>  import java.util.HashMap;
>  import java.util.Map;
> @@ -222,16 +223,17 @@ public class ArrayUtils {
>      * @throws IllegalArgumentException  if the array contains elements other
>      *  than {@link java.util.Map.Entry} and an Array
>      */
> -    public static Map<Object, Object> toMap(Object[] array) {
> +    @SuppressWarnings("unchecked")

Why is it safe to suppress warnings?

If it is really necessary, please document why it is safe, e.g. as is
done in ArrayUtils.addAll()


> +    public static <K,V> Map<K, V> toMap(Object[] array) {
>         if (array == null) {
>             return null;
>         }
> -        final Map<Object, Object> map = new HashMap<Object, Object>((int)
(array.length * 1.5));
> +        final Map<K, V> map = new HashMap<K, V>((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());
> +                map.put((K)entry.getKey(), (V)entry.getValue());
>             } else if (object instanceof Object[]) {
>                 Object[] entry = (Object[]) object;
>                 if (entry.length < 2) {
> @@ -239,7 +241,7 @@ public class ArrayUtils {
>                         + object
>                         + "', has a length less than 2");
>                 }
> -                map.put(entry[0], entry[1]);
> +                map.put((K)entry[0], (V)entry[1]);

As it stands, the code allows calls of the form:

Map<Integer,Integer> map = ArrayUtils.toMap(new String[][] {{"foo",
"bar"}, {"hello", "world"}});

without complaining.

Failing to check the types of the array entries on creation means that
the error may lie undetected until much later, making it potentially
much harder to debug.

>             } else {
>                 throw new IllegalArgumentException("Array element " + i + ",
'"
>                         + object
>
>
>

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


Mime
View raw message