Return-Path: Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: (qmail 6889 invoked from network); 25 Sep 2009 09:54:38 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 25 Sep 2009 09:54:38 -0000 Received: (qmail 6348 invoked by uid 500); 25 Sep 2009 09:54:38 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 6243 invoked by uid 500); 25 Sep 2009 09:54:38 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 6233 invoked by uid 99); 25 Sep 2009 09:54:38 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Sep 2009 09:54:38 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Sep 2009 09:54:36 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 51908234C4A9 for ; Fri, 25 Sep 2009 02:54:16 -0700 (PDT) Message-ID: <412479969.1253872456333.JavaMail.jira@brutus> Date: Fri, 25 Sep 2009 02:54:16 -0700 (PDT) From: "Joerg Schaible (JIRA)" To: issues@commons.apache.org Subject: [jira] Issue Comment Edited: (LANG-534) ArrayUtils should have method to convert null arrays to empty ones to help with Defensive coding In-Reply-To: <904928838.1253516777914.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/LANG-534?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12759445#action_12759445 ] Joerg Schaible edited comment on LANG-534 at 9/25/09 2:53 AM: -------------------------------------------------------------- bq. No, this just returns null if you give it a null array... This is not completely true, calling {code:java} String[] stringArray = ArrayUtils.nullToEmpty(); {code} will return an empty array of proper type. Therefore we actually talk about a new method that can be utilized (LANG-537): {code:java} public static T[] toArry(T... items) { return items; } public static T[] nullToEmpty(T[] items) { if (items != null) return items; else return ArrayUtils.toArray(); } {code} I've not tested the latter method, but the former definitely works for empty arguments. was (Author: joehni): bq. No, this just returns null if you give it a null array... This is not completely true, calling {code:java} String[] stringArray = ArrayUtils.nullToEmpty(); {code} will return an empty array of proper type. Therefore we actually talk about a new method that can be utilized (LANG-537): {code:java} public static T[] createArry(T... items) { return items; } public static T[] nullToEmpty(T[] items) { if (items != null) return items; else return ArrayUtils.createArray(); } {code} I've not tested the latter method, but the former definitely works for empty arguments. > ArrayUtils should have method to convert null arrays to empty ones to help with Defensive coding > ------------------------------------------------------------------------------------------------ > > Key: LANG-534 > URL: https://issues.apache.org/jira/browse/LANG-534 > Project: Commons Lang > Issue Type: Improvement > Reporter: Levon Karayan > Priority: Trivial > Attachments: 20090920LevonArrayUtils-nullToEmpty.patch > > > There are APIs that I've come across that return null Arrays in the event where there are no results. Often these APIs correctly throw exceptions when there is an "exceptional event", but no results isn't exceptional, and it often shouldn't be. This causes the programmer to make extra tests for null throughout the code to deal with the null case, and sometimes these null cases are added after a customer searched for gobleygook and got a NullPointerException. It's just far cleaner/safer to convert these null arrays to empty arrays. > Another benefit to this method is that if the array being passed in is actually already an empty array, it will swap the pointer for the static final in the ArrayUtils class to help decrease memory fragmentation. > e.g. > BEFORE: > try > { > results = customer.getResults(query); > } catch ( IOException ioex ) { > // ... > } > if ( null == results ) > { > results = new int[0]{}; > } > // do stuff > AFTER > try > { > results = ArrayUtils.nullToEmpty(customer.getResults(query)); > } catch ( IOException ioex ) { > // ... > } > // do stuff -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.