You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Simone Tripodi <si...@apache.org> on 2013/02/26 07:57:01 UTC

Re: svn commit: r1449929 - /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/bag/AbstractMapBag.java

Hi Thomas,

> +    @SuppressWarnings("unchecked")
>      public <T> T[] toArray(T[] array) {
>          final int size = size();
>          if (array.length < size) {
> -            // This is safe, both are type T
>              array = (T[]) Array.newInstance(array.getClass().getComponentType(), size);
>          }
>
> @@ -466,7 +469,6 @@ public abstract class AbstractMapBag<E>
>          while (it.hasNext()) {
>              final E current = it.next();
>              for (int index = getCount(current); index > 0; index--) {
> -                // TODO this is unsafe
>                  array[i++] = (T) current;
>              }
>          }
>

it is a good practice here commenting why the unchecked warning can be
suppressed - it will definitively help also future maintainers :)

TIA!
-Simo

http://people.apache.org/~simonetripodi/
http://simonetripodi.livejournal.com/
http://twitter.com/simonetripodi
http://www.99soft.org/

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


Re: svn commit: r1449929 - /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/bag/AbstractMapBag.java

Posted by Thomas Neidhart <th...@gmail.com>.
On 02/26/2013 06:37 PM, sebb wrote:
> On 26 February 2013 17:04, Jörg Schaible <Jo...@scalaris.com> wrote:
>> sebb wrote:
>>
>>> On 26 February 2013 10:27, Simone Tripodi <si...@apache.org>
>>> wrote:
>>>>> it is not safe, and it will throw an ArrayStoreException in this case,
>>>>> which is documented in the throws clause.
>>>>>
>>>>
>>>> OK I just read the commit, unless it is documented it is fine for me
>>>
>>> Should still be documented on the @SuppressWarnings line please, but
>>> can refer to Javadoc.
>>
>> In that case I typically prefer the suppression as close as possible, i.e.:
>>
>> =========== %< =========
>>  if (array.length < size)
>>  {
>>    @SuppressWarnings("unchecked")
>>    T[] unchecked = Array.newInstance(array.getClass().getComponentType(),
>> size);
>>    array = unchecked;
>>  }
>> =========== %< =========
>>
>> Otherwise you might suppress more than wanted.
> 
> +1

Well, normally I am quite open for such suggestions, but I did this
change after looking at the openjdk code. The change would look like this:

    /**
     * Returns an array of all of this bag's elements.
     * If the input array has more elements than are in the bag,
     * trailing elements will be set to null.
     *
     * @param <T> the type of the array elements
     * @param array the array to populate
     * @return an array of all of this bag's elements
     * @throws ArrayStoreException if the runtime type of the specified
array is not
     *   a supertype of the runtime type of the elements in this list
     * @throws NullPointerException if the specified array is null
     */
    public <T> T[] toArray(T[] array) {
        final int size = size();
        if (array.length < size) {
            @SuppressWarnings("unchecked") // safe as both are of type T
            final T[] unchecked = (T[])
Array.newInstance(array.getClass().getComponentType(), size);
            array = unchecked;
        }

        int i = 0;
        final Iterator<E> it = map.keySet().iterator();
        while (it.hasNext()) {
            final E current = it.next();
            for (int index = getCount(current); index > 0; index--) {
                // unsafe, will throw ArrayStoreException if types are
not compatible, see javadoc
                @SuppressWarnings("unchecked")
                final T unchecked = (T) current;
                array[i++] = unchecked;
            }
        }
        while (i < array.length) {
            array[i++] = null;
        }
        return array;
    }

Any other toArray(T[]) method does not have this level of detail, but at
least this one now.

Thomas

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


Re: svn commit: r1449929 - /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/bag/AbstractMapBag.java

Posted by sebb <se...@gmail.com>.
On 26 February 2013 17:04, Jörg Schaible <Jo...@scalaris.com> wrote:
> sebb wrote:
>
>> On 26 February 2013 10:27, Simone Tripodi <si...@apache.org>
>> wrote:
>>>> it is not safe, and it will throw an ArrayStoreException in this case,
>>>> which is documented in the throws clause.
>>>>
>>>
>>> OK I just read the commit, unless it is documented it is fine for me
>>
>> Should still be documented on the @SuppressWarnings line please, but
>> can refer to Javadoc.
>
> In that case I typically prefer the suppression as close as possible, i.e.:
>
> =========== %< =========
>  if (array.length < size)
>  {
>    @SuppressWarnings("unchecked")
>    T[] unchecked = Array.newInstance(array.getClass().getComponentType(),
> size);
>    array = unchecked;
>  }
> =========== %< =========
>
> Otherwise you might suppress more than wanted.

+1

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

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


Re: svn commit: r1449929 - /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/bag/AbstractMapBag.java

Posted by Jörg Schaible <Jo...@scalaris.com>.
sebb wrote:

> On 26 February 2013 10:27, Simone Tripodi <si...@apache.org>
> wrote:
>>> it is not safe, and it will throw an ArrayStoreException in this case,
>>> which is documented in the throws clause.
>>>
>>
>> OK I just read the commit, unless it is documented it is fine for me
> 
> Should still be documented on the @SuppressWarnings line please, but
> can refer to Javadoc.

