From issues-return-68604-archive-asf-public=cust-asf.ponee.io@commons.apache.org Mon Jul 9 01:16:22 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id CD48F180630 for ; Mon, 9 Jul 2018 01:16:21 +0200 (CEST) Received: (qmail 96792 invoked by uid 500); 8 Jul 2018 23:16:20 -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 96781 invoked by uid 99); 8 Jul 2018 23:16:20 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 08 Jul 2018 23:16:20 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 0E0A7DFE18; Sun, 8 Jul 2018 23:16:20 +0000 (UTC) From: MarkDacek To: issues@commons.apache.org Reply-To: issues@commons.apache.org References: In-Reply-To: Subject: [GitHub] commons-lang pull request #336: LANG-1402: Null/index safe get methods for A... Content-Type: text/plain Message-Id: <20180708231620.0E0A7DFE18@git1-us-west.apache.org> Date: Sun, 8 Jul 2018 23:16:20 +0000 (UTC) Github user MarkDacek commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/336#discussion_r200857848 --- Diff: src/main/java/org/apache/commons/lang3/ArrayUtils.java --- @@ -8672,4 +8672,53 @@ public static void shuffle(final double[] array, final Random random) { swap(array, i - 1, random.nextInt(i), 1); } } + + /** + * Gets an element from the array if the array is non-null and appropriately long, otherwise returns null + * @param the component type of the array + * @param array the array holding the desired element + * @param index the index of the element in the array + * @return The element in the array at the index, or null if it is ill-formatted + * @since 3.8 + */ + public static T get(T[] array, int index){ + return get(array, index, null); + } + + /** + * Gets an element from the array if the array is non-null and appropriately long, otherwise returns the specified value + * @param the component type of the array + * @param array the array holding the desired element + * @param index the index of the element in the array + * @param defaultReturn the object to be returned if the array is null or shorter than the index + * @return The element in the array at the specified index, or the given argument if it is ill-formatted + * @since 3.8 + */ + public static T get(T[] array, int index, T defaultReturn){ + if(getLength(array) == 0 || array.length <= index){ + return defaultReturn; + } + + if(index < 0 ){ + index = 0; --- End diff -- Fixed. ---