Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2463BEEEF for ; Tue, 26 Feb 2013 19:22:32 +0000 (UTC) Received: (qmail 53804 invoked by uid 500); 26 Feb 2013 19:22:31 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 53666 invoked by uid 500); 26 Feb 2013 19:22:31 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 53654 invoked by uid 99); 26 Feb 2013 19:22:31 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Feb 2013 19:22:31 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of thomas.neidhart@gmail.com designates 74.125.82.51 as permitted sender) Received: from [74.125.82.51] (HELO mail-wg0-f51.google.com) (74.125.82.51) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Feb 2013 19:22:25 +0000 Received: by mail-wg0-f51.google.com with SMTP id 8so3631059wgl.6 for ; Tue, 26 Feb 2013 11:22:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=xdILI536HA7JcEflekVw9/+zIv5SFjpENKiaF3OQDUs=; b=rccibr6zYRkXngDbCF5gXW0ZM/ZRIWRLOqkisMcMMjqi72WXWKTyYkEEBfX5upd+vC I3kUDyKb+1JgGrxXRMzIwtlLQdhwZFZ35Ku1Dy1tIiWQQhlaza573lqYsJPJObto9z7m AL77sBu26X0fXp2GU1Y01ymG4YdgNEYxUkovvFdCfNUOJwU8CpFP7D+JxYWwg0wLcXqz gaw51bKpBFoArNF7WCiz0vh8VhzXBMf1s05h3USKhrUyd14dDks7QiZfP4pUlf5tzniU kQ0Wpoh9BazdcEHasf3zfVurq3rgFWzxOBxiQRG+F+QdaVsfV/FZ7qLcAY7VmLg3xV7N 17gg== X-Received: by 10.180.92.129 with SMTP id cm1mr21661948wib.10.1361906523941; Tue, 26 Feb 2013 11:22:03 -0800 (PST) Received: from [192.168.1.2] (ip-83-134-114-250.dsl.scarlet.be. [83.134.114.250]) by mx.google.com with ESMTPS id ex15sm22991342wid.5.2013.02.26.11.22.02 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 26 Feb 2013 11:22:03 -0800 (PST) Message-ID: <512D0B58.1020403@gmail.com> Date: Tue, 26 Feb 2013 20:22:00 +0100 From: Thomas Neidhart User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: Commons Developers List Subject: Re: svn commit: r1449929 - /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/bag/AbstractMapBag.java References: <20130225221822.BC05F2388900@eris.apache.org> <512C65B9.7090803@gmail.com> In-Reply-To: X-Enigmail-Version: 1.5 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Checked: Checked by ClamAV on apache.org On 02/26/2013 06:37 PM, sebb wrote: > On 26 February 2013 17:04, J�rg Schaible wrote: >> sebb wrote: >> >>> On 26 February 2013 10:27, Simone Tripodi >>> 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 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[] 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 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