In that case I typically prefer the suppression as close as possible, i.e.:

=========== %< =========
 if (array.length < size)
 {
   @SuppressWarnings("unchecked")
   T[] unchecked = Array.newInstance(array.getClass().getComponentType(), 
size);
   array = unchecked;
 }
=========== %< =========

Otherwise you might suppress more than wanted.

- Jörg


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


Re: svn commit: r1449929 - /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/bag/AbstractMapBag.java

Posted by sebb <se...@gmail.com>.
On 26 February 2013 10:27, Simone Tripodi <si...@apache.org> wrote:
>> it is not safe, and it will throw an ArrayStoreException in this case,
>> which is documented in the throws clause.
>>
>
> OK I just read the commit, unless it is documented it is fine for me

Should still be documented on the @SuppressWarnings line please, but
can refer to Javadoc.

> -Simo
>
> http://people.apache.org/~simonetripodi/
> http://simonetripodi.livejournal.com/
> http://twitter.com/simonetripodi
> http://www.99soft.org/
>
>
> On Tue, Feb 26, 2013 at 8:35 AM, Thomas Neidhart
> <th...@gmail.com> wrote:
>> On 02/26/2013 07:57 AM, Simone Tripodi wrote:
>>> Hi Thomas,
>>>
>>>> +    @SuppressWarnings("unchecked")
>>>>      public <T> T[] toArray(T[] array) {
>>>>          final int size = size();
>>>>          if (array.length < size) {
>>>> -            // This is safe, both are type T
>>>>              array = (T[]) Array.newInstance(array.getClass().getComponentType(), size);
>>>>          }
>>>>
>>>> @@ -466,7 +469,6 @@ public abstract class AbstractMapBag<E>
>>>>          while (it.hasNext()) {
>>>>              final E current = it.next();
>>>>              for (int index = getCount(current); index > 0; index--) {
>>>> -                // TODO this is unsafe
>>>>                  array[i++] = (T) current;
>>>>              }
>>>>          }
>>>>
>>>
>>> it is a good practice here commenting why the unchecked warning can be
>>> suppressed - it will definitively help also future maintainers :)
>>
>> it is not safe, and it will throw an ArrayStoreException in this case,
>> which is documented in the throws clause.
>>
>> This is the same behavior as with all java.util collections.
>>
>> Thomas
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

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


Re: svn commit: r1449929 - /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/bag/AbstractMapBag.java

Posted by Simone Tripodi <si...@apache.org>.
> it is not safe, and it will throw an ArrayStoreException in this case,
> which is documented in the throws clause.
>

OK I just read the commit, unless it is documented it is fine for me

-Simo

http://people.apache.org/~simonetripodi/
http://simonetripodi.livejournal.com/
http://twitter.com/simonetripodi
http://www.99soft.org/


On Tue, Feb 26, 2013 at 8:35 AM, Thomas Neidhart
<th...@gmail.com> wrote:
> On 02/26/2013 07:57 AM, Simone Tripodi wrote:
>> Hi Thomas,
>>
>>> +    @SuppressWarnings("unchecked")
>>>      public <T> T[] toArray(T[] array) {
>>>          final int size = size();
>>>          if (array.length < size) {
>>> -            // This is safe, both are type T
>>>              array = (T[]) Array.newInstance(array.getClass().getComponentType(), size);
>>>          }
>>>
>>> @@ -466,7 +469,6 @@ public abstract class AbstractMapBag<E>
>>>          while (it.hasNext()) {
>>>              final E current = it.next();
>>>              for (int index = getCount(current); index > 0; index--) {
>>> -                // TODO this is unsafe
>>>                  array[i++] = (T) current;
>>>              }
>>>          }
>>>
>>
>> it is a good practice here commenting why the unchecked warning can be
>> suppressed - it will definitively help also future maintainers :)
>
> it is not safe, and it will throw an ArrayStoreException in this case,
> which is documented in the throws clause.
>
> This is the same behavior as with all java.util collections.
>
> Thomas
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

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


Re: svn commit: r1449929 - /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/bag/AbstractMapBag.java

Posted by Thomas Neidhart <th...@gmail.com>.
On 02/26/2013 07:57 AM, Simone Tripodi wrote:
> Hi Thomas,
> 
>> +    @SuppressWarnings("unchecked")
>>      public <T> T[] toArray(T[] array) {
>>          final int size = size();
>>          if (array.length < size) {
>> -            // This is safe, both are type T
>>              array = (T[]) Array.newInstance(array.getClass().getComponentType(), size);
>>          }
>>
>> @@ -466,7 +469,6 @@ public abstract class AbstractMapBag<E>
>>          while (it.hasNext()) {
>>              final E current = it.next();
>>              for (int index = getCount(current); index > 0; index--) {
>> -                // TODO this is unsafe
>>                  array[i++] = (T) current;
>>              }
>>          }
>>
> 
> it is a good practice here commenting why the unchecked warning can be
> suppressed - it will definitively help also future maintainers :)

it is not safe, and it will throw an ArrayStoreException in this case,
which is documented in the throws clause.

This is the same behavior as with all java.util collections.

Thomas

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